Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ime::ConvexShape Class Reference

A convex shape. More...

#include <ConvexShape.h>

Inheritance diagram for ime::ConvexShape:
ime::Shape ime::Drawable ime::ITransformable ime::Object

Public Types

using Ptr = std::unique_ptr< ConvexShape >
 Shared shape pointer. More...
 
enum class  Type { Rectangle , Circle , Convex }
 The types of shapes. More...
 

Public Member Functions

 ConvexShape (std::size_t pointCount=0)
 Default constructor. More...
 
 ConvexShape (const ConvexShape &)
 Copy constructor. More...
 
ConvexShapeoperator= (const ConvexShape &)
 Copy assignment operator. More...
 
 ConvexShape (ConvexShape &&) noexcept
 Move constructor. More...
 
ConvexShapeoperator= (ConvexShape &&) noexcept
 Move assignment operator. More...
 
ConvexShape::Ptr copy () const
 Create a copy of this shape. More...
 
Shape::Ptr clone () const override
 Make a copy of this shape. More...
 
std::string getClassName () const override
 Get the name of this class. More...
 
void setPointCount (std::size_t count)
 Set the number of points of the polygon. More...
 
std::size_t getPointCount () const
 Get the number of points of the polygon. More...
 
void setPoint (std::size_t index, const Vector2f &point)
 Set the position of a point. More...
 
Vector2f getPoint (std::size_t index) const
 Get the position of a point. More...
 
 ~ConvexShape () override
 Destructor. More...
 
std::string getClassType () const override
 Get the name of this class. More...
 
Type getShapeType () const
 Get the type of this shape. More...
 
void attachRigidBody (RigidBody::Ptr body)
 Attach a rigid body to a shape. More...
 
void removeRigidBody ()
 Remove a rigid body from the shape. More...
 
RigidBodygetRigidBody ()
 Get the rigid body attached to the shape. More...
 
const RigidBodygetRigidBody () const
 
bool hasRigidBody () const
 Check if the the shape has a rigid body attached to it or not. More...
 
void setTexture (const std::string &filename)
 Set the texture of the shape. More...
 
void setTexture (const Texture &texture)
 Set the texture of the shape from a source texture. More...
 
TexturegetTexture ()
 Get the shapes texture. More...
 
const TexturegetTexture () const
 
void setFillColour (const Colour &colour)
 Set the fill colour of the shape. More...
 
Colour getFillColour () const
 Get the fill colour of the shape. More...
 
void setOutlineColour (const Colour &colour)
 Set the outline colour of the shape. More...
 
Colour getOutlineColour () const
 Get the outline colour of the shape. More...
 
void setOutlineThickness (float thickness)
 Set the thickness of the shape's outline. More...
 
float getOutlineThickness () const
 Get the outline thickness of the shape. More...
 
FloatRect getLocalBounds () const
 Get the local bounding rectangle of the shape. More...
 
FloatRect getGlobalBounds () const
 Get the global bounding rectangle of the shape. More...
 
void setPosition (float x, float y) override
 Set the position of the shape. More...
 
void setPosition (const Vector2f &position) override
 Set the position of the shape. More...
 
Vector2f getPosition () const override
 Get the position of the shape. More...
 
void setRotation (float angle) override
 Set the orientation of the shape. More...
 
void rotate (float angle) override
 Rotate the shape. More...
 
float getRotation () const override
 Get the orientation of the shape. More...
 
void setScale (float factorX, float factorY) override
 Set the scale factors of the shape. More...
 
void setScale (const Vector2f &scale) override
 Set the scale factor of the shape. More...
 
void scale (float factorX, float factorY) override
 Scale the shape by an offset. More...
 
void scale (const Vector2f &factor) override
 Scale the shape by an offset. More...
 
Vector2f getScale () const override
 Get the current scale of the shape. More...
 
void setOrigin (float x, float y) override
 Set the local origin of the shape. More...
 
void setOrigin (const Vector2f &origin) override
 Set the local origin of the shape. More...
 
Vector2f getOrigin () const override
 Get the local origin of the shape. More...
 
void move (float offsetX, float offsetY) override
 Move the shape by a given offset. More...
 
void move (const Vector2f &offset) override
 Move the shape by a given offset. More...
 
