From 064db4419839fc0691473c02df8fdb7903ca4907 Mon Sep 17 00:00:00 2001 From: vgmoose Date: Sun, 28 Jan 2024 12:09:09 -0500 Subject: [PATCH] add Animations for Elements, fix texture width --- src/Animation.cpp | 26 ++++++++++++++++++++++++++ src/Animation.hpp | 15 +++++++++++++++ src/Element.cpp | 32 ++++++++++++++++++++++++++++++++ src/Element.hpp | 9 +++++++++ src/Texture.cpp | 3 +++ 5 files changed, 85 insertions(+) create mode 100644 src/Animation.cpp create mode 100644 src/Animation.hpp diff --git a/src/Animation.cpp b/src/Animation.cpp new file mode 100644 index 0000000..26c37cb --- /dev/null +++ b/src/Animation.cpp @@ -0,0 +1,26 @@ +#include "Animation.hpp" +#include "./DrawUtils.hpp" + +Animation::Animation(int startTime, int duration, std::function onStep, std::function onFinish) { + this->startTime = startTime; + this->duration = duration; + this->onStep = onStep; + this->onFinish = onFinish; +} + +bool Animation::isFinished() { + return CST_GetTicks() > startTime + duration; +} + +bool Animation::step() { + if (isFinished()) { + onFinish(); + return true; + } + + if (onStep != NULL) { + float progress = (float)(CST_GetTicks() - startTime) / (float)duration; + onStep(progress); + } + return false; +} \ No newline at end of file diff --git a/src/Animation.hpp b/src/Animation.hpp new file mode 100644 index 0000000..010ccd9 --- /dev/null +++ b/src/Animation.hpp @@ -0,0 +1,15 @@ +#pragma once +#include + +class Animation +{ +public: + Animation(int startTime, int duration, std::function onStep, std::function onFinish); + + bool isFinished(); + bool step(); + int startTime = 0; + int duration = 0; + std::function onStep = NULL; + std::function onFinish = NULL; +}; diff --git a/src/Element.cpp b/src/Element.cpp index 9c1ee50..4f9508d 100644 --- a/src/Element.cpp +++ b/src/Element.cpp @@ -1,6 +1,7 @@ #include "RootDisplay.hpp" #include #include "Constraint.hpp" +#include "Animation.hpp" #include Element::~Element() @@ -118,6 +119,26 @@ void Element::recalcPosition(Element* parent) { { constraint->apply(this); } + + // go through all animations and apply them + // TODO: animations can modify the actual positions, which can mess up constraints + if (animations.size() > 0) { + std::vector toRemove; + for (Animation* animation : animations) + { + // if there are any animations, we need to re-render + needsRedraw = true; + + bool finished = animation->step(); + if (finished) { + toRemove.push_back(animation); + } + } + for (Animation* animation : toRemove) { + animations.erase(std::remove(animations.begin(), animations.end(), animation), animations.end()); + delete animation; + } + } } CST_Rect Element::getBounds() @@ -272,6 +293,7 @@ void Element::removeAll(bool moveToTrash) } elements.clear(); constraints.clear(); // remove all constraints too + animations.clear(); } Element* Element::child(Element* child) @@ -327,6 +349,16 @@ Element* Element::constrain(int flags, int padding) return this; } +Element* Element::animate( + int duration, + std::function onStep, + std::function onFinish +) { + animations.push_back(new Animation( + CST_GetTicks(), duration, onStep, onFinish) + ); +} + // Move an element up within its parent Element* Element::moveToFront() { if (parent != NULL) { diff --git a/src/Element.hpp b/src/Element.hpp index 8215a17..5f81c99 100644 --- a/src/Element.hpp +++ b/src/Element.hpp @@ -3,6 +3,7 @@ #include "InputEvents.hpp" #include "colorspaces.hpp" #include "DrawUtils.hpp" +#include "Animation.hpp" #include #include @@ -153,6 +154,14 @@ class Element std::vector constraints; Element* constrain(int flags, int padding = 0); + // animations that can be added and will tween over time (and remove when finished) + std::vector animations; + Element* animate( + int durationIn, + std::function onStep, + std::function onFinish + ); + Element* moveToFront(); Element* setTouchable(bool touchable); diff --git a/src/Texture.cpp b/src/Texture.cpp index 82ded90..aad561f 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -239,4 +239,7 @@ void Texture::loadPath(std::string& path, bool forceReload) { loadFromSurfaceSaveToCache(path, surface); CST_FreeSurface(surface); } + + width = texW; + height = texH; } \ No newline at end of file