SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAnimPlayback Class Reference

Manages the playback of an SLAnimation. More...

#include <SLAnimPlayback.h>

Public Member Functions

 SLAnimPlayback (SLAnimation *parent, SLfloat weight=1.0f)
 
void playForward ()
 
void playBackward ()
 
void pause ()
 
void skipToNextKeyframe ()
 
void skipToPrevKeyframe ()
 
void skipToStart ()
 
void skipToEnd ()
 
SLfloat localTime () const
 
SLAnimationparentAnimation ()
 
SLfloat playbackRate () const
 
SLfloat weight () const
 
SLAnimLooping loop () const
 
SLbool enabled () const
 
SLEasingCurve easing () const
 
SLbool changed () const
 
SLbool isPlayingForward () const
 
SLbool isPlayingBackward () const
 
SLbool isPaused () const
 
SLbool isStopped () const
 
void localTime (SLfloat time)
 
void playbackRate (SLfloat pr)
 
void weight (SLfloat weight)
 
void loop (SLAnimLooping lb)
 
void enabled (SLbool val)
 
void easing (SLEasingCurve ec)
 
void changed (SLbool changed)
 
void advanceTime (SLfloat delta)
 
SLfloat calcEasingTime (SLfloat time) const
 Applies the easing time curve to the input time. More...
 
SLfloat calcEasingTimeInv (SLfloat time) const
 

Protected Attributes

SLAnimation_animation
 the animation this plays is referencing More...
 
SLfloat _localTime
 the current local timestamp (eased time) More...
 
SLfloat _weight
 the current weight More...
 
SLfloat _playbackRate
 the current playback speed More...
 
SLshort _playbackDir
 the current playback direction More...
 
SLbool _enabled
 is this animation running More...
 
SLEasingCurve _easing
 easing modifier curve (to customize start and end point easing) More...
 
SLfloat _linearLocalTime
 linear local time used for _easing propert More...
 
SLAnimLooping _loopingBehaviour
 We support different looping behaviours. More...
 
SLbool _gotChanged
 Did this playback change in the last frame. More...
 

Detailed Description

Manages the playback of an SLAnimation.

This class manages the playback state and the local time of an SLAnimation. It manages the way the time advances and how the animation loops. It has all functionality to play, pause, stop, enable, speedup and slowdown a playback. A list of all SLAnimPlayback is hold by the SLAnimManager.

It is possible to have multiple playbacks per animation. If we keep track of which nodes are affected by which SLAnimPlayback we can only manipulate these nodes for the time kept in the SLAnimPlayback. A practical example for this behaviour would be special skeleton instances that only keep track of SLAnimPlayback for their parent SLAnimSkeleton. The skeleton instance can then change its skeletal data based on the states and the actual SLAnimation has to only exist once in memory.

Definition at line 35 of file SLAnimPlayback.h.

Constructor & Destructor Documentation

◆ SLAnimPlayback()

SLAnimPlayback::SLAnimPlayback ( SLAnimation parent,
SLfloat  weight = 1.0f 
)

Constructor

Definition at line 16 of file SLAnimPlayback.cpp.

17  : _animation(parent),
18  _localTime(0.0f),
19  _weight(weight),
20  _playbackRate(1.0f),
21  _playbackDir(1),
22  _enabled(true),
24  _linearLocalTime(0.0f),
26 {
27 }
@ EC_linear
linear easing with constant velocity
Definition: SLEnums.h:181
@ AL_loop
loop
Definition: SLEnums.h:169
SLfloat _weight
the current weight
SLfloat _localTime
the current local timestamp (eased time)
SLEasingCurve _easing
easing modifier curve (to customize start and end point easing)
SLshort _playbackDir
the current playback direction
SLfloat _playbackRate
the current playback speed
SLbool _enabled
is this animation running
SLfloat _linearLocalTime
linear local time used for _easing propert
SLAnimLooping _loopingBehaviour
We support different looping behaviours.
SLAnimation * _animation
the animation this plays is referencing
SLfloat weight() const

