ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
AnimPrimitives.hpp
Go to the documentation of this file.
1#pragma once
2
23
24#include "Common.hpp"
25
26#include "CoreTypes.hpp"
27#include "Style.hpp"
28
29namespace ZenitUI
30{
34
35 class Layout;
36
50 class UIAnimation
51 {
52 public:
54 struct TrackBase
55 {
56 virtual ~TrackBase() = default;
58 virtual void apply(float raw_t, Layout *target) = 0;
59 };
60
62 template <typename T>
63 struct Track : public TrackBase
64 {
65 T start_val, end_val;
66 std::function<void(Layout *, T)> setter;
67 TransitionFunction transition;
68
69 Track(T s, T e, std::function<void(Layout *, T)> setter, TransitionFunction tr)
70 : start_val(s), end_val(e), setter(std::move(setter)), transition(tr) {}
71
72 void apply(float raw_t, Layout *target) override
73 {
74 float ratio = getRatio(raw_t, transition);
75 setter(target, lerpProp(start_val, end_val, ratio));
76 }
77 };
78
79 UIAnimation(float duration = 1.0f, float delay = 0.0f)
80 : duration(duration), delay(delay) {}
81
83 template <typename T, typename Callable>
84 void addTrack(T start, T end, Callable setter, TransitionFunction tr = TransitionFunction::Linear)
85 {
86 tracks.push_back(std::make_unique<Track<T>>(start, end, std::function<void(Layout *, T)>(setter), tr));
87 }
88
89 float duration;
90 float delay{0.0f};
91 bool blocksInput{true};
92 std::vector<std::unique_ptr<TrackBase>> tracks;
93 std::function<void()> onFinished;
94 };
95
97 struct AnimState
98 {
99 std::shared_ptr<UIAnimation> anim;
100 float elapsed{0.0f};
101 float delayElapsed{0.0f};
102 bool playing{false};
103 bool reverse{false};
104 };
105
107 struct Keyframe
108 {
109 float t{0.0f};
110 Style delta;
111 };
112
115 {
116 std::string name;
117 std::vector<Keyframe> keyframes;
118 };
119
122 {
123 std::string name;
124 float elapsed{0.0f};
125 double startTime{0.0};
126 float duration{1.0f};
127 float delay{0.0f};
128 int iterations{1};
129 bool alternate{false};
130 bool fillForwards{false};
131 bool finished{false};
132 TransitionFunction ease{TransitionFunction::Linear};
133
134 mutable float cachedSampleElapsed{-1.0f};
135 mutable float cachedLocalT{0.0f};
136 mutable bool cachedFinished{false};
137 };
138
150 inline float sampleActive(const ActiveCssAnimation &a, bool &finished)
151 {
152 finished = false;
153 float t = a.elapsed - a.delay;
154 if (t < 0.0f)
155 return 0.0f;
156 if (a.duration <= 0.0f)
157 {
158 finished = true;
159 return 1.0f;
160 }
161
162 float total = (a.iterations < 0) ? -1.0f : (a.duration * (float)a.iterations);
163 if (a.iterations >= 0 && t >= total)
164 {
165 t = total;
166 finished = true;
167 }
168 float iter = std::floor(t / a.duration);
169
170 if (a.iterations > 0 && finished && iter > 0.0f)
171 {
172 iter = std::min(iter, (float)(a.iterations - 1));
173 }
174
175 float local = (t - iter * a.duration) / a.duration;
176 if (a.alternate && ((int)iter % 2 == 1))
177 local = 1.0f - local;
178 return std::clamp(local, 0.0f, 1.0f);
179 }
180
184 inline float sampleActiveCached(const ActiveCssAnimation &a, bool &finished)
185 {
186 if (a.cachedSampleElapsed == a.elapsed)
187 {
188 finished = a.cachedFinished;
189 return a.cachedLocalT;
190 }
191
192 a.cachedLocalT = sampleActive(a, finished);
193 a.cachedFinished = finished;
194 a.cachedSampleElapsed = a.elapsed;
195 return a.cachedLocalT;
196 }
197
206 inline Style evaluateKeyframes(const KeyframeAnimation &anim, float localT, TransitionFunction ease)
207 {
208 Style out;
209 if (anim.keyframes.empty())
210 return out;
211 if (anim.keyframes.size() == 1)
212 return anim.keyframes[0].delta;
213
214 const Keyframe *k0 = &anim.keyframes.front();
215 const Keyframe *k1 = &anim.keyframes.back();
216 for (size_t i = 0; i + 1 < anim.keyframes.size(); ++i)
217 {
218 if (localT >= anim.keyframes[i].t && localT <= anim.keyframes[i + 1].t)
219 {
220 k0 = &anim.keyframes[i];
221 k1 = &anim.keyframes[i + 1];
222 break;
223 }
224 }
225
226 float span = k1->t - k0->t;
227 float u = (span > 0.0f) ? (localT - k0->t) / span : 0.0f;
228 u = getRatio(std::clamp(u, 0.0f, 1.0f), ease);
229
230#define X(T, name, def) \
231 if (k0->delta.name.is_set && k1->delta.name.is_set) \
232 out.name = lerpProp(k0->delta.name.value, k1->delta.name.value, u); \
233 else if (k0->delta.name.is_set) \
234 out.name = k0->delta.name.value; \
235 else if (k1->delta.name.is_set) \
236 out.name = k1->delta.name.value;
237 BUBBLE_STYLE_PROPS(X)
238#undef X
239
240 return out;
241 }
242
243} // namespace ZenitUI
Centralized standard library includes used throughout ZenitUI.
Small value types shared by the layout, rendering and input layers.
The Style / ComputedStyle pair and all style-related value types.
The base node type: tree, style, layout, input, scroll, render.
Definition Layout.hpp:154
A time-parameterised sequence of per-property tracks.
Definition AnimPrimitives.hpp:51
void addTrack(T start, T end, Callable setter, TransitionFunction tr=TransitionFunction::Linear)
Append a typed track.
Definition AnimPrimitives.hpp:84
float sampleActive(const ActiveCssAnimation &a, bool &finished)
Sample the given CSS animation's local time without touching the cache.
Definition AnimPrimitives.hpp:150
Style evaluateKeyframes(const KeyframeAnimation &anim, float localT, TransitionFunction ease)
Interpolate a KeyframeAnimation at local time localT.
Definition AnimPrimitives.hpp:206
float sampleActiveCached(const ActiveCssAnimation &a, bool &finished)
Cached wrapper around sampleActive.
Definition AnimPrimitives.hpp:184
TransitionFunction
Timing function applied between two keyframe values.
Definition Easing.hpp:28
float getRatio(float t, TransitionFunction fn)
Map a normalized time t in [0, 1] through the given easing.
Definition Easing.hpp:49
Playback state for one CSS animation on one node.
Definition AnimPrimitives.hpp:122
Playback state for one UIAnimation on one node.
Definition AnimPrimitives.hpp:98
A named list of keyframes, keyed by name in Theme::keyframes.
Definition AnimPrimitives.hpp:115
One keyframe of a CSS animation: a normalized time and a sparse style delta.
Definition AnimPrimitives.hpp:108
Sparse style: every property is Opt<T>, unset by default.
Definition Style.hpp:932
Type-erased track interface.
Definition AnimPrimitives.hpp:55
virtual void apply(float raw_t, Layout *target)=0
Advance the track to normalized time raw_t.
Concrete typed track: start/end pair plus a setter.
Definition AnimPrimitives.hpp:64
void apply(float raw_t, Layout *target) override
Advance the track to normalized time raw_t.
Definition AnimPrimitives.hpp:72