persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
UIPropertyAnimation.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include "core/geometry.hpp"
15
16#include <memory>
17#include <type_traits>
18#include <utility>
19
20class UIAnimator;
21
25template <typename T> class UIPropertyAnimation : public UIAnimation
26{
27 public:
28 UIPropertyAnimation(T* tgt, const T& fromVal, const T& toVal, ae::q20_12_t durationSec, UIEase easeMode)
29 : target(tgt), from(fromVal), to(toVal), duration(durationSec), ease(easeMode)
30 {
31 }
32
33 void reverse() override
34 {
35 std::swap(from, to);
36 elapsed = ae::q20_12_t{0};
37 done = false;
38 cancelled = false;
39 }
40
41 void reset() override
42 {
44 }
45
46 protected:
47 bool onUpdate(ae::q20_12_t dt) override
48 {
49 MathManager& math = MathManager::getInstance();
50 elapsed += dt;
51 ae::q20_12_t t = ae::q20_12_t{1};
52 if (duration > ae::q20_12_t{0})
53 {
54 t = math.clamp(math.div(elapsed, duration), ae::q20_12_t{0}, ae::q20_12_t{1});
55 }
56 t = math.ease(ease, t);
57 *target = lerpValue(from, to, t);
58 return elapsed >= duration;
59 }
60
61 private:
62 static T lerpValue(const T& a, const T& b, ae::q20_12_t t)
63 {
64 MathManager& math = MathManager::getInstance();
65 if constexpr (std::is_same_v<T, int>)
66 {
67 return math.lerp(a, b, t);
68 }
69 else if constexpr (std::is_same_v<T, ae::q20_12_t>)
70 {
71 return math.lerp(a, b, t);
72 }
73 else if constexpr (std::is_same_v<T, Point2D<int>>)
74 {
75 return Point2D<int>(math.lerp(a.x, b.x, t), math.lerp(a.z, b.z, t));
76 }
77 else if constexpr (std::is_same_v<T, Point2D<ae::q20_12_t>>)
78 {
79 return Point2D<ae::q20_12_t>(math.lerp(a.x, b.x, t), math.lerp(a.z, b.z, t));
80 }
81 else
82 {
83 static_assert(sizeof(T) == 0, "UIPropertyAnimation: unsupported type");
84 return a;
85 }
86 }
87
90 T to;
91 ae::q20_12_t duration{0};
92 UIEase ease = UIEase::Linear;
93};
94
98template <typename T> class UIPropertyAnimationBuilder
99{
100 public:
101 explicit UIPropertyAnimationBuilder(T& tgt, UIAnimator* animOwner = nullptr) : ptr(&tgt), owner(animOwner)
102 {
103 }
104
106 {
107 fromVal = v;
108 hasFrom = true;
109 return *this;
110 }
111
113 {
114 toVal = v;
115 return *this;
116 }
117
122 {
123 durationMs = ms;
124 return *this;
125 }
126
128 {
129 easeType = e;
130 return *this;
131 }
132
134 {
135 repeatMode = mode;
136 repeatCount = count;
137 return *this;
138 }
139
141 {
142 return repeat(UIRepeatMode::Loop, count);
143 }
144
146 {
147 return repeat(UIRepeatMode::PingPong, count);
148 }
149
150 std::shared_ptr<UIAnimation> build() const
151 {
152 MathManager& math = MathManager::getInstance();
153 const ae::q20_12_t durationSec = math.div(ae::q20_12_t{durationMs}, ae::q20_12_t{1000});
154 T f = hasFrom ? fromVal : *ptr;
155 auto anim = std::make_shared<UIPropertyAnimation<T>>(ptr, f, toVal, durationSec, easeType);
156 anim->setRepeat(repeatMode, repeatCount);
157 return anim;
158 }
159
161
162 private:
163 T* ptr;
164 T fromVal = T{};
165 T toVal = T{};
166 bool hasFrom = false;
167 int durationMs = 200;
168 UIEase easeType = UIEase::Linear;
170 int repeatCount = -1;
171 UIAnimator* owner = nullptr;
172};
Non-copyable wrapper around a value that UI tweens can target.
Weak handle used by callers to cancel/pause/query a running UI animation.
Base class for one UI animation step (property, delay, callback, sequence).
UIRepeatMode
How an animation behaves after reaching its end.
@ Loop
Restart from the beginning.
@ PingPong
Reverse direction each cycle.
@ Once
Play once, then complete.
Easing mode alias for UI property animations.
MathManager::EaseMode UIEase
Easing curves available to UI tweens.
Definition UIEase.hpp:16
ae::q20_12_t clamp(ae::q20_12_t v, ae::q20_12_t lo, ae::q20_12_t hi)
Clamps a Q20.12 value to [lo, hi].
ae::q20_12_t ease(EaseMode mode, ae::q20_12_t t)
Applies an easing curve to a normalized time value.
ae::q20_12_t div(ae::q20_12_t num, ae::q20_12_t den)
Divides two Q20.12 values.
ae::q20_12_t lerp(ae::q20_12_t a, ae::q20_12_t b, ae::q20_12_t t)
Linearly interpolates two Q20.12 values.
Non-owning control handle for a UIAnimation managed by UIAnimator.
ae::q20_12_t elapsed
virtual void reset()
Resets runtime state so the animation can play again (used by Loop).
UIAnimation(const UIAnimation &)=delete
Owns and ticks active UI animations. Safe against cancel-during-tick.
UIPropertyAnimationBuilder & repeat(UIRepeatMode mode, int count=-1)
UIPropertyAnimationBuilder & to(const T &v)
UIAnimationHandle start()
UIPropertyAnimationBuilder & ease(UIEase e)
UIPropertyAnimationBuilder(T &tgt, UIAnimator *animOwner=nullptr)
UIPropertyAnimationBuilder & pingPong(int count=-1)
UIPropertyAnimationBuilder & loop(int count=-1)
UIPropertyAnimationBuilder & duration(int ms)
Duration in milliseconds.
std::shared_ptr< UIAnimation > build() const
UIPropertyAnimationBuilder & from(const T &v)
void reset() override
Resets runtime state so the animation can play again (used by Loop).
bool onUpdate(ae::q20_12_t dt) override
Per-frame update.
static T lerpValue(const T &a, const T &b, ae::q20_12_t t)
UIPropertyAnimation(T *tgt, const T &fromVal, const T &toVal, ae::q20_12_t durationSec, UIEase easeMode)