void draw (priv::RenderTarget &renderTarget) const override
 Draw object on a render target. 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 ConvexShape::Ptr create (std::size_t pointCount=0)
 Create a new convex shape. More...
 

Protected Member Functions

std::shared_ptr< void > getInternalPtr () const
 
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...
 

Detailed Description

A convex shape.

Definition at line 36 of file ConvexShape.h.

Member Typedef Documentation

◆ Ptr

using ime::ConvexShape::Ptr = std::unique_ptr<ConvexShape>

Shared shape pointer.

Definition at line 38 of file ConvexShape.h.

Member Enumeration Documentation

◆ Type

enum class ime::Shape::Type
stronginherited

The types of shapes.

Enumerator
Rectangle 

Rectangle shape.

Circle 

Circle shape.

Convex 

Convex polygon shape.

Definition at line 55 of file Shape.h.

Constructor & Destructor Documentation

◆ ConvexShape() [1/3]

ime::ConvexShape::ConvexShape ( std::size_t  pointCount = 0)
explicit

Default constructor.

Parameters
pointCountThe number of points of the polygon

◆ ConvexShape() [2/3]

ime::ConvexShape::ConvexShape ( const ConvexShape )

Copy constructor.

◆ ConvexShape() [3/3]

ime::ConvexShape::ConvexShape ( ConvexShape &&  )
noexcept

Move constructor.

◆ ~ConvexShape()

ime::ConvexShape::~ConvexShape ( )
override

Destructor.

Member Function Documentation

◆ attachRigidBody()

void ime::Shape::attachRigidBody ( RigidBody::Ptr  body)
inherited

Attach a rigid body to a shape.

Parameters
bodyThe body to be attached

Attaching a rigid Body to a shape enables physics for that shape. This means that you should refrain from calling functions that MODIFY the shapes transform (position, rotation and origin). Note that the physics simulation does not account for scaling, that should be handles by you

Note
Attaching a rigid body will alter the origin of the shape to match the centre of mass of the body
Warning
The pointer must not be a nullptr. Also, you cannot attach a rigid body to a shape that already has a rigid body attached, the previous body must be removed first
See also
removeRigidBody

◆ clone()

Shape::Ptr ime::ConvexShape::clone ( ) const
overridevirtual

Make a copy of this shape.

Returns
A copy of this shape

You should use this function if you don't care about the type of the shape, otherwise use the shapes copy function

See also
copy

Implements ime::Shape.

◆ copy()

ConvexShape::Ptr ime::ConvexShape::copy ( ) const

Create a copy of this shape.

Returns
A copy of this shape
See also
clone

◆ create()

static ConvexShape::Ptr ime::ConvexShape::create ( std::size_t  pointCount = 0)
static

Create a new convex shape.

Parameters
pointCountThe number of points of the polygon
Returns
The created convex shape shape

◆ draw()

void ime::Shape::draw ( priv::RenderTarget &  renderTarget) const
overridevirtualinherited

Draw object on a render target.

Parameters
renderTargetTarget to draw object on
Note
This function is intended for internal use only

Implements ime::Drawable.

◆ emitChange()

void ime::Object::emitChange ( const Property property)
protectedinherited

Dispatch a property change event.

Parameters
propertyThe property that changed

This function will invoke all the event listeners of the specified property

See also
emit

◆ emitDestruction()

void ime::Object::emitDestruction ( )
protectedinherited

Emit a destruction event.

Note
This function must be the first statement in the definition of a destructor to avoid undefined behavior. In addition, note that destruction listeners are invoked once. Therefore, multiple classes in a hierarchy may call this function but the class that makes the call first will be the one that invokes the destruction listeners

◆ getClassName()

std::string ime::ConvexShape::getClassName ( ) const
overridevirtual

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

std::string ime::Shape::getClassType ( ) const
overridevirtualinherited

Get the name of this class.

Returns
The name of this class

Note that this function is only implemented by child classes of Object which also serve as a base class for other classes

See also
Object::getClassType and Object::getClassName

Reimplemented from ime::Drawable.

◆ getFillColour()

Colour ime::Shape::getFillColour ( ) const
inherited

Get the fill colour of the shape.

Returns
The fill colour of the shape

◆ getGlobalBounds()

FloatRect ime::Shape::getGlobalBounds ( ) const
inherited

Get the global bounding rectangle of the shape.

Returns
The global bounding rectangle of the shape

The returned rectangle is in global coordinates, which means that it takes into account the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the shape in the global 2D world's coordinate system.

◆ getLocalBounds()

FloatRect ime::Shape::getLocalBounds ( ) const
inherited

Get the local bounding rectangle of the shape.

Returns
The local bounding rectangle of the shape

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity

◆ getObjectId()

unsigned int ime::Object::getObjectId ( ) const
inherited

Get the unique id of the object.

Returns
The unique id of the object

Note that each instance of ime::Object has a unique id

See also
setTag

◆ getOrigin()

Vector2f ime::Shape::getOrigin ( ) const
overridevirtualinherited

Get the local origin of the shape.

Returns
Local origin of the shape

Implements ime::ITransformable.

◆ getOutlineColour()

Colour ime::Shape::getOutlineColour ( ) const
inherited

Get the outline colour of the shape.

Returns
The outline colour of the shape

◆ getOutlineThickness()

float ime::Shape::getOutlineThickness ( ) const
inherited

Get the outline thickness of the shape.

Returns
The outline thickness of the shape

◆ getPoint()

Vector2f ime::ConvexShape::getPoint ( std::size_t  index) const

Get the position of a point.

Parameters
indexIndex of the point to get, in range [0 .. getPointCount() - 1]
Returns
Position of the index-th point of the polygon

The returned point is in local coordinates, that is, the shape's transforms (position, rotation, scale) are not taken into account

Warning
The result is undefined if index is out of the valid range
See also
setPointCount

◆ getPointCount()

std::size_t ime::ConvexShape::getPointCount ( ) const

Get the number of points of the polygon.

Returns
The number of points of the polygon

◆ getPosition()

Vector2f ime::Shape::getPosition ( ) const
overridevirtualinherited

Get the position of the shape.

Returns
Current position of the shape

Implements ime::ITransformable.

◆ getRigidBody()

RigidBody * ime::Shape::getRigidBody ( )
inherited

Get the rigid body attached to the shape.

Returns
The rigid body attached to the shape or a nullptr if the shape does not have a rigid body attached to it

◆ getRotation()

float ime::Shape::getRotation ( ) const
overridevirtualinherited

Get the orientation of the shape.

Returns
Current rotation, in degrees

The rotation is always in the range [0, 360]

Implements ime::ITransformable.

◆ getScale()

Vector2f ime::Shape::getScale ( ) const
overridevirtualinherited

Get the current scale of the shape.

Returns
Current scale of the shape

Implements ime::ITransformable.

◆ getShapeType()

Type ime::Shape::getShapeType ( ) const
inherited

Get the type of this shape.

Returns
The type of the shape

◆ getTag()

const std::string & ime::Object::getTag ( ) const
inherited

Get the tag assigned to the object.

Returns
The tag of the object
See also
setTag

◆ getTexture()

Texture * ime::Shape::getTexture ( )
inherited

Get the shapes texture.

Returns
The shapes texture or a nullptr if there is no set texture
See also
setTexture

◆ hasRigidBody()

bool ime::Shape::hasRigidBody ( ) const
inherited

Check if the the shape has a rigid body attached to it or not.

Returns
True if the shape has a rigid body attached to it, otherwise false

◆ isEventListenerSuspended()

bool ime::Object::isEventListenerSuspended ( int  id) const
inherited

Check if an event listener is suspended or not.

Parameters
idThe identification number of the listener to be checked
Returns
True if suspended, otherwise false

This function also returns false if the specified event listener does not exist

See also
suspendedEventListener

◆ isSameObjectAs()

bool ime::Object::isSameObjectAs ( const Object other) const
inherited

Check if another object is the same instance as this object.

Parameters
otherThe object to compare against this object
Returns
True if other is the same instance as this object, otherwise false

◆ move() [1/2]

void ime::Shape::move ( const Vector2f offset)
overridevirtualinherited

Move the shape by a given offset.

Parameters
offsetOffset to apply

This function adds to the current position of the shape, unlike setPosition which overwrites it

See also
setPosition

Implements ime::ITransformable.

◆ move() [2/2]

void ime::Shape::move ( float  offsetX,
float  offsetY 
)
overridevirtualinherited

Move the shape by a given offset.

Parameters
offsetXHorizontal offset
offsetYVertical offset

This function adds to the current position of the shape, unlike setPosition which overwrites it

See also
setPosition

Implements ime::ITransformable.

◆ onDestruction()

int ime::Object::onDestruction ( const Callback<> &  callback)
inherited

Add a destruction listener.

Parameters
callbackFunction to be executed when the object is destroyed
Returns
The unique id of the destruction listener

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

Warning
It's not advised to call virtual functions in the destruction callback as some parts of the object may have already been destroyed by the time the callback is invoked. In such an event, the behavior is undefined
See also
removeEventListener

◆ onPropertyChange() [1/2]

int ime::Object::onPropertyChange ( const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to any property change event.

Parameters
callbackThe function to be executed when any property changes
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The unique id of the event listener

When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event

See also
onPropertyChange(const std::string&, const ime::Callback<ime::Property>&)

◆ onPropertyChange() [2/2]

int ime::Object::onPropertyChange ( const std::string &  property,
const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to add an event listener to
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

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.

// Prints the tag of the player object to the console everytime it changes
player.onPropertyChange("tag", [](const Property& property) {
cout << "New tag: " << property.getValue<std::string>() << endl;
});
...
//Sets tag = "player1" and invokes event listener(s)
player.setTag("player1");
See also
unsubscribe and onPropertyChange(const ime::Callback<ime::Property>&)

◆ operator=() [1/2]

ConvexShape & ime::ConvexShape::operator= ( const ConvexShape )

Copy assignment operator.

◆ operator=() [2/2]

ConvexShape & ime::ConvexShape::operator= ( ConvexShape &&  )
noexcept

Move assignment operator.

◆ removeEventListener() [1/2]

bool ime::Object::removeEventListener ( const std::string &  event,
int  id 
)
inherited

Remove an event listener from an event.

Parameters
eventThe name of the event to remove an event listener from
idThe unique id of the event listener to be removed
Returns
True if the event listener was removed or false if the event or the event listener is does not exist
// Display the tag of the object to console every time it changes
auto tagChangeId = object.onPropertyChange("tag", [](ime::Property tag) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the tag of the object when it changes
object.removeEventListener("tag", tagChangeId);
Class that can store a value of any type.
Definition: Property.h:38

◆ removeEventListener() [2/2]

bool ime::Object::removeEventListener ( int  id)
inherited

Remove an event listener.

Parameters
idThe id of the event listener to be removed
Returns
True if the event listener was removed or false if no such handler exists

◆ removeRigidBody()

void ime::Shape::removeRigidBody ( )
inherited

Remove a rigid body from the shape.

Removing a rigid Body from a shape disables all physics applied to the shape. This means that the shape will not respond to forces ans must be moved and rotated manually if need be

◆ rotate()

void ime::Shape::rotate ( float  angle)
overridevirtualinherited

Rotate the shape.

Parameters
angleAngle of rotation, in degrees

This function adds to the current rotation of the shape, unlike setRotation which overwrites it

See also
setRotation

Implements ime::ITransformable.

◆ scale() [1/2]

void ime::Shape::scale ( const Vector2f factor)
overridevirtualinherited

Scale the shape by an offset.

Parameters
factorOffset to apply

This function multiplies the current scale of the shape, unlike setScale which overwrites it

See also
setScale

Implements ime::ITransformable.

◆ scale() [2/2]

void ime::Shape::scale ( float  factorX,
float  factorY 
)
overridevirtualinherited

Scale the shape by an offset.

Parameters
factorXHorizontal scale factor
factorYVertical scale factor

This function multiplies the current scale of the shape, unlike setScale which overwrites it

See also
setScale

Implements ime::ITransformable.

◆ setFillColour()

void ime::Shape::setFillColour ( const Colour colour)
inherited

Set the fill colour of the shape.

Parameters
colourThe new colour of the shape

By default, the shape's fill colour is opaque white.

◆ setOrigin() [1/2]

void ime::Shape::setOrigin ( const Vector2f origin)
overridevirtualinherited

Set the local origin of the shape.

Parameters
originNew origin

The origin of the shape 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 shape, and ignore all transformations (position, scale, rotation).

The default origin of the shape is (0, 0)

Implements ime::ITransformable.

◆ setOrigin() [2/2]

void ime::Shape::setOrigin ( float  x,
float  y 
)
overridevirtualinherited

Set the local origin of the shape.

Parameters
xX coordinate of the new origin
yY coordinate of the new origin

The origin of the shape 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 shape, and ignore all transformations (position, scale, rotation).

The default origin of the shape is (0, 0)

Implements ime::ITransformable.

◆ setOutlineColour()

void ime::Shape::setOutlineColour ( const Colour colour)
inherited

Set the outline colour of the shape.

Parameters
colourThe new outline colour

By default, the shape's outline colour is opaque white.

◆ setOutlineThickness()

void ime::Shape::setOutlineThickness ( float  thickness)
inherited

Set the thickness of the shape's outline.

Parameters
thicknessThe new outline thickness

Note that negative values are allowed (so that the outline expands towards the center of the shape), and using zero disables the outline.

By default, the outline thickness is 0.

◆ setPoint()

void ime::ConvexShape::setPoint ( std::size_t  index,
const Vector2f point 
)

Set the position of a point.

Parameters
indexIndex of the point to change, in range [0 .. getPointCount() - 1]
pointNew position of the point
Warning
The polygon must remain convex, and the points need to stay ordered!. setPointCount must be called first in order to set the total number of points. The result is undefined if index is out of the valid range
See also
getPointCount

◆ setPointCount()

void ime::ConvexShape::setPointCount ( std::size_t  count)

Set the number of points of the polygon.

Parameters
countThe new number of points of the polygon
Note
count must be greater than 2 to define a valid shape

◆ setPosition() [1/2]

void ime::Shape::setPosition ( const Vector2f position)
overridevirtualinherited

Set the position of the shape.

Parameters
positionNew 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 shape is (0, 0)

See also
move

Implements ime::ITransformable.

◆ setPosition() [2/2]

void ime::Shape::setPosition ( float  x,
float  y 
)
overridevirtualinherited

Set the position of the shape.

Parameters
xX coordinate of the new position
yY 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 shape is (0, 0)

See also
move

Implements ime::ITransformable.

◆ setRotation()

void ime::Shape::setRotation ( float  angle)
overridevirtualinherited

Set the orientation of the shape.

Parameters
angleNew 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 shape is 0

See also
rotate

Implements ime::ITransformable.

◆ setScale() [1/2]

void ime::Shape::setScale ( const Vector2f scale)
overridevirtualinherited

Set the scale factor of the shape.

Parameters
scaleNew scale

This function completely overwrites the previous scale

See also
scale

Implements ime::ITransformable.

◆ setScale() [2/2]

void ime::Shape::setScale ( float  factorX,
float  factorY 
)
overridevirtualinherited

Set the scale factors of the shape.

Parameters
factorXNew horizontal scale factor
factorYNew vertical scale factor

This function completely overwrites the previous scale

See also
scale

Implements ime::ITransformable.

◆ setTag()

void ime::Object::setTag ( const std::string &  tag)
inherited

Assign the object an alias.

Parameters
tagThe 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

See also
getObjectId

◆ setTexture() [1/2]

void ime::Shape::setTexture ( const std::string &  filename)
inherited

Set the texture of the shape.

Parameters
filenameThe file name of the texture to be set
Exceptions
FileNotFoundExceptionif the filename cannot be found on the disk
See also
getTexture

◆ setTexture() [2/2]

void ime::Shape::setTexture ( const Texture texture)
inherited

Set the texture of the shape from a source texture.

Parameters
textureThe source texture

Note that the source texture is copied

See also
getTexture

◆ suspendedEventListener()

void ime::Object::suspendedEventListener ( int  id,
bool  suspend 
)
inherited

Pause or resume execution of an event listener.

Parameters
idThe event listeners unique identification number
suspendTrue to suspend/pause or false to unsuspend/resume
See also
isEventListenerSuspended

Member Data Documentation

◆ eventEmitter_

EventEmitter ime::Object::eventEmitter_
protectedinherited

Event dispatcher.

Definition at line 289 of file Object.h.


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