Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Timeline.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2011, The Cinder Project, All rights reserved.
3  This code is intended for use with the Cinder C++ library: http://libcinder.org
4 
5  Based on the sc-Choreograph CinderBlock by David Wicks: http://sansumbrella.com/
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
8  the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright notice, this list of conditions and
11  the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13  the following disclaimer in the documentation and/or other materials provided with the distribution.
14 
15  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22  POSSIBILITY OF SUCH DAMAGE.
23 */
24 
25 #pragma once
26 
27 #include "cinder/Cinder.h"
28 #include "cinder/TimelineItem.h"
29 #include "cinder/Easing.h"
30 #include "cinder/Tween.h"
31 #include "cinder/Function.h"
32 
33 #include <vector>
34 #include <list>
35 #include <map>
36 
37 namespace cinder {
38 
39 typedef std::shared_ptr<class Cue> CueRef;
40 typedef std::shared_ptr<class Timeline> TimelineRef;
41 
42 class Timeline : public TimelineItem {
43  public:
45  static TimelineRef create() { TimelineRef result( new Timeline() ); result->setInfinite( true ); return result; }
46 
48  void step( float timestep );
50  void stepTo( float absoluteTime );
51 
53  float getCurrentTime() const { return mCurrentTime; }
54 
56  template<typename T>
57  typename Tween<T>::Options apply( Anim<T> *target, T endValue, float duration, EaseFn easeFunction = easeNone, typename Tween<T>::LerpFn lerpFunction = &tweenLerp<T> )
58  {
59  target->setParentTimeline( thisRef() );
60  return applyPtr( target->ptr(), endValue, duration, easeFunction, lerpFunction );
61  }
62 
64  template<typename T>
65  typename Tween<T>::Options apply( Anim<T> *target, T startValue, T endValue, float duration, EaseFn easeFunction = easeNone, typename Tween<T>::LerpFn lerpFunction = &tweenLerp<T> )
66  {
67  target->setParentTimeline( thisRef() );
68  return applyPtr( target->ptr(), startValue, endValue, duration, easeFunction, lerpFunction );
69  }
70 
72  template<typename T>
73  typename Tween<T>::Options appendTo( Anim<T> *target, T endValue, float duration, EaseFn easeFunction = easeNone, typename Tween<T>::LerpFn lerpFunction = &tweenLerp<T> )
74  {
75  target->setParentTimeline( thisRef() );
76  return appendToPtr( target->ptr(), endValue, duration, easeFunction, lerpFunction );
77  }
78 
80  template<typename T>
81  typename Tween<T>::Options appendTo( Anim<T> *target, T startValue, T endValue, float duration, EaseFn easeFunction = easeNone, typename Tween<T>::LerpFn lerpFunction = &tweenLerp<T> )
82  {
83  target->setParentTimeline( thisRef() );
84  return appendToPtr( target->ptr(), startValue, endValue, duration, easeFunction, lerpFunction );
85  }
86 
88  template<typename T>
89  typename Tween<T>::Options applyPtr( T *target, T endValue, float duration, EaseFn easeFunction = easeNone, typename Tween<T>::LerpFn lerpFunction = &tweenLerp<T> )
90  {
91  TweenRef<T> newTween( new Tween<T>( target, endValue, mCurrentTime, duration, easeFunction, lerpFunction ) );
92  newTween->setAutoRemove( mDefaultAutoRemove );
93  apply( newTween );
94  return typename Tween<T>::Options( newTween, thisRef() );
95  }
96 
98  template<typename T>
99  typename Tween<T>::Options applyPtr( T *target, T startValue, T endValue, float duration, EaseFn easeFunction = easeNone, typename Tween<T>::LerpFn lerpFunction = &tweenLerp<T> )
100  {
101  TweenRef<T> newTween( new Tween<T>( target, startValue, endValue, mCurrentTime, duration, easeFunction, lerpFunction ) );
102  newTween->setAutoRemove( mDefaultAutoRemove );
103  apply( newTween );
104  return typename Tween<T>::Options( newTween, thisRef() );
105  }
106 
108  template<typename T>
109  typename Tween<T>::Options appendToPtr( T *target, T endValue, float duration, EaseFn easeFunction = easeNone, typename Tween<T>::LerpFn lerpFunction = &tweenLerp<T> )
110  {
111  float startTime = findEndTimeOf( target );
112  TweenRef<T> newTween( new Tween<T>( target, endValue, std::max( mCurrentTime, startTime ), duration, easeFunction, lerpFunction ) );
113  newTween->setAutoRemove( mDefaultAutoRemove );
114  insert( newTween );
115  return typename Tween<T>::Options( newTween, thisRef() );
116  }
117 
119  template<typename T>
120  typename Tween<T>::Options appendToPtr( T *target, T startValue, T endValue, float duration, EaseFn easeFunction = easeNone, typename Tween<T>::LerpFn lerpFunction = &tweenLerp<T> )
121  {
122  float startTime = findEndTimeOf( target );
123  TweenRef<T> newTween( new Tween<T>( target, startValue, endValue, std::max( mCurrentTime, startTime ), duration, easeFunction, lerpFunction ) );
124  newTween->setAutoRemove( mDefaultAutoRemove );
125  insert( newTween );
126  return typename Tween<T>::Options( newTween, thisRef() );
127  }
128 
130  CueRef add( const std::function<void ()> &action, float atTime );
131 
132  template<typename T>
133  FnTweenRef<T> applyFn( const std::function<void (T)> &fn, T startValue, T endValue, float duration, const EaseFn &easeFunction = easeNone, const typename Tween<T>::LerpFn &lerpFunction = &tweenLerp<T> )
134  {
135  FnTweenRef<T> newTween( new FnTween<T>( fn, startValue, endValue, mCurrentTime, duration, easeFunction, lerpFunction ) );
136  newTween->setAutoRemove( mDefaultAutoRemove );
137  apply( newTween );
138  return newTween;
139  }
140 
142  void appendPingPong();
143 
145  void apply( TimelineItemRef item );
146 
148  void add( TimelineItemRef item );
150  void insert( TimelineItemRef item );
152  void insert( TimelineItemRef item, float atTime ) { item->mStartTime = atTime; insert( item ); }
153 
155  size_t getNumItems() const { return mItems.size(); }
157  bool empty() const { return mItems.empty(); }
159  TimelineItemRef find( void *target ) const;
161  TimelineItemRef findLast( void *target ) const;
163  TimelineItemRef findLastEnd( void *target ) const;
165  float findEndTimeOf( void *target, bool *found = NULL ) const;
167  void remove( TimelineItemRef item );
169  void removeTarget( void *target );
171  void cloneAndReplaceTarget( void *target, void *replacementTarget );
173  void replaceTarget( void *target, void *replacementTarget );
174 
176  void clear();
178  void reset( bool unsetStarted = false );
179 
181  void setDefaultAutoRemove( bool defaultAutoRemove ) { mDefaultAutoRemove = defaultAutoRemove; }
183  bool getDefaultAutoRemove() const { return mDefaultAutoRemove; }
184 
186  void itemTimeChanged( TimelineItem *item );
187 
189  {
190  TimelineItemRef thisTimelineItem = TimelineItem::thisRef();
191  TimelineRef result = std::static_pointer_cast<Timeline>( thisTimelineItem );
192  return result;
193  }
194 
196  virtual void stepTo( float absoluteTime, bool reverse ) { stepTo( absoluteTime ); }
198 
199  protected:
200  Timeline();
201 
202  virtual void reverse();
203  virtual TimelineItemRef cloneReverse() const;
204  virtual TimelineItemRef clone() const;
205  virtual void start( bool reverse ) {} // no-op
206  virtual void loopStart();
207  virtual void update( float absTime );
208  virtual void complete( bool reverse ) {} // no-op
209 
210  void eraseMarked();
211  virtual float calcDuration() const;
212 
215 
216  std::multimap<void*,TimelineItemRef> mItems;
217 
218  private:
219  Timeline( const Timeline &rhs ); // private to prevent copying; use clone() method instead
220  Timeline& operator=( const Timeline &rhs ); // not defined to prevent copying
221 };
222 
223 class Cue : public TimelineItem {
224  public:
225  Cue( const std::function<void ()> &fn, float atTime = 0 );
226 
227  CueRef create( const std::function<void ()> &fn, float atTime = 0 ) { return CueRef( new Cue( fn, atTime ) ); }
228 
229  void setFn( const std::function<void ()> &fn ) { mFunction = fn; }
230  std::function<void ()> getFn() const { return mFunction; }
231 
232  protected:
233  virtual void reverse() { /* no-op */ }
234  virtual TimelineItemRef cloneReverse() const;
235  virtual TimelineItemRef clone() const;
236 
237  virtual void start( bool reverse ) {} // starting is a no-op for Cues
238  virtual void loopStart();
239  virtual void update( float relativeTime ) {} // update is a no-op for Cues
240  virtual void complete( bool reverse ) {} // completion is a no-op for Cues
241  virtual bool updateAtLoopStart() { return true; }
242 
243  std::function<void ()> mFunction;
244 };
245 
246 } // namespace cinder
Timeline()
Definition: Timeline.cpp:38
std::function< T(const T &, const T &, float)> LerpFn
Definition: Tween.h:157
static TimelineRef create()
Creates a new timeline, defaulted to infinite.
Definition: Timeline.h:45
void insert(TimelineItemRef item)
adds an item to the timeline. Its start time is not modified. Safe to use from callback fn's...
Definition: Timeline.cpp:124
TimelineRef thisRef()
Definition: Timeline.h:188
TimelineItemRef thisRef()
Definition: TimelineItem.h:104
TimelineItemRef find(void *target) const
Returns the first item in the timeline the target of which matches target.
Definition: Timeline.cpp:159
Definition: Tween.h:42
virtual float calcDuration() const
Definition: Timeline.cpp:149
virtual TimelineItemRef cloneReverse() const
Creates a cloned item which runs in reverse relative to a timeline of duration timelineDuration.
Definition: Timeline.cpp:314
float getCurrentTime() const
Returns the timeline's most recent current time.
Definition: Timeline.h:53
Definition: Tween.h:188
int int * max
Definition: GLee.h:17208
void appendPingPong()
Appends to the end of the timeline mirror images of all items.
Definition: Timeline.cpp:91
Cue(const std::function< void()> &fn, float atTime=0)
Definition: Timeline.cpp:337
bool mDefaultAutoRemove
Definition: Timeline.h:213
virtual void reverse()
Definition: Timeline.h:233
virtual void update(float absTime)
Definition: Timeline.cpp:324
void step(float timestep)
Advances time a specified amount and evaluates items.
Definition: Timeline.cpp:52
Definition: Tween.h:141
Tween< T >::Options apply(Anim< T > *target, T startValue, T endValue, float duration, EaseFn easeFunction=easeNone, typename Tween< T >::LerpFn lerpFunction=&tweenLerp< T >)
Replaces any existing tweens on the target with a new tween at the timeline's current time...
Definition: Timeline.h:65
TimelineItemRef findLastEnd(void *target) const
Returns the latest-end item in the timeline the target of which matches target.
Definition: Timeline.cpp:186
void reset(bool unsetStarted=false)
Sets the time to zero, marks all tweens as not completed, and if unsetStarted, marks the tweens as no...
Definition: Timeline.cpp:289
std::function< void()> mFunction
Definition: Timeline.h:243
std::shared_ptr< class Timeline > TimelineRef
Definition: Timeline.h:40
void insert(TimelineItemRef item, float atTime)
adds an item to the timeline, setting its startTime to be at atTime. Safe to use from callback fn's...
Definition: Timeline.h:152
Tween< T >::Options apply(Anim< T > *target, T endValue, float duration, EaseFn easeFunction=easeNone, typename Tween< T >::LerpFn lerpFunction=&tweenLerp< T >)
Replaces any existing tweens on the target with a new tween at the timeline's current time...
Definition: Timeline.h:57
Tween< T >::Options appendToPtr(T *target, T startValue, T endValue, float duration, EaseFn easeFunction=easeNone, typename Tween< T >::LerpFn lerpFunction=&tweenLerp< T >)
Creates a new tween and adds it to the end of the last tween on target, or if no existing tween match...
Definition: Timeline.h:120
GLenum target
Definition: GLee.h:13607
void clear()
Remove all tweens from the Timeline. Do not call from callback fn's.
Definition: Timeline.cpp:86
Tween< T >::Options appendTo(Anim< T > *target, T endValue, float duration, EaseFn easeFunction=easeNone, typename Tween< T >::LerpFn lerpFunction=&tweenLerp< T >)
Creates a new tween and adds it to the end of the last tween on target, or if no existing tween match...
Definition: Timeline.h:73
float mCurrentTime
Definition: Timeline.h:214
void stepTo(float absoluteTime)
Goes to a specific time and evaluates items.
Definition: Timeline.cpp:58
virtual void start(bool reverse)
Definition: Timeline.h:237
Definition: Tween.h:266
virtual void update(float relativeTime)
Definition: Timeline.h:239
size_t getNumItems() const
Returns the number of items in the Timeline.
Definition: Timeline.h:155
virtual TimelineItemRef clone() const
Creates a clone of the item.
Definition: Timeline.cpp:348
CueRef add(const std::function< void()> &action, float atTime)
add a cue to the Timeline add the start-time atTime
Definition: Timeline.cpp:78
Tween< T >::Options applyPtr(T *target, T startValue, T endValue, float duration, EaseFn easeFunction=easeNone, typename Tween< T >::LerpFn lerpFunction=&tweenLerp< T >)
Replaces any existing tweens on the target with a new tween at the timeline's current time...
Definition: Timeline.h:99
virtual TimelineItemRef cloneReverse() const
Creates a cloned item which runs in reverse relative to a timeline of duration timelineDuration.
Definition: Timeline.cpp:353
virtual void loopStart()
Definition: Timeline.cpp:342
std::multimap< void *, TimelineItemRef > mItems
Definition: Timeline.h:216
void itemTimeChanged(TimelineItem *item)
Call this to notify the Timeline if the item's start-time or duration has changed. Advanced use cases only.
Definition: Timeline.cpp:330
Tween< T >::Options applyPtr(T *target, T endValue, float duration, EaseFn easeFunction=easeNone, typename Tween< T >::LerpFn lerpFunction=&tweenLerp< T >)
Replaces any existing tweens on the target with a new tween at the timeline's current time...
Definition: Timeline.h:89
Base interface for anything that can go on a Timeline.
Definition: TimelineItem.h:34
virtual void start(bool reverse)
Definition: Timeline.h:205
bool getDefaultAutoRemove() const
Returns the default autoRemove value for all future TimelineItems added to the Timeline.
Definition: Timeline.h:183
void eraseMarked()
Definition: Timeline.cpp:132
Definition: Timeline.h:223
float easeNone(float t)
Easing equation for a simple linear tweening with no easing.
Definition: Easing.h:49
CueRef create(const std::function< void()> &fn, float atTime=0)
Definition: Timeline.h:227
Tween< T >::Options appendTo(Anim< T > *target, T startValue, T endValue, float duration, EaseFn easeFunction=easeNone, typename Tween< T >::LerpFn lerpFunction=&tweenLerp< T >)
Creates a new tween and adds it to the end of the last tween on target, or if no existing tween match...
Definition: Timeline.h:81
void setParentTimeline(TimelineRef parentTimeline)
Definition: Tween.cpp:109
bool empty() const
Returns true if there are no items in the Timeline.
Definition: Timeline.h:157
virtual void reverse()
Definition: Timeline.cpp:303
const T * ptr() const
Definition: Tween.h:369
void setDefaultAutoRemove(bool defaultAutoRemove)
Sets the default autoRemove value for all future TimelineItems added to the Timeline.
Definition: Timeline.h:181
float findEndTimeOf(void *target, bool *found=NULL) const
Returns the end of the latest-ending item in the timeline the target of which matches target...
Definition: Timeline.cpp:206
void replaceTarget(void *target, void *replacementTarget)
Replaces the target of all TimelineItems whose target matches target, with replacementTarget.
Definition: Timeline.cpp:274
std::function< void()> getFn() const
Definition: Timeline.h:230
Definition: Tween.h:46
void removeTarget(void *target)
Removes all TimelineItems whose target matches target.
Definition: Timeline.cpp:242
FnTweenRef< T > applyFn(const std::function< void(T)> &fn, T startValue, T endValue, float duration, const EaseFn &easeFunction=easeNone, const typename Tween< T >::LerpFn &lerpFunction=&tweenLerp< T >)
Definition: Timeline.h:133
std::shared_ptr< class TimelineItem > TimelineItemRef
Definition: TimelineItem.h:31
virtual void complete(bool reverse)
Definition: Timeline.h:240
void cloneAndReplaceTarget(void *target, void *replacementTarget)
Clones all TimelineItems whose target matches target, but replacing their target with replacementTarg...
Definition: Timeline.cpp:254
std::shared_ptr< class Cue > CueRef
Definition: Timeline.h:39
virtual void complete(bool reverse)
Definition: Timeline.h:208
virtual TimelineItemRef clone() const
Creates a clone of the item.
Definition: Timeline.cpp:309
std::function< float(float)> EaseFn
Definition: Tween.h:42
Definition: Tween.h:285
TimelineItemRef findLast(void *target) const
Returns the latest-starting item in the timeline the target of which matches target.
Definition: Timeline.cpp:171
Tween< T >::Options appendToPtr(T *target, T endValue, float duration, EaseFn easeFunction=easeNone, typename Tween< T >::LerpFn lerpFunction=&tweenLerp< T >)
Creates a new tween and adds it to the end of the last tween on target, or if no existing tween match...
Definition: Timeline.h:109
Definition: Timeline.h:42
void setFn(const std::function< void()> &fn)
Definition: Timeline.h:229
virtual void loopStart()
Definition: Timeline.cpp:298
virtual bool updateAtLoopStart()
Call update() only at the beginning of each loop (for example Cues exhibit require this behavior) ...
Definition: Timeline.h:241