Member Function Documentation

◆ advanceTime()

void SLAnimPlayback::advanceTime ( SLfloat  delta)

Advances the time of the playback play based on its different easing parameters.

Definition at line 31 of file SLAnimPlayback.cpp.

32 {
33  if (!_enabled)
34  return;
35 
36  if (delta == 0.0f)
37  return;
38 
39  // preserve time before update
40  SLfloat prevTime = _linearLocalTime;
41 
43 
44  // fix invalid inputs
46  {
47  // wrap around on loop, else just stay on last frame
48  switch (_loopingBehaviour)
49  {
50  case AL_once:
52  _enabled = false;
53  break;
54  case AL_loop:
55  _linearLocalTime = 0.0f;
56  break;
57  case AL_pingPong:
58  case AL_pingPongLoop:
60  _playbackDir *= -1;
61  break;
62  }
63  }
64 
65  // fix negative inputs, playback rate could be negative
66  else if (_linearLocalTime < 0.0f)
67  {
68  while (_linearLocalTime < 0.0f)
70 
71  switch (_loopingBehaviour)
72  {
73  case AL_once:
74  _linearLocalTime = 0.0f;
75  _enabled = false;
76  break;
77  case AL_loop: _linearLocalTime = _animation->lengthSec(); break;
78  case AL_pingPong:
79  _linearLocalTime = 0.0f;
80  _enabled = false;
81  break; // at the moment pingPong stops when reaching 0, if we start with a reverse direction this is illogical
82  case AL_pingPongLoop:
83  _linearLocalTime = 0.0f;
84  _playbackDir *= -1;
85  break;
86  }
87  }
88 
89  // don't go any further, nothing's changed
90  if (Utils::abs(_linearLocalTime - prevTime) < 0.0001f)
91  return;
92 
93  // mark the playback as changed
94  _gotChanged = true;
95 
96  // update the final eased local time
98 }
float SLfloat
Definition: SL.h:173
@ AL_pingPongLoop
loop forward and backwards
Definition: SLEnums.h:171
@ AL_once
play once
Definition: SLEnums.h:168
@ AL_pingPong
play once in two directions
Definition: SLEnums.h:170
SLbool _gotChanged
Did this playback change in the last frame.
SLfloat calcEasingTime(SLfloat time) const
Applies the easing time curve to the input time.
SLfloat lengthSec() const
Definition: SLAnimation.h:72
T abs(T a)
Definition: Utils.h:249

◆ calcEasingTime()

SLfloat SLAnimPlayback::calcEasingTime ( SLfloat  time) const

Applies the easing time curve to the input time.

See also the declaration of the SLEasingCurve enumeration for the different easing curve type that are taken from Qt QAnimation and QEasingCurve class. See http://qt-project.org/doc/qt-4.8/qeasingcurve.html#Type-enum

Definition at line 175 of file SLAnimPlayback.cpp.

