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
|
|
@ -9,12 +9,12 @@
|
|||
#include "animate2.hpp"
|
||||
#include "tools/animator.hpp"
|
||||
#include <unistd.h>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
*
|
||||
* - Easing functions from strings in json.
|
||||
* - Map of animation forms.
|
||||
* - Bring back shaders.
|
||||
*/
|
||||
|
||||
|
|
@ -80,8 +80,17 @@ namespace animator {
|
|||
$anim.play();
|
||||
}
|
||||
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:
|
||||
reload();
|
||||
$ui.update_status($anim);
|
||||
state(State::START);
|
||||
break;
|
||||
default:
|
||||
|
|
@ -92,6 +101,26 @@ namespace animator {
|
|||
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() {
|
||||
if($anim.playing) {
|
||||
$anim.update();
|
||||
|
|
@ -110,20 +139,29 @@ namespace animator {
|
|||
|
||||
void FSM::check_update() {
|
||||
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) {
|
||||
event(Event::RELOAD);
|
||||
if($last_mod_time < mod_time) {
|
||||
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() {
|
||||
$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");
|
||||
|
||||
$anim.play();
|
||||
}
|
||||
|
||||
void FSM::handle_keyboard_mouse() {
|
||||
|
|
@ -137,6 +175,10 @@ namespace animator {
|
|||
case KEY_PRESS:
|
||||
if($router.scancode == KEY::Space) {
|
||||
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;
|
||||
case MOUSE_CLICK:
|
||||
|
|
@ -155,7 +197,7 @@ namespace animator {
|
|||
|
||||
void FSM::render() {
|
||||
$window.clear();
|
||||
$ui.render($window, true);
|
||||
$ui.render($window, false);
|
||||
$window.display();
|
||||
}
|
||||
|
||||
|
|
@ -177,13 +219,24 @@ namespace animator {
|
|||
$ui.set<guecs::Sprite>(viewer, { sprite_name, 0, false});
|
||||
|
||||
$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) {
|
||||
$ui.render(window);
|
||||
$overlay.render(window);
|
||||
|
||||
if(debug) {
|
||||
$ui.debug_layout(window);
|
||||
$overlay.debug_layout(window);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -191,6 +244,12 @@ namespace animator {
|
|||
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() {
|
||||
auto viewer = $ui.entity("viewer");
|
||||
return $ui.get<guecs::Sprite>(viewer).sprite;
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ namespace animator {
|
|||
enum class Event {
|
||||
TICK=__LINE__,
|
||||
PLAY_STOP=__LINE__,
|
||||
NEXT_FORM=__LINE__,
|
||||
PREV_FORM=__LINE__,
|
||||
RELOAD=__LINE__,
|
||||
};
|
||||
|
||||
|
|
@ -23,11 +25,13 @@ namespace animator {
|
|||
|
||||
struct UI {
|
||||
guecs::UI $ui;
|
||||
guecs::UI $overlay;
|
||||
|
||||
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 render(sf::RenderWindow& window, bool debug=false);
|
||||
bool mouse(float x, float y, guecs::Modifiers mods);
|
||||
void update_status(animate2::Animate2& anim);
|
||||
std::shared_ptr<sf::Sprite> get_sprite();
|
||||
};
|
||||
|
||||
|
|
@ -44,6 +48,8 @@ namespace animator {
|
|||
std::string $background="";
|
||||
std::filesystem::file_time_type $last_mod_time;
|
||||
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 event(Event ev, std::any data={});
|
||||
|
|
@ -58,6 +64,7 @@ namespace animator {
|
|||
void tick();
|
||||
void reload();
|
||||
void check_update();
|
||||
void change_form(int direction);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue