include/cinder/TimelineItem.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2011, The Cinder Project, All rights reserved.
00003  This code is intended for use with the Cinder C++ library: http://libcinder.org
00004 
00005  Based on the sc-Choreograph CinderBlock by David Wicks: http://sansumbrella.com/
00006 
00007  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00008  the following conditions are met:
00009 
00010     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00011     the following disclaimer.
00012     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00013     the following disclaimer in the documentation and/or other materials provided with the distribution.
00014 
00015  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00016  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00017  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00018  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00019  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00020  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00021  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00022  POSSIBILITY OF SUCH DAMAGE.
00023 */
00024 
00025 #pragma once
00026 
00027 #include "cinder/Cinder.h"
00028 
00029 namespace cinder
00030 {
00031 typedef std::shared_ptr<class TimelineItem> TimelineItemRef;
00032 
00034 class TimelineItem : public std::enable_shared_from_this<TimelineItem>
00035 {
00036   public:
00037     TimelineItem( class Timeline *parent = 0 );
00038     TimelineItem( class Timeline *parent, void *target, float startTime, float duration );
00039     virtual ~TimelineItem() {}
00040     
00042     void* getTarget() const { return mTarget; }
00043 
00045     float           getStartTime() const { return mStartTime; }
00047     void            setStartTime( float newTime );
00048 
00050     float           getDuration() const { updateDuration(); return mDuration; }
00052     void            setDuration( float newDuration );
00053 
00055     bool            getLoop() const { return mLoop; }
00057     void            setLoop( bool doLoop = true ) { mLoop = doLoop; }
00058 
00060     bool            getPingPong() const { return mPingPong; }
00062     void            setPingPong( bool pingPong = true ) { mPingPong = pingPong; }
00063 
00065     bool            getInfinite() const { return mLoop; }
00067     void            setInfinite( bool infinite = true ) { mInfinite = infinite; }
00068 
00070     float           getEndTime() const { return mStartTime + mDuration; }
00071 
00073     class Timeline*     getParent() const { return mParent; }
00075     void removeSelf();
00077     virtual void reset( bool unsetStarted = false ) { if( unsetStarted ) mHasStarted = false; mComplete = false; }
00078     
00080     bool hasStarted() const { return mHasStarted; }         
00082     bool isComplete() { return mComplete; }
00083     
00085     bool    getAutoRemove() const { return mAutoRemove; }
00087     void    setAutoRemove( bool autoRemove = true ) { mAutoRemove = autoRemove; }
00088     
00089     virtual void start( bool reverse ) = 0;
00090     virtual void loopStart() {}
00091     virtual void update( float relativeTime ) = 0;
00092     virtual void complete( bool reverse ) = 0;
00094     virtual bool    updateAtLoopStart() { return false; }
00095     virtual float   calcDuration() const { return mDuration; }
00096     virtual void    reverse() = 0;
00098     virtual TimelineItemRef     clone() const = 0;
00100     virtual TimelineItemRef     cloneReverse() const = 0;
00102     void stepTo( float time, bool reverse );
00103     
00104     TimelineItemRef thisRef() { return shared_from_this(); }
00105     
00106   protected:
00107     void    setDurationDirty() { mDirtyDuration = true; }
00108     void    updateDuration() const;
00110     float   loopTime( float absTime );
00111     void    setTarget( void *target ) { mTarget = target; }
00112 
00113     class Timeline  *mParent;
00114 
00115     void    *mTarget;
00116     float   mStartTime;
00117     bool    mHasStarted, mHasReverseStarted;
00118     bool    mComplete, mReverseComplete;
00119     bool    mMarkedForRemoval;
00120     bool    mInfinite;
00121     bool    mLoop, mPingPong;
00122     bool    mUseAbsoluteTime;
00123     bool    mAutoRemove;
00124     int32_t mLastLoopIteration;
00125     
00126     friend class Timeline;
00127   private:
00128     mutable float   mDuration, mInvDuration;
00129     mutable bool    mDirtyDuration; // marked if the virtual calcDuration() needs to be calculated
00130 };
00131 
00132 } // namespace cinder
00133