guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
animation.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/components/animation.hpp"
24
25#include "guillaume/theme.hpp"
26
27#include <algorithm>
28
30{
32 : _duration(getMotionDuration(theme::MotionDuration::Medium2))
33 {
34 }
35
36 float Animation::getDuration(void) const
37 {
38 return _duration;
39 }
40
42 {
43 if (_duration == duration) {
44 return *this;
45 }
46 _duration = duration;
47 setHasChanged(true);
48 return *this;
49 }
50
51 float Animation::getElapsed(void) const
52 {
53 return _elapsed;
54 }
55
57 {
58 if (_elapsed == elapsed) {
59 return *this;
60 }
61 _elapsed = elapsed;
62 setHasChanged(true);
63 return *this;
64 }
65
66 float Animation::getProgress(void) const
67 {
68 if (_duration <= 0.0f) {
69 return _playing ? 0.0f : 1.0f;
70 }
71 return std::clamp(_elapsed / _duration, 0.0f, 1.0f);
72 }
73
74 bool Animation::isPlaying(void) const
75 {
76 return _playing;
77 }
78
80 {
81 _playing = true;
82 _elapsed = 0.0f;
83 setHasChanged(true);
84 return *this;
85 }
86
88 {
89 _playing = false;
90 _elapsed = 0.0f;
91 setHasChanged(true);
92 return *this;
93 }
94
95 bool Animation::isLooping(void) const
96 {
97 return _looping;
98 }
99
101 {
102 if (_looping == looping) {
103 return *this;
104 }
105 _looping = looping;
106 setHasChanged(true);
107 return *this;
108 }
109
110 motion::Easing Animation::getEasing(void) const
111 {
112 return _easing;
113 }
114
115 Animation &Animation::setEasing(motion::Easing easing)
116 {
117 if (_easing == easing) {
118 return *this;
119 }
120 _easing = easing;
121 setHasChanged(true);
122 return *this;
123 }
124
126 {
127 _onSample = handler;
128 return *this;
129 }
130
132 {
133 return _onSample;
134 }
135
137 {
138 _onComplete = handler;
139 return *this;
140 }
141
143 {
144 return _onComplete;
145 }
146
147 void Animation::advance(float deltaTime)
148 {
149 if (!_playing) {
150 return;
151 }
152
153 if (_duration <= 0.0f) {
154 _playing = true;
155 _elapsed = 0.0f;
156 setHasChanged(true);
157 if (_onSample) {
158 _onSample(1.0f);
159 }
160 if (_onComplete) {
161 _onComplete();
162 }
163 _playing = _looping;
164 return;
165 }
166
167 _elapsed += std::max(0.0f, deltaTime);
168 setHasChanged(true);
169
170 if (_elapsed >= _duration) {
171 _elapsed = _duration;
172 if (_onSample) {
173 _onSample(1.0f);
174 }
175 if (_onComplete) {
176 _onComplete();
177 }
178 if (_looping) {
179 _elapsed = 0.0f;
180 _playing = true;
181 } else {
182 _playing = false;
183 }
184 return;
185 }
186
187 if (_onSample) {
188 _onSample(motion::applyEasing(_easing, getProgress()));
189 }
190 }
191
192} // namespace guillaume::components
float getProgress(void) const
Get how far the current run is, before easing.
Definition animation.cpp:66
void advance(float deltaTime)
Move the animation forward in time and call the handlers.
float getElapsed(void) const
Get the time spent in the current run.
Definition animation.cpp:51
bool isLooping(void) const
Check whether the animation starts again when it completes.
Definition animation.cpp:95
CompleteHandler getOnCompleteHandler(void) const
Get the callback run when the animation reaches its duration.
motion::Easing getEasing(void) const
Get the curve that turns the linear progress into the value passed to the sample handler.
Animation & setEasing(motion::Easing easing)
Set the curve that turns the linear progress into the value passed to the sample handler.
std::function< void(void)> CompleteHandler
Called on completion.
Definition animation.hpp:65
Animation & setDuration(float duration)
Set how long one run of the animation lasts.
Definition animation.cpp:41
Animation & setElapsed(float elapsed)
Move the animation to a point in time, without calling any handler.
Definition animation.cpp:56
SampleHandler getOnSampleHandler(void) const
Get the callback that applies the animation.
Animation(void)
Construct a stopped animation lasting the Material 3 Medium2 motion duration of the theme.
Definition animation.cpp:31
Animation & setOnSampleHandler(const SampleHandler &handler)
Store the callback that applies the animation.
Animation & play(void)
Start the animation from the beginning, even when it is already running.
Definition animation.cpp:79
float getDuration(void) const
Get how long one run of the animation lasts.
Definition animation.cpp:36
Animation & setLooping(bool looping)
Set whether the animation starts again when it completes.
Animation & setOnCompleteHandler(const CompleteHandler &handler)
Store the callback run when the animation reaches its duration.
Animation & stop(void)
Stop the animation and bring the elapsed time back to 0.
Definition animation.cpp:87
bool isPlaying(void) const
Check whether the animation is running.
Definition animation.cpp:74
std::function< void(float)> SampleHandler
Receives the eased progress.
Definition animation.hpp:63
void setHasChanged(bool hasChanged)
Raise or lower the changed flag.
Definition component.cpp:32
State. Every class here is plain data attached to an entity.
Design tokens: colors, type scale, shapes, elevation, motion and spacing. Read them through guillaume...
float getMotionDuration(theme::MotionDuration duration)
Get the length the active theme gives to a duration token.