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

A closed 2D curved shape. More...

#include <CircleShape.h>

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

Public Types

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

Public Member Functions

 CircleShape (float radius=0.0f)
 Default constructor. More...
 
 CircleShape (const CircleShape &)
 Copy constructor. More...
 
CircleShapeoperator= (const CircleShape &)
 Copy assignment operator. More...
 
 CircleShape (CircleShape &&) noexcept
 Move constructor. More...
 
CircleShapeoperator= (CircleShape &&) noexcept
 Move assignment operator. More...
 
CircleShape::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 setRadius (float radius)
 Set the radius of the circle. More...
 
float getRadius () const
 Get the radius of the circle. More...
 
 ~CircleShape () 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 (Body::Ptr body)
 Attach a rigid body to a shape. More...
 
void removeRigidBody ()
 Remove a rigid body from the shape. More...
 
Body::Ptr getRigidBody ()
 Get the rigid body attached to the shape. More...
 
Body::Ptr getRigidBody () const
 
bool hasRigidBody () const
 Check if the the shape has a rigid body attached to it or not. More...
 
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 (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 (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 (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 (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 (Vector2f offset) override
 Move the shape by a given offset. More...
 
void draw (Window &renderTarget) const override
 Draw the shape on a render target. More...
 
void setTag (const std::string &tag)
 Assign the object an alias. More...
 
const std::string & getTag () const
 Get the alias of the object. More...
 
unsigned int getObjectId () const
 Get the id of the object. More...
 
int onPropertyChange (const std::string &property, const Callback< Property > &callback)
 Add an event listener to a specific property change event. More...
 
void onPropertyChange (const Callback< Property > &callback)
 Add an event listener to a property change event. More...
 
int onEvent (const std::string &event, const Callback<> &callback)
 Add an event listener to an event. More...
 
bool unsubscribe (const std::string &event, int id)
 Remove an event listener from an event. More...
 
int onDestruction (const Callback<> &callback)
 Add a destruction listener. More...
 
bool removeDestructionListener (int id)
 Remove a destruction listener form the object. More...
 
bool operator== (const Object &rhs) const
 Check if two objects are the same object or not. More...
 
bool operator!= (const Object &rhs) const
 Check if two objects are not the same object. More...
 

Static Public Member Functions

static CircleShape::Ptr create (float radius=0.0f)
 Create a new circle shape. More...
 

Protected Member Functions

std::shared_ptr< void > getInternalPtr () const
 
void emitChange (const Property &property)
 Dispatch a property change event. More...
 
void emit (const std::string &event)
 Dispatch an action event. More...
 

Detailed Description

A closed 2D curved shape.

Definition at line 36 of file CircleShape.h.

Member Typedef Documentation

◆ Ptr

using ime::CircleShape::Ptr = std::shared_ptr<CircleShape>

Shared shape pointer.

Definition at line 38 of file CircleShape.h.

Member Enumeration Documentation

◆ Type

enum ime::Shape::Type
stronginherited

The types of shapes.

Enumerator
Rectangle 

Rectangle shape.

Circle 

Circle shape.

Convex 

Convex polygon shape.

Definition at line 53 of file Shape.h.

Constructor & Destructor Documentation

◆ CircleShape() [1/3]

ime::CircleShape::CircleShape ( float  radius = 0.0f)
explicit

Default constructor.

Parameters
radiusThe radius of the circle

By default the radius is zero

◆ CircleShape() [2/3]

ime::CircleShape::CircleShape ( const CircleShape )

Copy constructor.

◆ CircleShape() [3/3]

ime::CircleShape::CircleShape ( CircleShape &&  )
noexcept

Move constructor.

◆ ~CircleShape()

ime::CircleShape::~CircleShape ( )
override

Destructor.

Member Function Documentation

◆ attachRigidBody()

void ime::Shape::attachRigidBody ( Body::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::CircleShape::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()

CircleShape::Ptr ime::CircleShape::copy ( ) const

Create a copy of this shape.

Returns
A copy of this shape
See also
clone

◆ create()

static CircleShape::Ptr ime::CircleShape::create ( float  radius = 0.0f)
static

Create a new circle shape.

Parameters
radiusThe radius of the circle
Returns
The created circle shape

◆ draw()

void ime::Shape::draw ( Window renderTarget) const
overridevirtualinherited

Draw the shape on a render target.

Parameters
renderTargetTarget to draw object on

Implements ime::Drawable.

◆ emit()

void ime::Object::emit ( const std::string &  event)
protectedinherited

Dispatch an action event.

Parameters
eventThe name of the event to be dispatched

This function will invoke all event listeners of the specified event. The function should be used for events that represent an action, rather than those that represent a property change (Use emitChange for that)

See also
emitChange

◆ 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

◆ getClassName()

std::string ime::CircleShape::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 id of the object.

Returns
The id of the object

Each object has a unique id

◆ 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

◆ getPosition()

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

Get the position of the shape.

Returns
Current position of the shape

Implements ime::ITransformable.

◆ getRadius()

float ime::CircleShape::getRadius ( ) const

Get the radius of the circle.

Returns
The radius of teh circle

◆ getRigidBody()

Body::Ptr 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 alias of the object.

Returns
The alias of the object
See also
setTag and getObjectId

◆ 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

◆ move() [1/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.

◆ move() [2/2]

void ime::Shape::move ( 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.

◆ 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 id of the destruction listener

Note that an object may have more than one destruction listeners, however, you have to keep the returned id if you may want to remove the callback at a later time

Warning
The callback is called when the object is destroyed. Do not try to access the object in the callback. Doing so is undefined behavior
See also
removeDestructionListener

◆ onEvent()

int ime::Object::onEvent ( const std::string &  event,
const Callback<> &  callback 
)
inherited

Add an event listener to an event.

Parameters
eventThe name of the event to add an an event listener to
callbackThe function to be executed when the event takes place
Returns
The unique identification number of the event listener

Unlike onPropertyChange, this function registers event listeners to events that occur when something happens to the object, or when the object does something (action events). Usually the name of the event/action is the name of the function:

// Add event listeners to the object
object.onEvent("attachRigidBody", [] {
std::cout << "Rigid body attached to object << std::endl;
});
object.onEvent("removeRigidBody", [] {
std::cout << "Rigid body removed from object << std::endl;
});
// Invokes event listener(s)
object.attachRigidBody(body);
object.removeRigidBody();
See also
onPropertyChange and unsubscribe

◆ onPropertyChange() [1/2]

void ime::Object::onPropertyChange ( const Callback< Property > &  callback)
inherited

Add an event listener to a property change event.

Parameters
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

Note that only one callback function may be registered with this function. This means that adding a new event listener overwrites the previous event listener. To remove the callback, pass a nullptr as an argument. The function may be useful if you want to write the logic for property changes in one function.

See also
onPropertyChange(std::string, Callback)

◆ onPropertyChange() [2/2]

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

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to listen for
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, for the setTag function, the property that the function modifies is Tag.

Note that 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");

Unlike onPropertyChange(const Callback&) you can add multiple event listeners to the same property using this function. However you must store the unique id of the event listener if you wish to remove it at a later time

See also
unsubscribe and onPropertyChange(Callback)

◆ operator!=()

bool ime::Object::operator!= ( const Object rhs) const
inherited

Check if two objects are not the same object.

Parameters
rhsObject to compare against this object
Returns
True if rhs is NOT the same object as this object, otherwise false

Two objects are different from each other if they have different object id's

See also
getObjectId and operator==

◆ operator=() [1/2]

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

Move assignment operator.

◆ operator=() [2/2]

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

Copy assignment operator.

◆ operator==()

bool ime::Object::operator== ( const Object rhs) const
inherited

Check if two objects are the same object or not.

Parameters
rhsObject to compare against this object
Returns
True if rhs is the same object as this object, otherwise false

Two objects are the same object if they have the same object id. Recall that each object instance has a unique id

See also
getObjectId and operator!=

◆ removeDestructionListener()

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

Remove a destruction listener form the object.

Parameters
Theid of the destruction listener to be removed
Returns
True if the destruction listener was removed or false if the listener with the given id does not exist

◆ 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 ( 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.

◆ scale() [2/2]

void ime::Shape::scale ( 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.

◆ 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 ( 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.

◆ setOrigin() [2/2]

void ime::Shape::setOrigin ( 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.

◆ 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.

◆ setPosition() [1/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.

◆ setPosition() [2/2]

void ime::Shape::setPosition ( 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.

◆ setRadius()

void ime::CircleShape::setRadius ( float  radius)

Set the radius of the circle.

Parameters
radiusThe new radius of the circle

◆ 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 ( 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.

◆ setScale() [2/2]

void ime::Shape::setScale ( 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.

◆ setTag()

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

Assign the object an alias.

Parameters
nameThe alias of the object

This function is useful if you want to refer to the object by tag instead of its id. Unlike an object id, multiple objects may have the same tag.

By default, the tag is an empty string

◆ unsubscribe()

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

Remove an event listener from an event.

Parameters
eventThe name of the event to remove 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 name of the object to console every time it changes
auto nameChangeId = object.onPropertyChange("name", [](Property name) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the name of the object when it changes
object.unsubscribe("name", id);

The documentation for this class was generated from the following file:
ime::Shape::removeRigidBody
void removeRigidBody()
Remove a rigid body from the shape.