GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
tween.h
Go to the documentation of this file.
1#pragma once
2#include "ga/easing.h"
3#include "ga/math.h"
4#include "ga/signal.h"
5#include "ga/timer.h"
6
7namespace ga {
8
10{
11public:
12 friend class Timeline;
13 virtual ~TweenBase() = default;
14 void setName( const std::string& name ) { m_name = name; }
15 const std::string& getName() { return m_name; }
16
17protected:
18 std::string m_name = "";
19 virtual bool update_() = 0;
20 virtual bool isDone_() = 0;
21 virtual bool isStarted_() = 0;
22 virtual void end( bool fireCallback ) = 0;
23};
24
25// Tween class
26template <typename T>
27class Tween : public TweenBase, public Timer
28{
29public:
30 Tween( const T& startVal, const T& endVal, std::function<float( float )> easeFn )
31 : Timer()
32 , m_startVal( startVal )
33 , m_endVal( endVal )
34 , m_easeFn( easeFn )
35 , m_onDone( nullptr )
36 , m_boundPtr( nullptr )
37 {
38 }
39 Tween( const T& startVal, const T& endVal, std::function<float( float )> easeFn, std::function<void()> onDone )
40 : Timer()
41 , m_startVal( startVal )
42 , m_endVal( endVal )
43 , m_easeFn( easeFn )
44 , m_onDone( onDone )
45 , m_boundPtr( nullptr )
46 {
47 }
48 Tween() {};
49
50 friend class Timeline;
51
52 Tween& set( const T& startVal, const T& endVal, std::function<float( float )> easeFn, std::function<void()> onDone = nullptr )
53 {
54 return setStartVal( startVal ).setEndVal( endVal ).setEaseFn( easeFn ).setOnDone( onDone );
55 }
56
57 Tween& setStartVal( const T& startVal )
58 {
59 m_startVal = startVal;
60 return *this;
61 }
62 Tween& setEndVal( const T& endVal )
63 {
64 m_endVal = endVal;
65 return *this;
66 }
68 {
69 m_easeFnType = easeFnType;
70 m_easeFn = ga::easeFn( easeFnType );
71 return *this;
72 }
73 Tween& setEaseFn( std::function<float( float )> easeFn )
74 {
77 return *this;
78 }
79 // assign callback on done
80 Tween& setOnDone( std::function<void()> onDone )
81 {
82 m_onDone = onDone;
83 return *this;
84 }
85
86 // bind a ptr to update with the animation value
87 Tween& bind( T* ptr )
88 {
89 m_boundPtr = ptr;
90 return *this;
91 }
93 {
94 m_boundPtr = nullptr;
95 return *this;
96 }
97
98 const T& getStartVal() const { return m_startVal; }
99 const T& getEndVal() const { return m_endVal; }
100
101 const T& getValue() const { return m_val; }
102
103 // ends at preset end time
104 virtual void startNow() override
105 {
108 }
109
110 // recalculates end time from now
111 virtual void startNow( long durationMillis ) override
112 {
114 Timer::startNow( durationMillis );
115 }
116
117 void startAfterDelay( long delayMs, long durationMs )
118 {
119 auto start = ga::Clock::now() + ga::Millis( delayMs );
120 Timer::setStart( start );
121 Timer::setEnd( start + ga::Millis( durationMs ) );
122 }
123
124 // update and return current value (call at frame rate when using bound ptr)
125 const T& update()
126 {
127 if ( Timer::isSet() && Timer::isActive() ) { // current time is between begin and end
130 }
131 if ( Timer::isDone() ) {
132 end();
133 }
134 return m_val;
135 }
136
137 // end animation now (optionally firing callback), return end value
138 const T& endNow( bool fireCallback = true )
139 {
140 end( fireCallback );
141 return m_val;
142 }
143
144protected:
145 T m_val, m_startVal, m_endVal; // val tweens from startVal to endVal
146 std::function<float( float )> m_easeFn; // easing function: p = f(t) - see Ease.h
148 std::function<void()> m_onDone; // callback
151 {
152 auto ptr = m_boundPtr;
153 if ( ptr ) {
154 try {
155 *ptr = m_val;
156 } catch ( std::exception& e ) {
157 // todo: log exception
158 m_boundPtr = nullptr; // clear pointer since it's broken
159 }
160 }
161 return ptr != nullptr;
162 }
163
164 void end( bool fireCallback = true ) override
165 {
166 m_val = m_endVal;
168 Timer::clear();
169 // fire on-done signal
170 if ( fireCallback && m_onDone ) {
171 // callback
172 try {
173 m_onDone();
174 } catch ( std::exception& e ) {
175 // todo: log exception
176 m_onDone = nullptr; // clear callback since it's broken
177 }
178 }
179 }
180
181 bool update_() override
182 {
183 // Timer::update()
184 if ( Timer::isSet() ) {
185 if ( Timer::isActive() ) { // current time is between begin and end
187 } else if ( Timer::isDone() ) {
188 m_val = m_endVal;
189 }
191 }
192 return isSet() && !isDone();
193 }
194
195 bool isDone_() override
196 {
197 return Timer::isDone();
198 }
199
200 bool isStarted_() override
201 {
202 return Timer::isStarted();
203 }
204};
205
206} // namespace ga
Definition: timeline_component.h:11
Definition: timer.h:46
virtual void setEnd(const TimePoint &time)
Definition: timer.cpp:16
virtual bool isActive()
Definition: timer.h:71
virtual bool isDone()
Definition: timer.h:70
virtual void setStart(const TimePoint &time)
Definition: timer.cpp:11
virtual void clear()
Definition: timer.cpp:27
virtual bool isSet()
Definition: timer.h:68
virtual void startNow()
Definition: timer.cpp:33
virtual bool isStarted()
Definition: timer.h:69
double elapsedPercent()
Definition: timer.cpp:61
Definition: tween.h:10
virtual bool isStarted_()=0
virtual bool isDone_()=0
virtual bool update_()=0
void setName(const std::string &name)
Definition: tween.h:14
virtual void end(bool fireCallback)=0
const std::string & getName()
Definition: tween.h:15
virtual ~TweenBase()=default
std::string m_name
Definition: tween.h:18
Definition: tween.h:28
void startAfterDelay(long delayMs, long durationMs)
Definition: tween.h:117
const T & update()
Definition: tween.h:125
Tween & unbind()
Definition: tween.h:92
Tween & setEndVal(const T &endVal)
Definition: tween.h:62
virtual void startNow() override
Definition: tween.h:104
T * m_boundPtr
Definition: tween.h:149
T m_val
Definition: tween.h:145
Tween & setEaseFn(ga::EaseType easeFnType)
Definition: tween.h:67
Tween(const T &startVal, const T &endVal, std::function< float(float)> easeFn)
Definition: tween.h:30
T m_endVal
Definition: tween.h:145
bool isStarted_() override
Definition: tween.h:200
Tween & setOnDone(std::function< void()> onDone)
Definition: tween.h:80
const T & getStartVal() const
Definition: tween.h:98
std::function< void()> m_onDone
Definition: tween.h:148
virtual void startNow(long durationMillis) override
Definition: tween.h:111
Tween()
Definition: tween.h:48
const T & endNow(bool fireCallback=true)
Definition: tween.h:138
bool update_() override
Definition: tween.h:181
const T & getEndVal() const
Definition: tween.h:99
Tween & set(const T &startVal, const T &endVal, std::function< float(float)> easeFn, std::function< void()> onDone=nullptr)
Definition: tween.h:52
ga::EaseType m_easeFnType
Definition: tween.h:147
T m_startVal
Definition: tween.h:145
Tween & setEaseFn(std::function< float(float)> easeFn)
Definition: tween.h:73
bool isDone_() override
Definition: tween.h:195
Tween(const T &startVal, const T &endVal, std::function< float(float)> easeFn, std::function< void()> onDone)
Definition: tween.h:39
Tween & bind(T *ptr)
Definition: tween.h:87
const T & getValue() const
Definition: tween.h:101
bool updateBoundPtr()
Definition: tween.h:150
void end(bool fireCallback=true) override
Definition: tween.h:164
std::function< float(float)> m_easeFn
Definition: tween.h:146
Tween & setStartVal(const T &startVal)
Definition: tween.h:57
Definition: color.h:9
std::function< float(float)> easeFn(const EaseType &type)
Definition: easing.h:102
T interpolate(const T &a, const T &b, float pct, std::function< float(float)> easeFn, bool bClamp=false)
Definition: math.h:263
std::chrono::milliseconds Millis
Definition: timer.h:15
EaseType
Definition: easing.h:87