Animation tool now lets you cycle through different sequence/transform 'forms' and shows you which one you're viewing.

This commit is contained in:
Zed A. Shaw 2026-02-04 15:10:07 -05:00
parent 07b2102f59
commit 4356b1535e
5 changed files with 170 additions and 56 deletions

View file

@ -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;