Gamedev Framework (gf)  0.7.0
A C++14 framework for 2D games
Tween.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_TWEEN_H
22 #define GF_TWEEN_H
23 
24 #include <functional>
25 
26 #include "Easings.h"
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
39  template<typename T>
40  class Tween {
41  public:
45  using Setter = std::function<void(const T&)>;
46 
56  Tween(T origin, T target, Setter setter, Time duration, Easing easing = Ease::linear)
57  : m_origin(origin)
58  , m_target(target)
59  , m_setter(setter)
60  , m_elapsed() // 0
61  , m_duration(duration)
62  , m_easing(easing)
63  {
64 
65  }
66 
76  Tween(T origin, T target, T& value, Time duration, Easing easing = Ease::linear)
77  : m_origin(origin)
78  , m_target(target)
79  , m_setter([&value](const T& newValue) { value = newValue; })
80  , m_elapsed() // 0
81  , m_duration(duration)
82  , m_easing(easing)
83  {
84 
85  }
86 
92  void setOrigin(T origin) {
93  m_origin = origin;
94  }
95 
101  constexpr T getOrigin() const noexcept {
102  return m_origin;
103  }
104 
110  void setTarget(T target) {
111  m_target = target;
112  }
113 
119  constexpr T getTarget() const noexcept {
120  return m_target;
121  }
122 
128  void setDuration(Time duration) {
129  m_duration = duration;
130  }
131 
137  constexpr Time getDuration() const noexcept {
138  return m_duration;
139  }
140 
146  T getValue() const {
147  return gf::lerp(m_origin, m_target, m_easing(m_elapsed.asSeconds() / m_duration.asSeconds()));
148  }
149 
155  bool isFinished() const {
156  return m_elapsed >= m_duration;
157  }
158 
164  void update(Time time) {
165  m_elapsed += time;
166 
167  if (m_elapsed >= m_duration) {
168  m_elapsed = m_duration;
169  }
170 
171  m_setter(getValue());
172  }
173 
177  void restart() {
178  m_elapsed = Time::zero();
179  }
180 
181  private:
182  T m_origin;
183  T m_target;
184  Setter m_setter;
185  Time m_elapsed;
186  Time m_duration;
187  Easing m_easing;
188  };
189 
190 #ifndef DOXYGEN_SHOULD_SKIP_THIS
191 }
192 #endif
193 }
194 
195 #endif // GF_TWEEN_H
constexpr T lerp(T lhs, T rhs, U t)
Linear interpolation function.
Definition: Math.h:242
void setOrigin(T origin)
Change the origin of the tween.
Definition: Tween.h:92
Tween(T origin, T target, Setter setter, Time duration, Easing easing=Ease::linear)
Constructor with a setter.
Definition: Tween.h:56
static constexpr Time zero()
Definition: Time.h:173
void restart()
Restart the tween.
Definition: Tween.h:177
bool isFinished() const
Check if the tween is finished.
Definition: Tween.h:155
Represents a time value.
Definition: Time.h:73
void setDuration(Time duration)
Change the duration of the tween.
Definition: Tween.h:128
constexpr T getOrigin() const noexcept
Get the origin of the tween.
Definition: Tween.h:101
void setTarget(T target)
Change the target of the tween.
Definition: Tween.h:110
The namespace for gf classes.
Definition: Action.h:34
static float linear(float t)
Linear easing.
constexpr T getTarget() const noexcept
Get the target of the tween.
Definition: Tween.h:119
constexpr Time getDuration() const noexcept
Get the duration of the tween.
Definition: Tween.h:137
Tween(T origin, T target, T &value, Time duration, Easing easing=Ease::linear)
Constructor with a reference.
Definition: Tween.h:76
float(*)(float) Easing
An easing function.
Definition: Easings.h:48
void update(Time time)
Update the tween.
Definition: Tween.h:164
An interpolation between two values.
Definition: Tween.h:40
std::function< void(const Vector< float, 4 > &)> Setter
A setter function.
Definition: Tween.h:45
T getValue() const
Get the current value of the tween.
Definition: Tween.h:146