diff --git a/animate2.cpp b/animate2.cpp index 2b57eb2..045aac3 100644 --- a/animate2.cpp +++ b/animate2.cpp @@ -33,7 +33,6 @@ namespace animate2 { playing = true; sequence.timer.start(); sequence.frame_count = sequence.frames.size(); - $frame_rects = calc_frames(); } void Animate2::stop() { @@ -149,9 +148,9 @@ namespace animate2 { void Transform::lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out) { // float dt = 1 - std::powf(ease_rate, seq.subframe + 0.0001); - float tick = easing(seq.subframe); + float tick = easing_func(seq.subframe); - motion(*this, pos_out, scale_out, tick); + motion_func(*this, pos_out, scale_out, tick); // fmt::println("sub: {}, tick: {}, tr: {},{}; pos: {},{}; scale: {},{}", // seq.subframe, tick, min_y, max_y, pos_out.x, pos_out.y, @@ -166,6 +165,11 @@ namespace animate2 { Animate2 anim; animate2::from_json(data[anim_name], anim); + // BUG: json doesn't like may "fancy" no-constructor bullshit + anim.$frame_rects = anim.calc_frames(); + anim.transform.easing_func = ease2::get_easing(anim.transform.easing); + anim.transform.motion_func = ease2::get_motion(anim.transform.motion); + return anim; } } diff --git a/animate2.hpp b/animate2.hpp index 3642da5..51f17d7 100644 --- a/animate2.hpp +++ b/animate2.hpp @@ -60,11 +60,14 @@ namespace animate2 { // handled by onLoop bool toggled{false}; bool looped{false}; + std::string easing{"in_out_back"}; + std::string motion{"move_rush"}; + + // change to using a callback function for these + ease2::EaseFunc easing_func{ease2::get_easing(easing)}; + ease2::MotionFunc motion_func{ease2::get_motion(motion)}; std::shared_ptr shader{nullptr}; - // change to using a callback function for these - ease2::EaseFunc easing = ease2::in_out_back; - ease2::MoveFunc motion = ease2::move_rush; void lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out); }; @@ -108,6 +111,7 @@ namespace animate2 { ENROLL_COMPONENT(Sheet, frames, frame_width, frame_height); ENROLL_COMPONENT(Sequence, frames, durations); - ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y, simple, flipped, ease_rate, scaled, toggled, looped); + ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y, simple, + flipped, ease_rate, scaled, toggled, looped, easing, motion); ENROLL_COMPONENT(Animate2, sheet, sequence, transform); } diff --git a/assets/animate2.json b/assets/animate2.json index 1e0131d..a43e7d5 100644 --- a/assets/animate2.json +++ b/assets/animate2.json @@ -19,7 +19,9 @@ "ease_rate": 5.0, "scaled": true, "toggled": false, - "looped": true + "looped": true, + "easing": "out_circle", + "motion": "scale_grow" } }, "rat_king_meth": { @@ -30,19 +32,21 @@ }, "sequence": { "frames": [0, 1], - "durations": [33, 33] + "durations": [133, 133] }, "transform": { - "min_x": 0.6, - "min_y": 0.6, - "max_x": 0.8, - "max_y": 0.8, + "min_x": -20.0, + "min_y": -20.0, + "max_x": 20.0, + "max_y": 20.0, "simple": false, - "flipped": false, + "flipped": true, "ease_rate": 5.0, "scaled": true, "toggled": false, - "looped": true + "looped": true, + "easing": "normal_dist", + "motion": "move_shake" } } } diff --git a/ease2.cpp b/ease2.cpp index 993e028..adc6163 100644 --- a/ease2.cpp +++ b/ease2.cpp @@ -2,6 +2,8 @@ #include "rand.hpp" #include "animate2.hpp" #include +#include +#include "dbc.hpp" namespace ease2 { using namespace animate2; @@ -96,4 +98,39 @@ namespace ease2 { scale_out.x = std::lerp(scale_out.x * tr.min_x, scale_out.x * tr.max_x, tick); scale_out.y = std::lerp(scale_out.y * tr.min_y, scale_out.y * tr.max_y, tick); } + + std::unordered_map map_of_easings{ + {"sine", sine}, + {"out_circle", out_circle}, + {"out_bounce", out_bounce}, + {"in_out_back", in_out_back}, + {"random", random}, + {"normal_dist", normal_dist}, + }; + + std::unordered_map map_of_motions{ + {"move_bounce", move_bounce}, + {"move_rush", move_rush}, + {"scale_squeeze", scale_squeeze}, + {"scale_squash", scale_squash}, + {"scale_stretch", scale_stretch}, + {"scale_grow", scale_grow}, + {"move_slide", move_slide}, + {"move_none", move_none}, + {"scale_only", scale_only}, + {"move_shake", move_shake}, + }; + + EaseFunc get_easing(const std::string& name) { + dbc::check(map_of_easings.contains(name), + fmt::format("easing name {} does not exist", name)); + return map_of_easings.at(name); + } + + MotionFunc get_motion(const std::string& name) { + dbc::check(map_of_motions.contains(name), + fmt::format("motion name {} does not exist", name)); + return map_of_motions.at(name); + } + } diff --git a/ease2.hpp b/ease2.hpp index 4979257..391ab8c 100644 --- a/ease2.hpp +++ b/ease2.hpp @@ -7,7 +7,10 @@ namespace animate2 { namespace ease2 { using EaseFunc = std::function; - using MoveFunc = std::function; + using MotionFunc = std::function; + + EaseFunc get_easing(const std::string& name); + MotionFunc get_motion(const std::string& name); double sine(double x); double out_circle(double x); diff --git a/tools/animator.cpp b/tools/animator.cpp index 7f18cd9..74d52ce 100644 --- a/tools/animator.cpp +++ b/tools/animator.cpp @@ -13,10 +13,9 @@ /* * TODO: * - * - Setting a background. - * - Options parsing for that. * - Easing functions from strings in json. * - Map of animation forms. + * - Bring back shaders. */ using namespace std::chrono_literals;