176 {
177  SLfloat x = time / _animation->lengthSec();
178  SLfloat y = 0.0f;
179 
180  switch (_easing)
181  {
182  case EC_linear: y = x; break;
183 
184  case EC_inQuad: y = pow(x, 2.0f); break;
185  case EC_outQuad: y = -pow(x - 1.0f, 2.0f) + 1.0f; break;
186  case EC_inOutQuad: y = (x < 0.5f) ? 2.0f * pow(x, 2.0f) : -2.0f * pow(x - 1.0f, 2.0f) + 1.0f; break;
187  case EC_outInQuad: y = (x < 0.5f) ? -2.0f * pow(x - 0.5f, 2.0f) + 0.5f : 2.0f * pow(x - 0.5f, 2.0f) + 0.5f; break;
188 
189  case EC_inCubic: y = pow(x, 3.0f); break;
190  case EC_outCubic: y = pow(x - 1.0f, 3.0f) + 1.0f; break;
191  case EC_inOutCubic: y = (x < 0.5f) ? 4.0f * pow(x, 3.0f) : 4.0f * pow(x - 1.0f, 3.0f) + 1.0f; break;
192  case EC_outInCubic: y = 4.0f * pow(x - 0.5f, 3.0f) + 0.5f; break;
193 
194  case EC_inQuart: y = pow(x, 4.0f); break;
195  case EC_outQuart: y = -pow(x - 1.0f, 4.0f) + 1.0f; break;
196  case EC_inOutQuart: y = (x < 0.5f) ? 8.0f * pow(x, 4.0f) : -8.0f * pow(x - 1.0f, 4.0f) + 1.0f; break;
197  case EC_outInQuart: y = (x < 0.5f) ? -8.0f * pow(x - 0.5f, 4.0f) + 0.5f : 8.0f * pow(x - 0.5f, 4.0f) + 0.5f; break;
198 
199  case EC_inQuint: y = pow(x, 5.0f); break;
200  case EC_outQuint: y = pow(x - 1.0f, 5.0f) + 1.0f; break;
201  case EC_inOutQuint: y = (x < 0.5f) ? 16.0f * pow(x, 5.0f) : 16.0f * pow(x - 1.0f, 5.0f) + 1.0f; break;
202  case EC_outInQuint: y = 16.0f * pow(x - 0.5f, 5.0f) + 0.5f; break;
203 
204  case EC_inSine: y = sin(x * Utils::PI * 0.5f - Utils::PI * 0.5f) + 1.0f; break;
205  case EC_outSine: y = sin(x * Utils::PI * 0.5f); break;
206  case EC_inOutSine: y = 0.5f * sin(x * Utils::PI - Utils::PI * 0.5f) + 0.5f; break;
207  case EC_outInSine: y = (x < 0.5f) ? 0.5f * sin(x * Utils::PI) : 0.5f * sin(x * Utils::PI - Utils::PI) + 1.0f; break;
208  }
209 
210  return y * _animation->lengthSec();
211 }
@ EC_inOutQuart
quartic easing in and then out
Definition: SLEnums.h:192
@ EC_inCubic
cubic in easing in, acceleration from zero velocity
Definition: SLEnums.h:186
@ EC_outQuart
quartic easing out, decelerating to zero velocity
Definition: SLEnums.h:191
@ EC_outInQuint
quintic easing out and then in
Definition: SLEnums.h:197
@ EC_inOutCubic
cubic easing in and then out
Definition: SLEnums.h:188
@ EC_outCubic
cubic easing out, decelerating to zero velocity
Definition: SLEnums.h:187
@ EC_outInSine
sine easing out and then in
Definition: SLEnums.h:201
@ EC_outInQuad
quadratic easing out and then in
Definition: SLEnums.h:185
@ EC_inOutQuad
quadratic easing in and then out
Definition: SLEnums.h:184
@ EC_inOutSine
sine easing in and then out
Definition: SLEnums.h:200
@ EC_outInCubic
cubic easing out and then in
Definition: SLEnums.h:189
@ EC_inSine
sine easing in, acceleration from zero velocity
Definition: SLEnums.h:198
@ EC_inQuart
quartic easing in, acceleration from zero velocity
Definition: SLEnums.h:190
@ EC_outQuint
quintic easing out, decelerating to zero velocity
Definition: SLEnums.h:195
@ EC_outQuad
quadratic easing out, decelerating to zero velocity
Definition: SLEnums.h:183
@ EC_inOutQuint
quintic easing in and then out
Definition: SLEnums.h:196
@ EC_outInQuart
quartic easing out and then in
Definition: SLEnums.h:193
@ EC_inQuad
quadratic easing in, acceleration from zero velocity
Definition: SLEnums.h:182
@ EC_inQuint
quintic easing in, acceleration from zero velocity
Definition: SLEnums.h:194
@ EC_outSine
sine easing out, decelerating to zero velocity
Definition: SLEnums.h:199
static const float PI
Definition: Utils.h:237

◆ calcEasingTimeInv()

SLfloat SLAnimPlayback::calcEasingTimeInv ( SLfloat  time) const

Inverse functions for the above easing curve functions.

Definition at line 215 of file SLAnimPlayback.cpp.

216 {
217  SLfloat x = time / _animation->lengthSec();
218  SLfloat y = 0.0f;
219 
220  switch (_easing)
221  {
222  case EC_linear: y = x; break;
223 
224  case EC_inQuad: y = sqrt(x); break;
225  case EC_outQuad: y = 1.0f - sqrt(1.0f - x); break;
226  case EC_inOutQuad: y = (x < 0.5f) ? sqrt(x) / sqrt(2.0f) : 1.0f - sqrt(1.0f - x) / sqrt(2.0f); break;
227  case EC_outInQuad: y = (x < 0.5f) ? 0.5f - 0.25f * sqrt(4.0f - 8.0f * x) : 0.5f + 0.25f * sqrt(8.0f * x - 4.0f); break;
228 
229  case EC_inCubic: y = pow(x, 1.0f / 3.0f); break;
230  case EC_outCubic: y = 1.0f - pow(1.0f - x, 1.0f / 3.0f); break;
231  case EC_inOutCubic: y = (x < 0.5f) ? pow(x, 1.0f / 3.0f) / pow(4.0f, 1.0f / 3.0f) : 1.0f - pow(1.0f - x, 1.0f / 3.0f) / pow(4.0f, 1.0f / 3.0f); break;
232  case EC_outInCubic: y = (x < 0.5f) ? -pow((0.5f - x) / 4.0f, 1.0f / 3.0f) + 0.5f : pow((x - 0.5f) / 4.0f, 1.0f / 3.0f) + 0.5f; break;
233 
234  case EC_inQuart: y = pow(x, 1.0f / 4.0f); break;
235  case EC_outQuart: y = 1.0f - pow(1.0f - x, 1.0f / 4.0f); break;
236  case EC_inOutQuart: y = (x < 0.5f) ? pow(x, 1.0f / 4.0f) / pow(8.0f, 1.0f / 4.0f) : 1.0f - pow(1.0f - x, 1.0f / 4.0f) / pow(8.0f, 1.0f / 4.0f); break;
237  case EC_outInQuart: y = (x < 0.5f) ? -pow(0.5f - x, 1.0f / 4.0f) / pow(8.0f, 1.0f / 4.0f) + 0.5f : pow(x - 0.5f, 1.0f / 4.0f) / pow(8.0f, 1.0f / 4.0f) + 0.5f; break;
238 
239  case EC_inQuint: y = pow(x, 1.0f / 5.0f); break;
240  case EC_outQuint: y = 1.0f - pow(1.0f - x, 1.0f / 5.0f); break;
241  case EC_inOutQuint: y = (x < 0.5f) ? pow(x, 1.0f / 5.0f) / pow(16.0f, 1.0f / 5.0f) : 1.0f - pow(1.0f - x, 1.0f / 5.0f) / pow(16.0f, 1.0f / 5.0f); break;
242  case EC_outInQuint: y = (x < 0.5f) ? -pow(0.5f - x, 1.0f / 5.0f) / pow(16.0f, 1.0f / 5.0f) + 0.5f : pow(x - 0.5f, 1.0f / 5.0f) / pow(16.0f, 1.0f / 5.0f) + 0.5f; break;
243 
244  case EC_inSine: y = -2.0f * asin(1.0f - x) * Utils::ONEOVERPI + 1.0f; break;
245  case EC_outSine: y = -2.0f * acos(x) * Utils::ONEOVERPI + 1.0f; break;
246  case EC_inOutSine: y = acos(1.0f - 2.0f * x) * Utils::ONEOVERPI; break;
247  case EC_outInSine: y = (x < 0.5f) ? asin(2.0f * x) * Utils::ONEOVERPI : asin(2.0f * (x - 1.0f)) * Utils::ONEOVERPI + 1.0f; break;
248  }
249 
250  return y * _animation->lengthSec();
251 }
static const float ONEOVERPI
Definition: Utils.h:241

◆ changed() [1/2]

SLbool SLAnimPlayback::changed ( ) const
inline

Definition at line 58 of file SLAnimPlayback.h.

58 { return _gotChanged; }

◆ changed() [2/2]

void SLAnimPlayback::changed ( SLbool  changed)
inline

Definition at line 71 of file SLAnimPlayback.h.

71 { _gotChanged = changed; }
SLbool changed() const

◆ easing() [1/2]

SLEasingCurve SLAnimPlayback::easing ( ) const
inline

Definition at line 57 of file SLAnimPlayback.h.

57 { return _easing; }

◆ easing() [2/2]

void SLAnimPlayback::easing ( SLEasingCurve  ec)
inline

Definition at line 70 of file SLAnimPlayback.h.

70 { _easing = ec; }

◆ enabled() [1/2]

SLbool SLAnimPlayback::enabled ( ) const
inline

Definition at line 56 of file SLAnimPlayback.h.

56 { return _enabled; }

◆ enabled() [2/2]

void SLAnimPlayback::enabled ( SLbool  val)
inline

Definition at line 69 of file SLAnimPlayback.h.

69 { _enabled = val; }

◆ isPaused()

SLbool SLAnimPlayback::isPaused ( ) const
inline

Definition at line 61 of file SLAnimPlayback.h.

61 { return _enabled && _playbackDir == 0; }

◆ isPlayingBackward()

SLbool SLAnimPlayback::isPlayingBackward ( ) const
inline

Definition at line 60 of file SLAnimPlayback.h.

60 { return _enabled && _playbackDir == -1; }

◆ isPlayingForward()

SLbool SLAnimPlayback::isPlayingForward ( ) const
inline

Definition at line 59 of file SLAnimPlayback.h.

59 { return _enabled && _playbackDir == 1; }

◆ isStopped()

SLbool SLAnimPlayback::isStopped ( ) const
inline

Definition at line 62 of file SLAnimPlayback.h.

62 { return !_enabled; }

◆ localTime() [1/2]

SLfloat SLAnimPlayback::localTime ( ) const
inline

Definition at line 51 of file SLAnimPlayback.h.

51 { return _localTime; }

◆ localTime() [2/2]

void SLAnimPlayback::localTime ( SLfloat  time)

Setter for the local time parameter. Takes the currently active easing curve into consideration.

Definition at line 158 of file SLAnimPlayback.cpp.

159 {
160  // Set the eased time
161  _localTime = time;
162 
163  // calculate the equivalent linear time from the new eased time
165 
166  // mark changed
167  _gotChanged = true;
168 }
SLfloat calcEasingTimeInv(SLfloat time) const

◆ loop() [1/2]

SLAnimLooping SLAnimPlayback::loop ( ) const
inline

Definition at line 55 of file SLAnimPlayback.h.

55 { return _loopingBehaviour; }

◆ loop() [2/2]

void SLAnimPlayback::loop ( SLAnimLooping  lb)
inline

Definition at line 68 of file SLAnimPlayback.h.

68 { _loopingBehaviour = lb; }

◆ parentAnimation()

SLAnimation* SLAnimPlayback::parentAnimation ( )
inline

Definition at line 52 of file SLAnimPlayback.h.

52 { return _animation; }

◆ pause()

void SLAnimPlayback::pause ( )

Set this playback to be paused.

Definition at line 118 of file SLAnimPlayback.cpp.

119 {
120  // a paused anmation is an enabled animation that has a 0 direction multiplier
121  _enabled = true;
122  _playbackDir = 0;
123 }

◆ playbackRate() [1/2]

SLfloat SLAnimPlayback::playbackRate ( ) const
inline

Definition at line 53 of file SLAnimPlayback.h.

53 { return _playbackRate; }

◆ playbackRate() [2/2]

void SLAnimPlayback::playbackRate ( SLfloat  pr)
inline

Definition at line 66 of file SLAnimPlayback.h.

66 { _playbackRate = pr; }

◆ playBackward()

void SLAnimPlayback::playBackward ( )

Set this playback to be playing backward.

Definition at line 110 of file SLAnimPlayback.cpp.

111 {
112  _enabled = true;
113  _playbackDir = -1;
114 }

◆ playForward()

void SLAnimPlayback::playForward ( )

Set this playback to be playing forward.

Definition at line 102 of file SLAnimPlayback.cpp.

103 {
104  _enabled = true;
105  _playbackDir = 1;
106 }

◆ skipToEnd()

void SLAnimPlayback::skipToEnd ( )

Set the local time of this animation to the end time.

Definition at line 150 of file SLAnimPlayback.cpp.

151 {
153 }
SLfloat localTime() const

◆ skipToNextKeyframe()

void SLAnimPlayback::skipToNextKeyframe ( )

Set the local time of this playback to be on the time of the next keyframe.

Definition at line 127 of file SLAnimPlayback.cpp.

128 {
130  localTime(time);
131 }
SLfloat nextKeyframeTime(SLfloat time)
Definition: SLAnimation.cpp:38

◆ skipToPrevKeyframe()

void SLAnimPlayback::skipToPrevKeyframe ( )

Set the local time of this playback to be on the time of the previous keyframe.

Definition at line 135 of file SLAnimPlayback.cpp.

136 {
138  localTime(time);
139 }
SLfloat prevKeyframeTime(SLfloat time)
Definition: SLAnimation.cpp:57

◆ skipToStart()

void SLAnimPlayback::skipToStart ( )

Set the local time of this playback to the starting time.

Definition at line 143 of file SLAnimPlayback.cpp.

144 {
145  localTime(0.0f);
146 }

◆ weight() [1/2]

SLfloat SLAnimPlayback::weight ( ) const
inline

Definition at line 54 of file SLAnimPlayback.h.

54 { return _weight; }

◆ weight() [2/2]

void SLAnimPlayback::weight ( SLfloat  weight)
inline

Definition at line 67 of file SLAnimPlayback.h.

67 { _weight = weight; }

Member Data Documentation

◆ _animation

SLAnimation* SLAnimPlayback::_animation
protected

the animation this plays is referencing

Definition at line 79 of file SLAnimPlayback.h.

◆ _easing

SLEasingCurve SLAnimPlayback::_easing
protected

easing modifier curve (to customize start and end point easing)

Definition at line 85 of file SLAnimPlayback.h.

◆ _enabled

SLbool SLAnimPlayback::_enabled
protected

is this animation running

Definition at line 84 of file SLAnimPlayback.h.

◆ _gotChanged

SLbool SLAnimPlayback::_gotChanged
protected

Did this playback change in the last frame.

Definition at line 88 of file SLAnimPlayback.h.

◆ _linearLocalTime

SLfloat SLAnimPlayback::_linearLocalTime
protected

linear local time used for _easing propert

Definition at line 86 of file SLAnimPlayback.h.

◆ _localTime

SLfloat SLAnimPlayback::_localTime
protected

the current local timestamp (eased time)

Definition at line 80 of file SLAnimPlayback.h.

◆ _loopingBehaviour

SLAnimLooping SLAnimPlayback::_loopingBehaviour
protected

We support different looping behaviours.

Definition at line 87 of file SLAnimPlayback.h.

◆ _playbackDir

SLshort SLAnimPlayback::_playbackDir
protected

the current playback direction

Definition at line 83 of file SLAnimPlayback.h.

◆ _playbackRate

SLfloat SLAnimPlayback::_playbackRate
protected

the current playback speed

Definition at line 82 of file SLAnimPlayback.h.

◆ _weight

SLfloat SLAnimPlayback::_weight
protected

the current weight

Definition at line 81 of file SLAnimPlayback.h.


The documentation for this class was generated from the following files: