Skip to content

Commit

Permalink
add Animations for Elements, fix texture width
Browse files Browse the repository at this point in the history
  • Loading branch information
vgmoose committed Jan 28, 2024
1 parent 087c5fb commit 064db44
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Animation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "Animation.hpp"
#include "./DrawUtils.hpp"

Animation::Animation(int startTime, int duration, std::function<void(float)> onStep, std::function<void()> 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;
}
15 changes: 15 additions & 0 deletions src/Animation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <functional>

class Animation
{
public:
Animation(int startTime, int duration, std::function<void(float)> onStep, std::function<void()> onFinish);

bool isFinished();
bool step();
int startTime = 0;
int duration = 0;
std::function<void(float)> onStep = NULL;
std::function<void()> onFinish = NULL;
};
32 changes: 32 additions & 0 deletions src/Element.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "RootDisplay.hpp"
#include <algorithm>
#include "Constraint.hpp"
#include "Animation.hpp"
#include <string>

Element::~Element()
Expand Down Expand Up @@ -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<Animation*> 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()
Expand Down Expand Up @@ -272,6 +293,7 @@ void Element::removeAll(bool moveToTrash)
}
elements.clear();
constraints.clear(); // remove all constraints too
animations.clear();
}

Element* Element::child(Element* child)
Expand Down Expand Up @@ -327,6 +349,16 @@ Element* Element::constrain(int flags, int padding)
return this;
}

Element* Element::animate(
int duration,
std::function<void(float)> onStep,
std::function<void()> onFinish
) {
animations.push_back(new Animation(
CST_GetTicks(), duration, onStep, onFinish)
);
}

// Move an element up within its parent
Element* Element::moveToFront() {
if (parent != NULL) {
Expand Down
9 changes: 9 additions & 0 deletions src/Element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "InputEvents.hpp"
#include "colorspaces.hpp"
#include "DrawUtils.hpp"
#include "Animation.hpp"

#include <functional>
#include <vector>
Expand Down Expand Up @@ -153,6 +154,14 @@ class Element
std::vector<Constraint*> constraints;
Element* constrain(int flags, int padding = 0);

// animations that can be added and will tween over time (and remove when finished)
std::vector<Animation*> animations;
Element* animate(
int durationIn,
std::function<void(float)> onStep,
std::function<void()> onFinish
);

Element* moveToFront();
Element* setTouchable(bool touchable);

Expand Down
3 changes: 3 additions & 0 deletions src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,7 @@ void Texture::loadPath(std::string& path, bool forceReload) {
loadFromSurfaceSaveToCache(path, surface);
CST_FreeSurface(surface);
}

width = texW;
height = texH;
}

0 comments on commit 064db44

Please sign in to comment.