CUGL 3.0
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Friends | List of all members
cugl::ActionTimeline Class Reference

#include <CUActionTimeline.h>

Public Member Functions

 ActionTimeline ()
 
 ~ActionTimeline ()
 
void dispose ()
 
bool init ()
 
bool add (const std::string key, ActionFunction action, float duration)
 
bool add (const std::string key, const std::shared_ptr< Action > &action, float duration)
 
bool add (const std::string key, ActionFunction action, float duration, EasingFunction easing)
 
bool add (const std::string key, const std::shared_ptr< Action > &action, float duration, EasingFunction easing)
 
bool remove (const std::string key)
 
void update (float dt)
 
bool isActive (const std::string key) const
 
Uint32 addListener (const std::string key, float time, ActionListener listener)
 
Uint32 addCompletionListener (const std::string key, ActionListener listener)
 
const ActionListener getListener (Uint32 key) const
 
bool removeListener (Uint32 key)
 
float getElapsed (const std::string key) const
 
bool isPaused (const std::string key)
 
void pause (const std::string key)
 
void unpause (const std::string key)
 

Static Public Member Functions

static std::shared_ptr< ActionTimelinealloc ()
 

Friends

class ActionInstance
 

Detailed Description

This class provides an timeline for managing active animations.

Because we do not want to force the users to define hash functions for their actions, actions are attached to the timeline via a key. This key allows the user to pause an action or query when it is complete. With each call to update frame, this class moves the each action further along the timeline it is complete.

The timeline can only have one action for each key. However, it has no way preventing the user from assigning the same action to different keys. This is discouraged as the behavior in this case is undefined.

It is possible to assign listeners to each action to monitor its progress. As with the rest of our input listeners, attached listeners are assigned a key when they are attached, which can be used to remove them from the timeline. With that said, listeners are automatically removed when their associated action is complete.

Constructor & Destructor Documentation

◆ ActionTimeline()

cugl::ActionTimeline::ActionTimeline ( )
inline

Creates a new degenerate timeline on the stack.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.

◆ ~ActionTimeline()

cugl::ActionTimeline::~ActionTimeline ( )
inline

Deletes this timeline, disposing all resources

Member Function Documentation

◆ add() [1/4]

bool cugl::ActionTimeline::add ( const std::string  key,
ActionFunction  action,
float  duration 
)
inline

Adds an action with the given duration.

The action will be invoked at the next call to update.

This method will fail if the provided key is already in use.

Parameters
keyThe identifying key
actionThe action to animate
durationThe action duration
Returns
true if the action was successfully added

◆ add() [2/4]

bool cugl::ActionTimeline::add ( const std::string  key,
ActionFunction  action,
float  duration,
EasingFunction  easing 
)

Adds an action with the given duration and easing function

The action will be invoked at the next call to update. The easing function allows for effects like bouncing or elasticity in the linear interpolation. If null, the animation will use the standard linear easing.

This method will fail if the provided key is already in use.

Parameters
keyThe identifying key
actionThe action to animate
durationThe action duration
easingThe easing (interpolation) function
Returns
true if the animation was successfully started

◆ add() [3/4]

bool cugl::ActionTimeline::add ( const std::string  key,
const std::shared_ptr< Action > &  action,
float  duration 
)
inline

Adds an action with the given duration.

The action will be invoked at the next call to update.

This method will fail if the provided key is already in use.

Parameters
keyThe identifying key
actionThe action to animate
durationThe action duration
Returns
true if the animation was successfully started

◆ add() [4/4]

bool cugl::ActionTimeline::add ( const std::string  key,
const std::shared_ptr< Action > &  action,
float  duration,
EasingFunction  easing 
)

Actives an animation with the given action and easing function

The action will be invoked at the next call to update. The easing function allows for effects like bouncing or elasticity in the linear interpolation. If null, the animation will use the standard linear easing.

This method will fail if the provided key is already in use.

Parameters
keyThe identifying key
actionThe action to animate
durationThe action duration
easingThe easing (interpolation) function
Returns
true if the animation was successfully started

◆ addCompletionListener()

Uint32 cugl::ActionTimeline::addCompletionListener ( const std::string  key,
ActionListener  listener 
)

Adds a listener for action completion.

This listener will be invoked when action is completed, just before it is removed from this timeline. This method is the same as calling addListener with a time greater than the duration.

If there is no action for the given key, this method will return 0, indicating failure.

Parameters
keyThe key identifying the action
listenerThe listener to add
Returns
a key identifying this listener (or 0 for failure)

◆ addListener()

Uint32 cugl::ActionTimeline::addListener ( const std::string  key,
float  time,
ActionListener  listener 
)

Adds a listener for the specified action at the given time.

This listener will be invoked when the timeline first passes the given time for the specified object. Due to framerate imprecision, the actual time the listener is invoked may be slightly greater than the time requested.

If time is greater than or equal to the duration of action, this listener will be invoked once the action is completed. If it is less than or equal to 0, it will be invoked once the action is started.

If there is no action for the given key, this method will return 0, indicating failure.

Parameters
keyThe key identifying the action
timeThe invocation time
listenerThe listener to add
Returns
a key identifying this listener (or 0 for failure)

◆ alloc()

static std::shared_ptr< ActionTimeline > cugl::ActionTimeline::alloc ( )
inlinestatic

Returns a newly allocated timeline.

The timeline will have no attached actions and will have an initial getElapsed of 0.

Returns
a newly allocated timeline.

◆ dispose()

void cugl::ActionTimeline::dispose ( )

Disposes all of the resources used by this timeline.

A disposed action manager can be safely reinitialized. Any animations owned by this action manager will immediately stop and be released.

◆ getElapsed()

float cugl::ActionTimeline::getElapsed ( const std::string  key) const

Returns the elapsed time of the given action.

If there is no animation for the give key (e.g. the animation is complete) this method will return 0.

Parameters
keyThe identifying key
Returns
the elapsed time of the given action.

◆ getListener()

const ActionListener cugl::ActionTimeline::getListener ( Uint32  key) const

Returns the action listener for the given key

If there is no listener for the given key, it returns nullptr.

Parameters
keyThe identifier for the listener
Returns
the action listener for the given key

◆ init()

bool cugl::ActionTimeline::init ( )
inline

Initializes a new timeline.

The timeline will have no attached actions and will have an initial getElapsed of 0.

Returns
true if initialization was successful.

◆ isActive()

bool cugl::ActionTimeline::isActive ( const std::string  key) const

Returns true if the given key represents an active action

Note that paused actions are still active, even though they are paused.

Parameters
keyThe identifying key
Returns
true if the given key represents an active action

◆ isPaused()

bool cugl::ActionTimeline::isPaused ( const std::string  key)

Returns true if the animation for the given key is paused

This method will return false if there is no active animation with the given key.

Parameters
keyThe identifying key
Returns
true if the animation for the given key is paused

◆ pause()

void cugl::ActionTimeline::pause ( const std::string  key)

Pauses the animation for the given key.

If there is no active animation for the given key, or if it is already paused, this method does nothing.

Parameters
keyThe identifying key

◆ remove()

bool cugl::ActionTimeline::remove ( const std::string  key)

Removes the action for the given key.

This method will immediately stop the animation. In particular, it will invoke any listeners waiting on completion.

If there is no animation for the give key (e.g. the animation is complete) this method will return false.

Parameters
keyThe identifying key
Returns
true if the animation was successfully removed

◆ removeListener()

bool cugl::ActionTimeline::removeListener ( Uint32  key)

Removes the action listener for the given key

If there is no active listener for the given key, this method fails and returns false.

Parameters
keyThe identifier for the listener
Returns
true if the listener was succesfully removed

◆ unpause()

void cugl::ActionTimeline::unpause ( const std::string  key)

Unpauses the animation for the given key.

If there is no active animation for the given key, or if it is not currently paused, this method does nothing.

Parameters
keyThe identifying key

◆ update()

void cugl::ActionTimeline::update ( float  dt)

Updates all non-paused actions by dt seconds

Each action is moved forward by dt seconds. If this causes an action to reach its duration, the action is removed and the key is once again available.

Parameters
dtThe number of seconds to animate

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