Fully implement Tween class

This commit is contained in:
2025-02-10 16:41:56 -05:00
parent 030da85166
commit 6e58e30022
2 changed files with 83 additions and 14 deletions

View File

@@ -6,6 +6,9 @@
namespace JUI
{
using EasingFunc = std::function<float(float)>;
using TweenTickFunc = std::function<void(float, float)>;
namespace EasingFunctions
{
@@ -76,39 +79,58 @@ namespace JUI
float EaseInOutBounce(float t);
}
struct TweenInfo {
float time = 1.f;
float delay = 0.f;
int repeats = 0;
bool reverse = false;
EasingFunc easing = &EasingFunctions::EaseInOutLinear;
};
/// A class that represents an animation-in-action.
class Tween {
public:
Tween(TweenTickFunc tick_func) {
this->tick_func = tick_func;
}
Tween(TweenTickFunc tick_func, TweenInfo info) {
this->tick_func = tick_func;
this->time = info.time;
this->delay_time = info.delay;
this->repeat_count = info.repeats;
this->reverses = info.reverse;
this->easing_func = info.easing;
}
Event<> Completed;
void Update(float elapsed)
{
void Update(float elapsed);
}
std::function<float(float)> easing_func = &EasingFunctions::EaseInOutLinear;
std::function<void(float, float)> tick_func;
std::function<float(float)> easing_func;
std::function<void(float)> tick_func;
/// Duration of the tween, in seconds.
float time;
float time = 5;
/// Time of delay until the tween begins, in seconds.
float delay_time;
float delay_time = 0;
/// Number of times the tween repeats. -1 indicates indefinite repetition.
int repeat_count;
int repeat_count = 0; // TODO: Implement
/// Whether or not the tween interpolates in reverse once the initial tween completes.
bool reverses;
bool reverses = false; // TODO: Implement
float lifetime = 5;
float progress = 0;
bool alive = true;
bool paused = false;
bool completed = false;
void Cancel();
void ForceFinish();
void Pause() { paused = true; }
void Resume() { paused = false; }
void Start();
bool Paused() const { return paused; }
bool HasCompleted() const { return completed; }
bool Paused() const;
bool HasCompleted() const;
};
}

View File

@@ -148,4 +148,51 @@ namespace JUI
}
float EasingFunctions::EaseInOutLinear(float t) { return t;}
void Tween::Update(float elapsed) {
if (time <= 0)
{
// TODO: Error in this case.
completed = true;
}
if (delay_time > 0)
{
delay_time -= elapsed;
return;
}
progress += (elapsed / time);
if (progress >= 1.f)
{
progress = 1.f;
if (!completed)
{
completed = true;
Completed.Invoke();
}
return;
}
float modified_progress = easing_func(progress);
tick_func(elapsed, modified_progress);
}
void Tween::ForceFinish() {
progress = 1.f;
}
void Tween::Cancel() {
completed = true;
}
bool Tween::Paused() const { return paused; }
bool Tween::HasCompleted() const { return completed; }
void Tween::Start() { progress = 0; Resume(); }
}