Drawable representation of a texture. More...
#include <Sprite.h>
Public Types | |
using | Ptr = std::unique_ptr< Sprite > |
Unique sprite pointer. More... | |
Public Member Functions | |
Sprite () | |
Construct an empty sprite. More... | |
Sprite (const std::string &texture, const UIntRect &rectangle={}) | |
Construct a sprite from a texture. More... | |
Sprite (const Texture &texture, const UIntRect &rectangle={}) | |
Construct a sprite from a texture. More... | |
Sprite (const Sprite &) | |
Copy constructor. More... | |
Sprite & | operator= (const Sprite &) |
Copy assignment operator. More... | |
Sprite (Sprite &&) noexcept | |
Move constructor. More... | |
Sprite & | operator= (Sprite &&) noexcept |
Move assignment operator. More... | |
Sprite::Ptr | copy () const |
Create a copy of the sprite. More... | |
std::string | getClassName () const override |
Get the name of this class. More... | |
void | setTexture (const std::string &filename) |
Set the texture of the object. More... | |
void | setTexture (const Texture &texture) |
Set the texture of the sprite from a source texture. More... | |
const Texture & | getTexture () const |
Get the texture used by the sprite. More... | |
void | setTextureRect (unsigned int left, unsigned int top, unsigned int width, unsigned int height) |
Set the sub-rectangle of the texture that the sprite will display. More... | |
void | setTextureRect (const UIntRect &rect) |
Set the sub-rectangle of the texture that the sprite will display. More... | |
UIntRect | getTextureRect () const |
Get the sub-rectangle of the texture displayed by the sprite. More... | |
void | setColour (Colour colour) |
Set the colour of the sprite. More... | |
Colour | getColour () const |
Get the colour of the sprite. More... | |
void | setOpacity (unsigned int opacity) |
Set the opacity of the sprite. More... | |
unsigned int | getOpacity () const |
Get the opacity of the sprite. More... | |
void | setVisible (bool visible) |
Show or hide the sprite. More... | |
bool | isVisible () const |
Check whether or not the sprite is visible. More... | |
void | toggleVisibility () |
Toggle the visibility of the sprite. More... | |
FloatRect | getLocalBounds () const |
Get the local bounding rectangle of the sprite. More... | |
FloatRect | getGlobalBounds () const |
Get the global bounding rectangle of the sprite. More... | |
void | setPosition (float x, float y) override |
Set the position of the sprite. More... | |
void | setPosition (const Vector2f &position) override |
Set the position of the sprite. More... | |
Vector2f | getPosition () const override |
Get the position of the sprite. More... | |
void | setRotation (float angle) override |
Set the orientation of the sprite. More... | |
void | rotate (float angle) override |
Rotate the sprite. More... | |
float | getRotation () const override |
Get the orientation of the sprite. More... | |
void | setScale (float factorX, float factorY) override |
Set the scale factors of the sprite. More... | |
void | setScale (const Vector2f &scale) override |
Set the scale factor of the sprite. More... | |
void | scale (float factorX, float factorY) override |
Scale the sprite by an offset. More... | |
void | scale (const Vector2f &offset) override |
Scale the sprite by an offset. More... | |
Vector2f | getScale () const override |
Get the current scale of the sprite. More... | |
void | setOrigin (float x, float y) override |
Set the local origin of the sprite. More... | |
void | setOrigin (const Vector2f &origin) override |
Set the local origin of the sprite. More... | |
Vector2f | getOrigin () const override |
Get the local origin of the sprite. More... | |
void | move (float offsetX, float offsetY) override |
Move the sprite by a given offset. More... | |
void | move (const Vector2f &offset) override |
Move the sprite by a given offset. More... | |
void | draw (priv::RenderTarget &renderTarget) const override |
Draw object on a render target. More... | |
Animator & | getAnimator () |
Get the sprites animator. More... | |
const Animator & | getAnimator () const |
void | updateAnimation (Time deltaTime) |
void | swap (Sprite &other) |
Swap this sprite with another sprite. More... | |
~Sprite () override | |
Destructor. More... | |
std::string | getClassType () const override |
Get the name of this class. More... | |
void | setTag (const std::string &tag) |
Assign the object an alias. More... | |
const std::string & | getTag () const |
Get the tag assigned to the object. More... | |
unsigned int | getObjectId () const |
Get the unique id of the object. More... | |
int | onPropertyChange (const std::string &property, const Callback< Property > &callback, bool oneTime=false) |
Add an event listener to a specific property change event. More... | |
int | onPropertyChange (const Callback< Property > &callback, bool oneTime=false) |
Add an event listener to any property change event. More... | |
void | suspendedEventListener (int id, bool suspend) |
Pause or resume execution of an event listener. More... | |
bool | isEventListenerSuspended (int id) const |
Check if an event listener is suspended or not. More... | |
bool | removeEventListener (const std::string &event, int id) |
Remove an event listener from an event. More... | |
bool | removeEventListener (int id) |
Remove an event listener. More... | |
int | onDestruction (const Callback<> &callback) |
Add a destruction listener. More... | |
bool | isSameObjectAs (const Object &other) const |
Check if another object is the same instance as this object. More... | |
Static Public Member Functions | |
static Sprite::Ptr | create (const std::string &texture, const UIntRect &rectangle={}) |
Create a new Sprite instance. More... | |
static Sprite::Ptr | create (const Texture &texture, const UIntRect &rectangle) |
Construct the sprite from a sub-rectangle of a source texture. More... | |
Protected Member Functions | |
void | emitChange (const Property &property) |
Dispatch a property change event. More... | |
void | emitDestruction () |
Emit a destruction event. More... | |
Protected Attributes | |
EventEmitter | eventEmitter_ |
Event dispatcher. More... | |
Drawable representation of a texture.
The sprite can be static (display a single non changing texture) or animated via its animator (see the getAnimator function)
using ime::Sprite::Ptr = std::unique_ptr<Sprite> |
ime::Sprite::Sprite | ( | ) |
Construct an empty sprite.
|
explicit |
Construct a sprite from a texture.
texture | Filename of the texture to construct the sprite from |
rectangle | A sub-rectangle of the texture to assign to the sprite |
FileNotFoundException | if the specified texture cannot be loaded |
The rectangle can be used to construct the sprite from a sub-rectangle of the whole texture. To construct the sprite from the whole texture (default), leave the rectangle argument unspecified
Construct a sprite from a texture.
texture | Texture to construct sprite from |
rectangle | A sub-rectangle of the texture to assign to the sprite |
The rectangle can be used to construct the sprite from a sub-rectangle of the whole texture. To construct the sprite from the whole texture (default), leave the rectangle argument unspecified
ime::Sprite::Sprite | ( | const Sprite & | ) |
Copy constructor.
|
noexcept |
Move constructor.
|
override |
Destructor.
Sprite::Ptr ime::Sprite::copy | ( | ) | const |
|
static |
Create a new Sprite instance.
texture | Filename of the texture to construct the sprite from |
rectangle | A sub-rectangle of the texture to assign to the sprite |
FileNotFoundException | if the specified texture cannot be loaded |
The rectangle can be used to construct the sprite from a sub-rectangle of the whole texture. To construct the sprite from the whole texture (default), leave the rectangle argument unspecified
|
static |
Construct the sprite from a sub-rectangle of a source texture.
texture | The source texture |
rectangle | Sub-rectangle of the texture to assign to the sprite |
The texture is copied, so theres no need to keep it alive. The rectangle can be used to construct the sprite from a sub-rectangle of the whole texture. To construct the sprite from the whole texture (default), leave the rectangle argument unspecified
|
overridevirtual |
Draw object on a render target.
renderTarget | Target to draw object on |
Implements ime::Drawable.
|
protectedinherited |
Dispatch a property change event.
property | The property that changed |
This function will invoke all the event listeners of the specified property
|
protectedinherited |
Emit a destruction event.
Animator & ime::Sprite::getAnimator | ( | ) |
Get the sprites animator.
This function enables the sprite to be animated
checkout the Animator class
|
overridevirtual |
|
overridevirtualinherited |
Get the name of this class.
Note that this function is only overridden by child classes of Object which also serve as a base class for other classes
Reimplemented from ime::Object.
Reimplemented in ime::Shape.
Colour ime::Sprite::getColour | ( | ) | const |
Get the colour of the sprite.
FloatRect ime::Sprite::getGlobalBounds | ( | ) | const |
Get the global bounding rectangle of the sprite.
The returned rectangle is in global coordinates, which means that it takes into account the transformations (translation, rotation, scale, ...) that are applied to the sprite. In other words, this function returns the bounds of the sprite in the global 2D world's coordinate system
FloatRect ime::Sprite::getLocalBounds | ( | ) | const |
Get the local bounding rectangle of the sprite.
The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the sprite. In other words, this function returns the bounds of the sprite in the sprite's coordinate system
|
inherited |
Get the unique id of the object.
Note that each instance of ime::Object has a unique id
unsigned int ime::Sprite::getOpacity | ( | ) | const |
|
overridevirtual |
Get the local origin of the sprite.
Implements ime::ITransformable.
|
overridevirtual |
Get the position of the sprite.
Implements ime::ITransformable.
|
overridevirtual |
Get the orientation of the sprite.
The rotation is always in the range [0, 360]
Implements ime::ITransformable.
|
overridevirtual |
Get the current scale of the sprite.
Implements ime::ITransformable.
|
inherited |
const Texture & ime::Sprite::getTexture | ( | ) | const |
Get the texture used by the sprite.
UIntRect ime::Sprite::getTextureRect | ( | ) | const |
Get the sub-rectangle of the texture displayed by the sprite.
|
inherited |
Check if an event listener is suspended or not.
id | The identification number of the listener to be checked |
This function also returns false if the specified event listener does not exist
|
inherited |
Check if another object is the same instance as this object.
other | The object to compare against this object |
bool ime::Sprite::isVisible | ( | ) | const |
Check whether or not the sprite is visible.
|
overridevirtual |
Move the sprite by a given offset.
offset | Offset to apply |
This function adds to the current position of the sprite, unlike setPosition which overwrites it
Implements ime::ITransformable.
|
overridevirtual |
Move the sprite by a given offset.
offsetX | Horizontal offset |
offsetY | Vertical offset |
This function adds to the current position of the sprite, unlike setPosition which overwrites it
Implements ime::ITransformable.
|
inherited |
Add a destruction listener.
callback | Function to be executed when the object is destroyed |
The destruction listener is called when the object reaches the end of its lifetime. Note that an object may have multiple destruction listeners registered to it
|
inherited |
Add an event listener to any property change event.
callback | The function to be executed when any property changes |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event
|
inherited |
Add an event listener to a specific property change event.
property | The name of the property to add an event listener to |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
callback | The function to be executed when the property changes |
A property change event is triggered by any function that begins with set, where the the text after set is the name of the property. For example, the setTag() function, modifies the tag property of the object, thus will generate a "tag" change event each time it is called
Note that multiple event listeners may be registered to the same property change event. In addition, when adding a property change event listener, the name of the property must be in lowercase.
|
inherited |
Remove an event listener from an event.
event | The name of the event to remove an event listener from |
id | The unique id of the event listener to be removed |
|
inherited |
Remove an event listener.
id | The id of the event listener to be removed |
|
overridevirtual |
Rotate the sprite.
angle | Angle of rotation, in degrees |
This function adds to the current rotation of the sprite, unlike setRotation which overwrites it
Implements ime::ITransformable.
|
overridevirtual |
Scale the sprite by an offset.
offset | Offset to apply |
This function multiplies the current scale of the sprite, unlike setScale which overwrites it
Implements ime::ITransformable.
|
overridevirtual |
Scale the sprite by an offset.
factorX | Horizontal scale factor |
factorY | Vertical scale factor |
This function multiplies the current scale of the sprite, unlike setScale which overwrites it
Implements ime::ITransformable.
void ime::Sprite::setColour | ( | Colour | colour | ) |
Set the colour of the sprite.
colour | New colour opf the sprite |
void ime::Sprite::setOpacity | ( | unsigned int | opacity | ) |
Set the opacity of the sprite.
opacity | The new opacity of the sprite in the range [0, 255] |
By default the opacity is 255
|
overridevirtual |
Set the local origin of the sprite.
origin | New origin |
The origin of the sprite defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the sprite, and ignore all transformations (position, scale, rotation).
The default origin of the sprite is (0, 0)
Implements ime::ITransformable.
|
overridevirtual |
Set the local origin of the sprite.
x | X coordinate of the new origin |
y | Y coordinate of the new origin |
The origin of the sprite defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the sprite, and ignore all transformations (position, scale, rotation).
The default origin of the sprite is (0, 0)
Implements ime::ITransformable.
|
overridevirtual |
Set the position of the sprite.
position | New position |
This function completely overwrites the previous position. Use the move function to apply an offset based on the previous position instead.
The default position of the sprite is (0, 0)
Implements ime::ITransformable.
|
overridevirtual |
Set the position of the sprite.
x | X coordinate of the new position |
y | Y coordinate of the new position |
This function completely overwrites the previous position. use move function to apply an offset based on the previous position instead
The default position of a the sprite is (0, 0)
Implements ime::ITransformable.
|
overridevirtual |
Set the orientation of the sprite.
angle | New rotation, in degrees |
This function completely overwrites the previous rotation. See the rotate function to add an angle based on the previous rotation instead.
The default rotation of the sprite is 0
Implements ime::ITransformable.
|
overridevirtual |
Set the scale factor of the sprite.
scale | New scale |
This function completely overwrites the previous scale
Implements ime::ITransformable.
|
overridevirtual |
Set the scale factors of the sprite.
factorX | New horizontal scale factor |
factorY | New vertical scale factor |
This function completely overwrites the previous scale
Implements ime::ITransformable.
|
inherited |
Assign the object an alias.
tag | The alias of the object |
This function is useful if you want to refer to the object by a tag instead of its object id. Unlike an object id, multiple objects may have the same tag
By default, the tag is an empty string
void ime::Sprite::setTexture | ( | const std::string & | filename | ) |
Set the texture of the object.
filename | Filename of the texture to set |
FileNotFoundException | if the specified texture cannot be found in the images path |
This function will set the texture to the whole image
void ime::Sprite::setTexture | ( | const Texture & | texture | ) |
Set the texture of the sprite from a source texture.
texture | The source texture |
The texture is copied
void ime::Sprite::setTextureRect | ( | const UIntRect & | rect | ) |
Set the sub-rectangle of the texture that the sprite will display.
rect | The rectangle to set |
This function will set the texture that the sprite displays to a portion of the entire texture.
By default, the sprite displays the entire texture
void ime::Sprite::setTextureRect | ( | unsigned int | left, |
unsigned int | top, | ||
unsigned int | width, | ||
unsigned int | height | ||
) |
Set the sub-rectangle of the texture that the sprite will display.
left | horizontal starting position |
top | Vertical starting position |
width | Width of the sub-rectangle |
height | Height of the sub-rectangle |
This function will set the texture that the sprite displays to a portion of the entire texture. The displaying will start at the specified starting position
By default, the sprite displays the entire texture
void ime::Sprite::setVisible | ( | bool | visible | ) |
Show or hide the sprite.
visible | True to show or false to hide |
When hidden the sprite will not be shown on the render target
By default, the sprite is visible
|
inherited |
Pause or resume execution of an event listener.
id | The event listeners unique identification number |
suspend | True to suspend/pause or false to unsuspend/resume |
void ime::Sprite::swap | ( | Sprite & | other | ) |
Swap this sprite with another sprite.
other | The sprite to be swapped with this sprite |
void ime::Sprite::toggleVisibility | ( | ) |
Toggle the visibility of the sprite.
This function will hide the sprite if its currently visible or show it if it is currently hidden
|
protectedinherited |