Animation tool now lets you cycle through different sequence/transform 'forms' and shows you which one you're viewing.
This commit is contained in:
parent
07b2102f59
commit
4356b1535e
5 changed files with 170 additions and 56 deletions
33
animate2.cpp
33
animate2.cpp
|
|
@ -157,6 +157,31 @@ namespace animate2 {
|
||||||
// scale_out.x, scale_out.y);
|
// scale_out.x, scale_out.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Animate2::set_form(const std::string& as_form) {
|
||||||
|
dbc::check(forms.contains(as_form),
|
||||||
|
fmt::format("form {} does not exist in animation", as_form));
|
||||||
|
stop();
|
||||||
|
|
||||||
|
const auto& [seq_name, tr_name] = forms.at(as_form);
|
||||||
|
|
||||||
|
dbc::check(sequences.contains(seq_name),
|
||||||
|
fmt::format("sequences do NOT have \"{}\" name", seq_name));
|
||||||
|
|
||||||
|
dbc::check(transforms.contains(tr_name),
|
||||||
|
fmt::format("transforms do NOT have \"{}\" name", tr_name));
|
||||||
|
|
||||||
|
// everything good, do the update
|
||||||
|
form_name = as_form;
|
||||||
|
sequence_name = seq_name;
|
||||||
|
transform_name = tr_name;
|
||||||
|
sequence = sequences.at(seq_name);
|
||||||
|
transform = transforms.at(tr_name);
|
||||||
|
|
||||||
|
$frame_rects = calc_frames();
|
||||||
|
transform.easing_func = ease2::get_easing(transform.easing);
|
||||||
|
transform.motion_func = ease2::get_motion(transform.motion);
|
||||||
|
}
|
||||||
|
|
||||||
Animate2 load(const std::string &file, const std::string &anim_name) {
|
Animate2 load(const std::string &file, const std::string &anim_name) {
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
std::ifstream infile(file);
|
std::ifstream infile(file);
|
||||||
|
|
@ -165,10 +190,10 @@ namespace animate2 {
|
||||||
Animate2 anim;
|
Animate2 anim;
|
||||||
animate2::from_json(data[anim_name], anim);
|
animate2::from_json(data[anim_name], anim);
|
||||||
|
|
||||||
// BUG: json doesn't like may "fancy" no-constructor bullshit
|
dbc::check(anim.forms.contains("idle"),
|
||||||
anim.$frame_rects = anim.calc_frames();
|
fmt::format("animation {} must have 'idle' form", anim_name));
|
||||||
anim.transform.easing_func = ease2::get_easing(anim.transform.easing);
|
|
||||||
anim.transform.motion_func = ease2::get_motion(anim.transform.motion);
|
anim.set_form("idle");
|
||||||
|
|
||||||
return anim;
|
return anim;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
animate2.hpp
18
animate2.hpp
|
|
@ -86,20 +86,32 @@ namespace animate2 {
|
||||||
return tr.looped;
|
return tr.looped;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using Form = std::pair<std::string, std::string>;
|
||||||
|
|
||||||
class Animate2 {
|
class Animate2 {
|
||||||
public:
|
public:
|
||||||
Sheet sheet;
|
Sheet sheet;
|
||||||
Sequence sequence;
|
std::unordered_map<std::string, Sequence> sequences;
|
||||||
Transform transform;
|
std::unordered_map<std::string, Transform> transforms;
|
||||||
|
std::unordered_map<std::string, Form> forms;
|
||||||
OnFrameHandler onFrame = nullptr;
|
OnFrameHandler onFrame = nullptr;
|
||||||
|
|
||||||
|
Sequence sequence{};
|
||||||
|
Transform transform{};
|
||||||
|
|
||||||
std::vector<sf::IntRect> $frame_rects{calc_frames()};
|
std::vector<sf::IntRect> $frame_rects{calc_frames()};
|
||||||
OnLoopHandler onLoop = DefaultOnLoop;
|
OnLoopHandler onLoop = DefaultOnLoop;
|
||||||
bool playing = false;
|
bool playing = false;
|
||||||
|
|
||||||
|
// mostly for debugging purposes
|
||||||
|
std::string form_name="idle";
|
||||||
|
std::string sequence_name="";
|
||||||
|
std::string transform_name="";
|
||||||
|
|
||||||
std::vector<sf::IntRect> calc_frames();
|
std::vector<sf::IntRect> calc_frames();
|
||||||
void play();
|
void play();
|
||||||
void stop();
|
void stop();
|
||||||
|
void set_form(const std::string& form);
|
||||||
void apply(sf::Sprite& sprite);
|
void apply(sf::Sprite& sprite);
|
||||||
void update_frame();
|
void update_frame();
|
||||||
void update();
|
void update();
|
||||||
|
|
@ -113,5 +125,5 @@ namespace animate2 {
|
||||||
ENROLL_COMPONENT(Sequence, frames, durations);
|
ENROLL_COMPONENT(Sequence, frames, durations);
|
||||||
ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y, simple,
|
ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y, simple,
|
||||||
flipped, ease_rate, scaled, toggled, looped, easing, motion);
|
flipped, ease_rate, scaled, toggled, looped, easing, motion);
|
||||||
ENROLL_COMPONENT(Animate2, sheet, sequence, transform);
|
ENROLL_COMPONENT(Animate2, sheet, sequences, transforms, forms);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,48 +5,59 @@
|
||||||
"frame_width": 720,
|
"frame_width": 720,
|
||||||
"frame_height": 720
|
"frame_height": 720
|
||||||
},
|
},
|
||||||
"sequence": {
|
"sequences": {
|
||||||
"frames": [0, 1],
|
"normal": {"frames": [0, 1], "durations": [800, 240] },
|
||||||
"durations": [240, 240]
|
"reversed": {"frames": [1, 0], "durations": [800, 200] },
|
||||||
|
"meth": {"frames": [0, 1], "durations": [33, 33] }
|
||||||
},
|
},
|
||||||
"transform": {
|
"transforms": {
|
||||||
"min_x": 0.6,
|
"rushing": {
|
||||||
"min_y": 0.6,
|
"min_x": 0.6,
|
||||||
"max_x": 0.8,
|
"min_y": 0.6,
|
||||||
"max_y": 0.8,
|
"max_x": 0.8,
|
||||||
"simple": false,
|
"max_y": 0.8,
|
||||||
"flipped": false,
|
"simple": false,
|
||||||
"ease_rate": 5.0,
|
"flipped": false,
|
||||||
"scaled": true,
|
"ease_rate": 5.0,
|
||||||
"toggled": false,
|
"scaled": true,
|
||||||
"looped": true,
|
"toggled": false,
|
||||||
"easing": "out_circle",
|
"looped": true,
|
||||||
"motion": "scale_grow"
|
"easing": "in_out_back",
|
||||||
}
|
"motion": "move_rush"
|
||||||
},
|
},
|
||||||
"rat_king_meth": {
|
"meth": {
|
||||||
"sheet": {
|
"min_x": -20.0,
|
||||||
"frames": 2,
|
"min_y": -20.0,
|
||||||
"frame_width": 720,
|
"max_x": 20.0,
|
||||||
"frame_height": 720
|
"max_y": 20.0,
|
||||||
|
"simple": true,
|
||||||
|
"flipped": true,
|
||||||
|
"ease_rate": 5.0,
|
||||||
|
"scaled": true,
|
||||||
|
"toggled": false,
|
||||||
|
"looped": true,
|
||||||
|
"easing": "normal_dist",
|
||||||
|
"motion": "move_shake"
|
||||||
|
},
|
||||||
|
"breathe": {
|
||||||
|
"min_x": 0.90,
|
||||||
|
"min_y": 0.90,
|
||||||
|
"max_x": 1.0,
|
||||||
|
"max_y": 1.0,
|
||||||
|
"simple": true,
|
||||||
|
"flipped": false,
|
||||||
|
"ease_rate": 3.5,
|
||||||
|
"scaled": true,
|
||||||
|
"toggled": false,
|
||||||
|
"looped": true,
|
||||||
|
"easing": "in_out_back",
|
||||||
|
"motion": "scale_squeeze"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"sequence": {
|
"forms": {
|
||||||
"frames": [0, 1],
|
"idle": ["normal", "breathe"],
|
||||||
"durations": [133, 133]
|
"attack": ["normal", "rushing"],
|
||||||
},
|
"hurt": ["reversed", "meth"]
|
||||||
"transform": {
|
|
||||||
"min_x": -20.0,
|
|
||||||
"min_y": -20.0,
|
|
||||||
"max_x": 20.0,
|
|
||||||
"max_y": 20.0,
|
|
||||||
"simple": false,
|
|
||||||
"flipped": true,
|
|
||||||
"ease_rate": 5.0,
|
|
||||||
"scaled": true,
|
|
||||||
"toggled": false,
|
|
||||||
"looped": true,
|
|
||||||
"easing": "normal_dist",
|
|
||||||
"motion": "move_shake"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,12 @@
|
||||||
#include "animate2.hpp"
|
#include "animate2.hpp"
|
||||||
#include "tools/animator.hpp"
|
#include "tools/animator.hpp"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <ranges>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO:
|
* TODO:
|
||||||
*
|
*
|
||||||
* - Easing functions from strings in json.
|
|
||||||
* - Map of animation forms.
|
|
||||||
* - Bring back shaders.
|
* - Bring back shaders.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -80,8 +80,17 @@ namespace animator {
|
||||||
$anim.play();
|
$anim.play();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case Event::PREV_FORM:
|
||||||
|
change_form(-1);
|
||||||
|
$ui.update_status($anim);
|
||||||
|
break;
|
||||||
|
case Event::NEXT_FORM:
|
||||||
|
change_form(1);
|
||||||
|
$ui.update_status($anim);
|
||||||
|
break;
|
||||||
case Event::RELOAD:
|
case Event::RELOAD:
|
||||||
reload();
|
reload();
|
||||||
|
$ui.update_status($anim);
|
||||||
state(State::START);
|
state(State::START);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
@ -92,6 +101,26 @@ namespace animator {
|
||||||
void FSM::END(Event ev) {
|
void FSM::END(Event ev) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FSM::change_form(int direction) {
|
||||||
|
dbc::check($anim.forms.size() > 0, "you can't use an empty animation.forms idiot.");
|
||||||
|
|
||||||
|
$cur_form_i = std::clamp($cur_form_i + direction, 0, int($anim.forms.size()) - 1);
|
||||||
|
dbc::check($cur_form_i >= 0, "you fucked up, Zed");
|
||||||
|
|
||||||
|
// this is the dumbest shit ever
|
||||||
|
auto key_view = std::views::keys($anim.forms);
|
||||||
|
std::vector<std::string> keys(key_view.begin(), key_view.end());
|
||||||
|
|
||||||
|
dbc::check(size_t($cur_form_i) < keys.size(), "form index outside of form keys vector");
|
||||||
|
|
||||||
|
$cur_form = keys[$cur_form_i];
|
||||||
|
fmt::println("cur_form_i {}; cur_form: {}", $cur_form_i, $cur_form);
|
||||||
|
|
||||||
|
// set_form will stop the animation
|
||||||
|
$anim.set_form($cur_form);
|
||||||
|
$anim.play();
|
||||||
|
}
|
||||||
|
|
||||||
void FSM::run_animation() {
|
void FSM::run_animation() {
|
||||||
if($anim.playing) {
|
if($anim.playing) {
|
||||||
$anim.update();
|
$anim.update();
|
||||||
|
|
@ -110,20 +139,29 @@ namespace animator {
|
||||||
|
|
||||||
void FSM::check_update() {
|
void FSM::check_update() {
|
||||||
if($timer.getElapsedTime().toDuration() > 500ms) {
|
if($timer.getElapsedTime().toDuration() > 500ms) {
|
||||||
auto mod_time = std::filesystem::last_write_time("assets/animate2.json");
|
try {
|
||||||
|
auto mod_time = std::filesystem::last_write_time("assets/animate2.json");
|
||||||
|
|
||||||
if($last_mod_time < mod_time) {
|
if($last_mod_time < mod_time) {
|
||||||
event(Event::RELOAD);
|
event(Event::RELOAD);
|
||||||
|
}
|
||||||
|
|
||||||
|
$timer.restart();
|
||||||
|
} catch(const std::filesystem::filesystem_error& err) {
|
||||||
|
fmt::println("failed to open {}: {}", err.path1().string(), err.what());
|
||||||
|
$timer.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
$timer.restart();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FSM::reload() {
|
void FSM::reload() {
|
||||||
$anim = animate2::load("assets/animate2.json", $anim_name);
|
$anim = animate2::load("assets/animate2.json", $anim_name);
|
||||||
$anim.play();
|
|
||||||
|
// BUG: this will throw the index off after reloads, oh well
|
||||||
|
$anim.set_form($cur_form);
|
||||||
$last_mod_time = std::filesystem::last_write_time("assets/animate2.json");
|
$last_mod_time = std::filesystem::last_write_time("assets/animate2.json");
|
||||||
|
|
||||||
|
$anim.play();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FSM::handle_keyboard_mouse() {
|
void FSM::handle_keyboard_mouse() {
|
||||||
|
|
@ -137,6 +175,10 @@ namespace animator {
|
||||||
case KEY_PRESS:
|
case KEY_PRESS:
|
||||||
if($router.scancode == KEY::Space) {
|
if($router.scancode == KEY::Space) {
|
||||||
event(Event::PLAY_STOP);
|
event(Event::PLAY_STOP);
|
||||||
|
} else if($router.scancode == KEY::Up) {
|
||||||
|
event(Event::PREV_FORM);
|
||||||
|
} else if($router.scancode == KEY::Down) {
|
||||||
|
event(Event::NEXT_FORM);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MOUSE_CLICK:
|
case MOUSE_CLICK:
|
||||||
|
|
@ -155,7 +197,7 @@ namespace animator {
|
||||||
|
|
||||||
void FSM::render() {
|
void FSM::render() {
|
||||||
$window.clear();
|
$window.clear();
|
||||||
$ui.render($window, true);
|
$ui.render($window, false);
|
||||||
$window.display();
|
$window.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -177,13 +219,24 @@ namespace animator {
|
||||||
$ui.set<guecs::Sprite>(viewer, { sprite_name, 0, false});
|
$ui.set<guecs::Sprite>(viewer, { sprite_name, 0, false});
|
||||||
|
|
||||||
$ui.init();
|
$ui.init();
|
||||||
|
|
||||||
|
$overlay.position(0, 0, width/4, height/4);
|
||||||
|
$overlay.layout(
|
||||||
|
"[form]"
|
||||||
|
"[sequence]"
|
||||||
|
"[transform]"
|
||||||
|
"[error]");
|
||||||
|
|
||||||
|
$overlay.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UI::render(sf::RenderWindow& window, bool debug) {
|
void UI::render(sf::RenderWindow& window, bool debug) {
|
||||||
$ui.render(window);
|
$ui.render(window);
|
||||||
|
$overlay.render(window);
|
||||||
|
|
||||||
if(debug) {
|
if(debug) {
|
||||||
$ui.debug_layout(window);
|
$ui.debug_layout(window);
|
||||||
|
$overlay.debug_layout(window);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -191,6 +244,12 @@ namespace animator {
|
||||||
return $ui.mouse(x, y, mods);
|
return $ui.mouse(x, y, mods);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UI::update_status(animate2::Animate2& anim) {
|
||||||
|
$overlay.show_text("form", guecs::to_wstring(anim.form_name));
|
||||||
|
$overlay.show_text("sequence", guecs::to_wstring(anim.sequence_name));
|
||||||
|
$overlay.show_text("transform", guecs::to_wstring(anim.transform_name));
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<sf::Sprite> UI::get_sprite() {
|
std::shared_ptr<sf::Sprite> UI::get_sprite() {
|
||||||
auto viewer = $ui.entity("viewer");
|
auto viewer = $ui.entity("viewer");
|
||||||
return $ui.get<guecs::Sprite>(viewer).sprite;
|
return $ui.get<guecs::Sprite>(viewer).sprite;
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ namespace animator {
|
||||||
enum class Event {
|
enum class Event {
|
||||||
TICK=__LINE__,
|
TICK=__LINE__,
|
||||||
PLAY_STOP=__LINE__,
|
PLAY_STOP=__LINE__,
|
||||||
|
NEXT_FORM=__LINE__,
|
||||||
|
PREV_FORM=__LINE__,
|
||||||
RELOAD=__LINE__,
|
RELOAD=__LINE__,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -23,11 +25,13 @@ namespace animator {
|
||||||
|
|
||||||
struct UI {
|
struct UI {
|
||||||
guecs::UI $ui;
|
guecs::UI $ui;
|
||||||
|
guecs::UI $overlay;
|
||||||
|
|
||||||
void button(const std::string& name, std::function<void(guecs::Modifiers mods)> cb);
|
void button(const std::string& name, std::function<void(guecs::Modifiers mods)> cb);
|
||||||
void init(const std::string& sprite_name, const std::string& background, int width, int height);
|
void init(const std::string& sprite_name, const std::string& background, int width, int height);
|
||||||
void render(sf::RenderWindow& window, bool debug=false);
|
void render(sf::RenderWindow& window, bool debug=false);
|
||||||
bool mouse(float x, float y, guecs::Modifiers mods);
|
bool mouse(float x, float y, guecs::Modifiers mods);
|
||||||
|
void update_status(animate2::Animate2& anim);
|
||||||
std::shared_ptr<sf::Sprite> get_sprite();
|
std::shared_ptr<sf::Sprite> get_sprite();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -44,6 +48,8 @@ namespace animator {
|
||||||
std::string $background="";
|
std::string $background="";
|
||||||
std::filesystem::file_time_type $last_mod_time;
|
std::filesystem::file_time_type $last_mod_time;
|
||||||
sf::Clock $timer;
|
sf::Clock $timer;
|
||||||
|
std::string $cur_form = "idle";
|
||||||
|
int $cur_form_i = 0;
|
||||||
|
|
||||||
void init(const std::string &sprite_name, const std::string& background, const std::string &anim_name);
|
void init(const std::string &sprite_name, const std::string& background, const std::string &anim_name);
|
||||||
void event(Event ev, std::any data={});
|
void event(Event ev, std::any data={});
|
||||||
|
|
@ -58,6 +64,7 @@ namespace animator {
|
||||||
void tick();
|
void tick();
|
||||||
void reload();
|
void reload();
|
||||||
void check_update();
|
void check_update();
|
||||||
|
void change_form(int direction);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue