GlsAnimation Library  1.0.6
Provides classes and functions to support animating GL Studio objects programmatically or with script files
gls_animation_juggler.h
Go to the documentation of this file.
1 
41 #ifndef INCLUDED_GLS_ANIMATION_JUGGLER_H
42 #define INCLUDED_GLS_ANIMATION_JUGGLER_H
43 
44 #include <limits>
45 #include <memory>
46 #include <vector>
47 
48 #include "gls_keyframe.h"
49 #include "gls_animation.h"
50 #include "gls_animation_observer.h"
51 
52 namespace disti
53 {
54  // Forward declarations
55  class DisplayFrame;
56 
60  {
61  public:
62  typedef stdortr1::shared_ptr<GlsAnimationJuggler> Ptr;
63 
66  {
68  LOOP,
70  };
71 
74 
76  typedef Detail::ID ID;
77 
79  static ID GetInvalidID();
80 
83  GlsAnimationJuggler( Key currentTime = 0 );
84 
87 
103  template<class Animation>
104  ID Schedule(
105  Animation animation,
106  DisplayFrame* displayFrame = 0,
107  LoopBehavior loop = PLAY_ONCE,
108  double playbackSpeed = 1,
109  Key delayKey = 0,
110  Key startKey = Detail::g_nan,
111  Key endKey = Detail::g_nan)
112  {
113  return Schedule( animation, GlsAnimationObserverBase::Ptr(), displayFrame, loop, playbackSpeed, delayKey, startKey, endKey );
114  }
115 
139  template<class Animation, class Observer>
140  ID Schedule(
141  Animation animation,
142  Observer observer,
143  DisplayFrame* displayFrame = 0,
144  LoopBehavior loop = PLAY_ONCE,
145  double playbackSpeed = 1,
146  Key delayKey = 0,
147  Key startKey = Detail::g_nan,
148  Key endKey = Detail::g_nan,
150  {
151  return DoSchedule( GlsAnimation::Ptr( animation ),
152  GlsAnimationObserverBase::Ptr( observer ),
153  displayFrame,
154  loop,
155  playbackSpeed,
156  delayKey,
157  startKey,
158  endKey );
159  }
160 
165  void Advance( Key dt );
166 
171  void SetKey( Key key );
172 
175  void Cancel( ID id );
176 
178  void CancelAllAnimations();
179 
182  Key GetKey( ID id ) const;
183 
184  private:
185 
186  // Types
187  struct AnimationData
188  {
189  GlsAnimation::Ptr animation;
191  Key delayUntilKey;
192  Key startKey; // Used to only play a segment of the animation
193  Key endKey;
194  double speed;
195  LoopBehavior loopBehavior;
196  ID id;
197  int direction; // Indicates whether a ping-ponging animation is going forward or backward
198 
199  AnimationData() : delayUntilKey( Detail::g_nan ), startKey( Detail::g_nan ), endKey( Detail::g_nan ), speed(1), loopBehavior( PLAY_ONCE ), id( GetInvalidID() ), direction( 1 )
200  {
201  CalcStartEndKeys();
202  }
203 
204  AnimationData( const GlsAnimation::Ptr& animation_,
205  const GlsAnimationObserverBase::Ptr& observer_,
206  Key delayUntilKey_,
207  Key startKey_,
208  Key endKey_,
209  double speed_,
210  LoopBehavior loopBehavior_,
211  ID id_ ,
212  int direction_)
213  : animation( animation_ )
214  , observer( observer_ )
215  , delayUntilKey( delayUntilKey_ )
216  , startKey( startKey_ )
217  , endKey( endKey_ )
218  , speed( speed_ )
219  , loopBehavior( loopBehavior_ )
220  , id( id_ )
221  , direction( direction_ )
222  {
223  DistiAssert( speed >= 0.0f );
224  DistiAssert( 1==direction || -1==direction );
225 
226  CalcStartEndKeys();
227  }
228 
229  void CalcStartEndKeys()
230  {
231  if (animation)
232  {
233  // If startKey or endKey are not specified, then use the values from the animation
234  const auto animationMinMax = animation->GetMinMaxKeys();
235  if (Detail::IsNaN( startKey ) || startKey < animationMinMax.first)
236  {
237  startKey = animationMinMax.first;
238  }
239  if (Detail::IsNaN( endKey ) || endKey > animationMinMax.second)
240  {
241  endKey = animationMinMax.second;
242  }
243  }
244  }
245 
246  };
247 
248  static bool HasAnimationEnded( const Key& key, const AnimationData& animationData, const int direction);
249 
250  // Comparator for std::find_if() lookup of AnimationData by ID
251  struct HasID
252  {
253  ID id;
254  explicit HasID( ID id_ ) : id(id_) {}
255  bool operator()( const AnimationData& animationData ) const { return animationData.id == id; }
256  };
257 
258  typedef std::vector<AnimationData> AnimationList;
259  typedef AnimationList::iterator AnimationIter;
260  typedef AnimationList::const_iterator AnimationConstIter;
261 
262  // Data members
263  Key _currentTime;
264  AnimationList _animationList;
265  AnimationList _animationListAdvanceCopy; // A copy for Advance() to work on; member var to avoid frequent memory allocations
266  bool _isAnimationListAdvanceCopyStale;
267  ID _nextID;
268 
269  // Methods
270  // \param notifyCompleted will be set to true to allow the juggler to send the OnCompleted notification for continuous modes
271  std::pair<Key, int> ComputeKeyAndDirection( const AnimationData& animationData, Key dtm, bool& notifyCompleted ) const;
272 
273  // Helper for the template above (needed because GCC's shared_ptr
274  // doesn't support conversion from shared_ptr<Derived> to shared_ptr<Base>).
275  ID DoSchedule(
276  const GlsAnimation::Ptr& animation,
277  const GlsAnimationObserverBase::Ptr& observer,
278  DisplayFrame* displayFrame,
279  LoopBehavior loop,
280  double playbackSpeed,
281  Key delayKey,
282  Key startKey,
283  Key endKey );
284 
285  GlsAnimationJuggler( const GlsAnimationJuggler& ); // = delete
286  void operator=( const GlsAnimationJuggler& ); // = delete
287  };
288 }
289 
290 #endif
void SetKey(Key key)
Definition: gls_animation_juggler.cpp:375
A class used to schedule and manage multiple animations.
Definition: gls_animation_juggler.h:59
void CancelAllAnimations()
Cancels all scheduled animations. No animation complete notifications are sent.
Definition: gls_animation_juggler.cpp:399
Plays the animation from its starting value to its ending value, and then from its ending value to it...
Definition: gls_animation_juggler.h:69
Detail::ID ID
Animation ID used to cancel animations.
Definition: gls_animation_juggler.h:76
Create our own version of meta-function which is not available on all our target platfo...
Definition: gls_animation.h:731
stdortr1::shared_ptr< GlsAnimation > Ptr
Alias for easier reading.
Definition: gls_animation.h:60
The GL Studio animation observer class and helpers used by the juggler.
stdortr1::shared_ptr< GlsAnimationJuggler > Ptr
Alias for easier reading.
Definition: gls_animation_juggler.h:62
~GlsAnimationJuggler()
Destructor.
Definition: gls_animation_juggler.cpp:68
ID Schedule(Animation animation, Observer observer, DisplayFrame *displayFrame=0, LoopBehavior loop=PLAY_ONCE, double playbackSpeed=1, Key delayKey=0, Key startKey=Detail::g_nan, Key endKey=Detail::g_nan, typename Detail::enable_if< !Detail::is_convertible< Observer, DisplayFrame * >::value >::type *=0)
Definition: gls_animation_juggler.h:140
Plays the animation from its starting value to its ending value, and then removes the animation...
Definition: gls_animation_juggler.h:67
void Cancel(ID id)
Definition: gls_animation_juggler.cpp:381
Key GetKey(ID id) const
Definition: gls_animation_juggler.cpp:415
GlsKeyframeCurveBase::Key Key
Alias for easier reading.
Definition: gls_animation_juggler.h:73
The GL Studio keyframe animation classes.
static ID GetInvalidID()
Gets the sentinel value for when scheduling fails.
Definition: gls_animation_juggler.cpp:76
GlsAnimationJuggler(Key currentTime=0)
Definition: gls_animation_juggler.cpp:61
LoopBehavior
Specifies looping behaviors for animations.
Definition: gls_animation_juggler.h:65
Plays the animation from its starting value to its ending value, and then wraps around to the startin...
Definition: gls_animation_juggler.h:68
The GL Studio animation classes.
stdortr1::shared_ptr< GlsAnimationObserverBase > Ptr
Alias for easier reading.
Definition: gls_animation_observer.h:53
Replacement for std::is_convertible, adapted from Loki 0.1.7.
Definition: gls_animation.h:784
ID Schedule(Animation animation, DisplayFrame *displayFrame=0, LoopBehavior loop=PLAY_ONCE, double playbackSpeed=1, Key delayKey=0, Key startKey=Detail::g_nan, Key endKey=Detail::g_nan)
Definition: gls_animation_juggler.h:104
Definition: gls_animation.cpp:64
void Advance(Key dt)
Definition: gls_animation_juggler.cpp:290
double Key
The key type (usually time, but not always)
Definition: gls_keyframe.h:76