Class for animating sprites. More...
#include <Animator.h>
Public Types | |
enum | Event { Event::AnimationPlay, Event::AnimationStart, Event::AnimationPause, Event::AnimationResume, Event::AnimationStop, Event::AnimationComplete, Event::AnimationRepeat, Event::AnimationRestart, Event::AnimationSwitch } |
Animation events (Triggered by the current animation) More... | |
using | Ptr = std::shared_ptr< Animator > |
using | Animations = std::initializer_list< Animation::Ptr > |
Alias. More... | |
Public Member Functions | |
Animator () | |
Default constructor. More... | |
Animator (Sprite &target) | |
Constructor. More... | |
Animator (const Animator &other) | |
Construct animator from an existing animator. More... | |
Animator & | operator= (Animator) |
Copy assignment operator. More... | |
Animation::Ptr | createAnimation (const std::string &name, const SpriteSheet &spriteSheet, Time duration=Time::Zero) |
Create an animation in the animator. More... | |
void | setTarget (Sprite &target) |
Set the target to be animated. More... | |
void | setTimescale (float timescale) |
Set the timescale factor. More... | |
float | getTimescale () const |
Get the timescale factor of the current animation. More... | |
bool | addAnimation (Animation::Ptr animation) |
Add an animation to the animator. More... | |
void | addAnimation (Animations animations) |
Add multiple animations at the same time to the animator. More... | |
Animation::Ptr | getAnimation (const std::string &name) const |
Get an animation in the animator. More... | |
Animation::Ptr | getCurrentAnimation () const |
Get the current animation. More... | |
bool | removeAnimation (const std::string &name) |
Remove an animation from the animator. More... | |
void | removeAll () |
Remove all animations from the animator. More... | |
bool | hasAnimation (const Animation::Ptr &animation) const |
Check whether or not an animation exists in the animator. More... | |
bool | hasAnimation (const std::string &name) const |
Check whether or not an animation exists in the animator. More... | |
void | chainAnimation (Animation::Ptr animation) |
Chain an animation to the current animation. More... | |
void | chainAnimation (const std::string &name) |
Chain an animation to play after the current animation. More... | |
bool | removeChain (const std::string &name) |
Remove an animation from a chain. More... | |
void | clearAllChains () |
Remove all the animations chained to the current animation. More... | |
bool | switchAnimation (const std::string &name, bool ignorePlaying=false) |
Change the current animation. More... | |
void | startAnimation (const std::string &name, bool unchain=false) |
Play an animation immediately. More... | |
void | play () |
Play the current animation if any. More... | |
void | pause () |
Pause the current animation. More... | |
void | resume () |
Resume a paused animation. More... | |
void | stop () |
Stop the current animation. More... | |
void | restart () |
Restart the current animation from the beginning. More... | |
void | complete () |
Finish the current animation. More... | |
bool | isAnimationPlaying () const |
Check whether or not the current animation is playing. More... | |
bool | isAnimationPaused () const |
Check whether or not the current animation is paused. More... | |
bool | hasAnimationStarted () const |
Check if the current animation has started. More... | |
void | update (Time deltaTime) |
Update the current animation. More... | |
int | on (Event event, Callback< Animation::Ptr > callback) |
Subscribe a callback to an animation event. More... | |
int | on (Event event, Callback<> callback) |
int | on (Event event, const std::string &name, Callback< Animation::Ptr > callback) |
Subscribe a callback to a specific animation event. More... | |
int | on (Event event, const std::string &name, Callback<> callback) |
bool | unsubscribe (Event event, int id) |
Remove a callback from an animation event. More... | |
bool | unsubscribe (Event event, const std::string &name, int id) |
Remove a callback from a specific animation. More... | |
Class for animating sprites.
This class takes an Animation object and animates a sprite with the properties of the animation object. The animator can store multiple animations but only one can be played at a time. This class is usually not instantiated directly, all animatable entities internally have an animator
Definition at line 51 of file Animator.h.
using ime::Animator::Animations = std::initializer_list<Animation::Ptr> |
Alias.
Definition at line 54 of file Animator.h.
|
strong |
Animation events (Triggered by the current animation)
Definition at line 59 of file Animator.h.
ime::Animator::Animator | ( | ) |
Default constructor.
|
explicit |
ime::Animator::Animator | ( | const Animator & | other | ) |
Construct animator from an existing animator.
other | The animator to construct this animator from |
bool ime::Animator::addAnimation | ( | Animation::Ptr | animation | ) |
Add an animation to the animator.
animation | Animation to be added |
void ime::Animator::addAnimation | ( | Animations | animations | ) |
Add multiple animations at the same time to the animator.
animations | Animations to be added |
void ime::Animator::chainAnimation | ( | Animation::Ptr | animation | ) |
Chain an animation to the current animation.
animation | The animation to be chained |
A chained animation is an animation that plays immediately after, the current animation finishes. If there is no current animation then this animation will be set as the current animation and played. If the current animation is chained to another animation then then the given animation will be chained to that animation
void ime::Animator::chainAnimation | ( | const std::string & | name | ) |
Chain an animation to play after the current animation.
name | Name of the animation to be chained |
A chained animation is an animation that plays immediately after, the current animation finishes. If there is no current animation then this animation will be set as the current animation and played. If the current animation is chained to another animation then then the given animation will be chained to that animation
void ime::Animator::clearAllChains | ( | ) |
Remove all the animations chained to the current animation.
void ime::Animator::complete | ( | ) |
Finish the current animation.
This function will force the current animation to complete and any chained animations will start playing
This function will dispatch an Event::AnimationComplete event
Animation::Ptr ime::Animator::createAnimation | ( | const std::string & | name, |
const SpriteSheet & | spriteSheet, | ||
Time | duration = Time::Zero |
||
) |
Create an animation in the animator.
name | Name of the animation |
spriteSheet | The spritesheet containing the animation frames |
duration | The duration of the animation |
The animation will be created and added to the Animator and a pointer to it will be returned. Therefore calling addAnimation with the returned pointer will always fail since it already exist in the animator
All the frame data used by the animation will be extracted from the spritesheet. As a result, the spritesheet must already be constructed before passed it as an argument. Passing an uncreated SpriteSheet is undefined behavior
By default the duration is derived from the frame rate. If the duration is specified then the frame rate will be overridden and derived from the given duration
Animation::Ptr ime::Animator::getAnimation | ( | const std::string & | name | ) | const |
Get an animation in the animator.
name | The name of the animation to retrieve |
Animation::Ptr ime::Animator::getCurrentAnimation | ( | ) | const |
Get the current animation.
float ime::Animator::getTimescale | ( | ) | const |
Get the timescale factor of the current animation.
bool ime::Animator::hasAnimation | ( | const Animation::Ptr & | animation | ) | const |
Check whether or not an animation exists in the animator.
animation | Animation to be checked |
bool ime::Animator::hasAnimation | ( | const std::string & | name | ) | const |
Check whether or not an animation exists in the animator.
name | Name of the animation to be checked |
bool ime::Animator::hasAnimationStarted | ( | ) | const |
Check if the current animation has started.
This function returns false if either the current animation is not playing or the animation is playing but it is still waiting for the start delay to expire
bool ime::Animator::isAnimationPaused | ( | ) | const |
Check whether or not the current animation is paused.
This function returns false if there is no current animation or the animation is playing or is stopped
bool ime::Animator::isAnimationPlaying | ( | ) | const |
Check whether or not the current animation is playing.
This function will return false if there is no current animation or the animation is paused or stopped
int ime::Animator::on | ( | Event | event, |
Callback< Animation::Ptr > | callback | ||
) |
Subscribe a callback to an animation event.
event | Name of the event to subscribe a callback to |
callback | Function to be executed when the event is fired |
The callback id must be remembered if the callback must be removed from the event at a later time
The callback function is optionally passed the animation that triggered the event
int ime::Animator::on | ( | Event | event, |
const std::string & | name, | ||
Callback< Animation::Ptr > | callback | ||
) |
Subscribe a callback to a specific animation event.
event | The event to subscribe a callback to |
name | The name of the animation to subscribe callback to |
callback | The Function to be executed when the event is fired |
This function subscribes a callback to events triggered by a specific animation only. The callback will not be invoked for events triggered by animations whose name do not match name.
To unsubscribe callbacks registered with this function, use the unsubscribe function that removes only callbacks that were subscribed to events triggered by specific animation objects
The callback function is optionally passed the animation that triggered the event which in this case its always the animation with the name
void ime::Animator::pause | ( | ) |
Pause the current animation.
This function will dispatch an Event::AnimationPause event if the current animation was playing
void ime::Animator::play | ( | ) |
void ime::Animator::removeAll | ( | ) |
Remove all animations from the animator.
bool ime::Animator::removeAnimation | ( | const std::string & | name | ) |
Remove an animation from the animator.
name | Name of the animation to be removed |
bool ime::Animator::removeChain | ( | const std::string & | name | ) |
Remove an animation from a chain.
name | Name of the animation to be removed |
void ime::Animator::restart | ( | ) |
Restart the current animation from the beginning.
This function will dispatch an Event::AnimationRestart event
void ime::Animator::resume | ( | ) |
Resume a paused animation.
This function will dispatch an Event::AnimationResume event if the current animation was paused
void ime::Animator::setTarget | ( | Sprite & | target | ) |
Set the target to be animated.
target | The target for the animations |
void ime::Animator::setTimescale | ( | float | timescale | ) |
Set the timescale factor.
timescale | The new timescale factor |
The timescale factor control the speed of the current animation For example, a timescale of 2 will make the animation twice as fast whilst a timescale of 0.5 will slow down the animation
The timescale will be set to 1 if timescale is negative.
By default the timescale is 1
void ime::Animator::startAnimation | ( | const std::string & | name, |
bool | unchain = false |
||
) |
Play an animation immediately.
name | Name of the animation to play |
unchain | True to clear all chained animations, or false to chained pending chains to the specified animation |
This function will stop the current animation if there is any and immediately play the specified animation. If the specified animation does not exist in the animator the current animation will continue playing
By default, animations that were pending to be chained to the previous animation will be chained to the specified animation
To switch the animation without playing it, use the switchAnimation function
void ime::Animator::stop | ( | ) |
Stop the current animation.
This function will dispatch an Event::AnimationStop event if the current animation was playing. The event is dispatched whether or not the current animation was started or waiting for a delay to expire
bool ime::Animator::switchAnimation | ( | const std::string & | name, |
bool | ignorePlaying = false |
||
) |
Change the current animation.
name | Name of the new animation |
ignorePlaying | True to ignore switch if an animation is currently playing otherwise false to switch anyway |
This function does not start the animation, it only sets the current animation to the given animation such that when play() is called, the given animation is played.
By default the animation will be switched whether or not there is an active animation
To switch and immediately play an animation use the playAnimation function
bool ime::Animator::unsubscribe | ( | Event | event, |
const std::string & | name, | ||
int | id | ||
) |
Remove a callback from a specific animation.
event | Th event to remove callback from |
name | The name of the animation to remove the callback from |
id | The identification number of the callback |
Note that this function removes only callbacks that were subscribed to animation events triggered by a specific animation. In other words, it is only applicable to callbacks that were registered using the on(Event, const std::string&, Callback) function. For other callbacks, unsubscribe using the unsubscribe(Event, int) function
bool ime::Animator::unsubscribe | ( | Event | event, |
int | id | ||
) |
Remove a callback from an animation event.
event | The event to remove the callback from |
id | Identification number of the callback |
void ime::Animator::update | ( | Time | deltaTime | ) |
Update the current animation.
deltaTime | Time passed since last animation update |