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

Stores and manages a scene's render layers. More...

#include <RenderLayerContainer.h>

Inheritance diagram for ime::RenderLayerContainer:
ime::Object

Public Types

using Callback = std::function< void(const RenderLayer::Ptr &)>
 Callback. More...
 
using Ptr = std::shared_ptr< Object >
 Shared object pointer. More...
 

Public Member Functions

 RenderLayerContainer (const RenderLayerContainer &)=delete
 Copy constructor. More...
 
RenderLayerContaineroperator= (const RenderLayerContainer &)=delete
 Copy assignment operator. More...
 
 RenderLayerContainer (RenderLayerContainer &&) noexcept=default
 Move constructor. More...
 
RenderLayerContaineroperator= (RenderLayerContainer &&) noexcept=default
 Move assignment operator. 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...
 
virtual std::string getClassType () const
 Get the name of the direct parent of an object instance. 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...
 

Protected Member Functions

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

The name of the layer to be created

Create a layer

Returns
The created layer

Note that the container keeps a pointer to the layer after it is created, therefore you don't need to keep the returned pointer alive after using it

Warning
The name of the layer must be unique, that is, another layer must not exist in the container with the same name as name otherwise it is undefined behaviour
See also
removeByTag, removeByIndex and removeAll
class Scene
 

Needs access to the constructor

More...
 
class TileMap
 
RenderLayer::Ptr create (const std::string &name)
 
void add (const Drawable &drawable, int renderOrder=0u, const std::string &renderLayer="default")
 Add a drawable object to a render layer in the container. More...
 
std::string getClassName () const override
 Get the name of this class. More...
 
RenderLayer::Ptr front () const
 Get the render layer at the front. More...
 
RenderLayer::Ptr back () const
 Get the render layer at the back. More...
 
RenderLayer::Ptr findByIndex (unsigned int index) const
 Get the layer at a given index. More...
 
RenderLayer::Ptr findByName (const std::string &name) const
 Get a layer with the given name. More...
 
bool isIndexValid (unsigned int index) const
 Check if a given index is within bounds or not. More...
 
bool hasLayer (const std::string &name) const
 Check if the container has a layer with the given name. More...
 
bool removeByIndex (unsigned int index)
 Remove the layer at the given index. More...
 
bool removeByName (const std::string &name)
 Remove the layer with the given name from the container. More...
 
void removeAll ()
 Remove all layers from the container. More...
 
void moveUp (unsigned int index)
 Move a layer one level up. More...
 
void moveUp (const std::string &name)
 Move the layer up one level. More...
 
void moveDown (unsigned int index)
 Move a layer one level down. More...
 
void moveDown (const std::string &name)
 Move a layer one level down. More...
 
void moveToFront (unsigned int index)
 Move a layer to the highest level (foreground) More...
 
void moveToFront (const std::string &name)
 Move a layer to the highest level (foreground) More...
 
void sendToBack (unsigned int index)
 Move a layer to the lowest level (background) More...
 
void sendToBack (const std::string &name)
 Move a layer to the lowest level (background) More...
 
bool swap (unsigned int layerOneIndex, unsigned int layerTwoIndex)
 Swap the render positions of two layers. More...
 
void swap (const std::string &layerOne, const std::string &layerTwo)
 Swap the positions of two layers. More...
 
std::size_t getCount () const
 Get the number of layer in the container. More...
 
void forEachLayer (const Callback &callback)
 Execute a callback for each layer in the container. More...
 
void render (Window &window) const
 
 ~RenderLayerContainer () override
 Destructor. More...
 

Detailed Description

Stores and manages a scene's render layers.

A render layer container determines the order in which objects are drawn to the render window. After creating an object (e.g sprite), you specify the RenderLayer in which the object belongs to and the object will be rendered on top or below other objects depending on the index of its render layer in the render layer container.

Render layers are added to the container in ascending order with an index of 0 representing the the furthest background. Therefore the last added render layer will always be the foreground. The layers are drawn from index 0 going upwards. Note that you cannot change the index of a layer (indexes will always be arranged in ascending order), however you can change the layer at a given index. This means that you can specify which layer gets rendered on top of which layer using appropriate member function. For example if you add a mountain sprite to a render layer at index 5 and a tree sprite to a render layer at index 6, then the mountain will be rendered first followed by the tree and vice versa.

This class is not directly instantiatable, it is created when you instantiate a Scene object. Each scene instance has its own render layers and a single render layer container

Definition at line 59 of file RenderLayerContainer.h.

Member Typedef Documentation

◆ Callback

using ime::RenderLayerContainer::Callback = std::function<void(const RenderLayer::Ptr&)>

Callback.

Definition at line 61 of file RenderLayerContainer.h.

◆ Ptr

using ime::Object::Ptr = std::shared_ptr<Object>
inherited

Shared object pointer.

Definition at line 45 of file Object.h.

Constructor & Destructor Documentation

◆ RenderLayerContainer() [1/2]

ime::RenderLayerContainer::RenderLayerContainer ( const RenderLayerContainer )
delete

Copy constructor.

◆ RenderLayerContainer() [2/2]

ime::RenderLayerContainer::RenderLayerContainer ( RenderLayerContainer &&  )
defaultnoexcept

Move constructor.

◆ ~RenderLayerContainer()

ime::RenderLayerContainer::~RenderLayerContainer ( )
override

Destructor.

Member Function Documentation

◆ add()

void ime::RenderLayerContainer::add ( const Drawable drawable,
int  renderOrder = 0u,
const std::string &  renderLayer = "default" 
)

Add a drawable object to a render layer in the container.

Parameters
drawableThe drawable object to be added
renderOrderThe render order of the object in the render layer
renderLayerThe RenderLayer to add the object belongs to

If the render layer is unspecified or the specified layer cannot be found then the drawable will be added to the 'default' layer. The 'default' render layer is created by the Scene when you instantiate it. Note that the 'default' layer may be deleted from the container, however you must make sure that the layer you specify during a call to this function already exists in the container otherwise undefined behavior

Note
You should only use this function if you don't want to the scene to keep an instance of the drawable on your behalf, otherwise you should use the container classes found in the Scene class, the drawable will be automatically added to the specified render layer

◆ back()

RenderLayer::Ptr ime::RenderLayerContainer::back ( ) const

Get the render layer at the back.

Returns
The render layer at the back or a nullptr if the container is empty
See also
front

◆ 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

◆ findByIndex()

RenderLayer::Ptr ime::RenderLayerContainer::findByIndex ( unsigned int  index) const

Get the layer at a given index.

Parameters
indexThe index of the layer to retrieve
Returns
The index at the specified index or a nullptr if the index is out of bounds
See also
findByTag

◆ findByName()

RenderLayer::Ptr ime::RenderLayerContainer::findByName ( const std::string &  name) const

Get a layer with the given name.

Parameters
nameThe name of the layer to retrieve
Returns
The layer with the given name or a nullptr if the layer does not exits
See also
findByIndex

◆ forEachLayer()

void ime::RenderLayerContainer::forEachLayer ( const Callback callback)

Execute a callback for each layer in the container.

Parameters
callbackThe callback to be executed

◆ front()

RenderLayer::Ptr ime::RenderLayerContainer::front ( ) const

Get the render layer at the front.

Returns
The layer at the front or a nullptr if the container is empty
See also
back

◆ getClassName()

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

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

virtual std::string ime::Object::getClassType ( ) const
virtualinherited

Get the name of the direct parent of an object instance.

Returns
The name of the direct parent of an object instance

In contrast to getClassName which returns the name of the concrete class, this function returns the name of the concrete class's base class. This function is implemented by all derived classes of Object which also serve as base classes for other Objects. For classes whose direct parent is this class, this function will return the name of this class

auto rectangle = ime::RectangleShape(); // RectangleShape is derived from Shape
std::cout << rectangle->getClassName(); // Prints "RectangleShape"
std::cout << rectangle->getClassType(); // Prints "Shape"
See also
getClassName

Reimplemented in ime::SpriteImage, ime::Shape, ime::Drawable, ime::GridMover, ime::Joint, ime::Collider, and ime::GameObject.

◆ getCount()

std::size_t ime::RenderLayerContainer::getCount ( ) const

Get the number of layer in the container.

Returns
The number of layers in the container

◆ 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

◆ 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

◆ hasLayer()

bool ime::RenderLayerContainer::hasLayer ( const std::string &  name) const

Check if the container has a layer with the given name.

Parameters
nameThe name of the layer to be checked
Returns
True of the container has the layer otherwise false
See also
findByTag and removeByTag

◆ isIndexValid()

bool ime::RenderLayerContainer::isIndexValid ( unsigned int  index) const

Check if a given index is within bounds or not.

Parameters
indexThe index to be checked
Returns
True of the container has the layer otherwise false
See also
findByIndex and removeByIndex

◆ moveDown() [1/2]

void ime::RenderLayerContainer::moveDown ( const std::string &  name)

Move a layer one level down.

Parameters
nameThe name of the layer to be moved

This function has no effect if the layer does not exist or the layer is already at the lowest level

See also
moveUp(const std::string&)

◆ moveDown() [2/2]

void ime::RenderLayerContainer::moveDown ( unsigned int  index)

Move a layer one level down.

Parameters
indexThe index of the layer to be moved

This function has no effect if the layer does not exist or the layer is already at the lowest level

See also
moveUp(unsigned int)

◆ moveToFront() [1/2]

void ime::RenderLayerContainer::moveToFront ( const std::string &  name)

Move a layer to the highest level (foreground)

Parameters
nameThe name of the level to be moved

This function has no effect if the layer does not exist or the layer is already at the highest level

See also
sendToBack(const std::string&)

◆ moveToFront() [2/2]

void ime::RenderLayerContainer::moveToFront ( unsigned int  index)

Move a layer to the highest level (foreground)

Parameters
indexThe index of the layer to be moved

This function has no effect if the layer does not exist or the layer is already at the highest level

See also
sendToBack(unsigned int)

◆ moveUp() [1/2]

void ime::RenderLayerContainer::moveUp ( const std::string &  name)

Move the layer up one level.

Parameters
nameThe name of the layer to be moved

This function has no effect if the layer does not exist

See also
moveDown(const std::string&)

◆ moveUp() [2/2]

void ime::RenderLayerContainer::moveUp ( unsigned int  index)

Move a layer one level up.

Parameters
indexThe index of the layer to be moved

This function has no effect if the index is out of bounds or the layer is already at the highest level

See also
moveDown(unsigned int)

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

RenderLayerContainer& ime::RenderLayerContainer::operator= ( const RenderLayerContainer )
delete

Copy assignment operator.

◆ operator=() [2/2]

RenderLayerContainer& ime::RenderLayerContainer::operator= ( RenderLayerContainer &&  )
defaultnoexcept

Move 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!=

◆ removeAll()

void ime::RenderLayerContainer::removeAll ( )

Remove all layers from the container.

See also
removeByIndex and removeByTag

◆ removeByIndex()

bool ime::RenderLayerContainer::removeByIndex ( unsigned int  index)

Remove the layer at the given index.

Parameters
indexThe index of the layer to be removed
Returns
True if the layer was removed or false if the index is out of bounds
See also
removeByTag

◆ removeByName()

bool ime::RenderLayerContainer::removeByName ( const std::string &  name)

Remove the layer with the given name from the container.

Parameters
nameThe name of the container to be removed
Returns
True if the layer was removed or false if the layer does not exist
See also
removeByIndex

◆ 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

◆ sendToBack() [1/2]

void ime::RenderLayerContainer::sendToBack ( const std::string &  name)

Move a layer to the lowest level (background)

Parameters
nameThe name of the layer ot be moved

This function has no effect if the layer does not exist or the layer is already at the lowest level

See also
moveToFront(const std::string&)

◆ sendToBack() [2/2]

void ime::RenderLayerContainer::sendToBack ( unsigned int  index)

Move a layer to the lowest level (background)

Parameters
indexThe index of the layer ot be moved

This function has no effect if the layer does not exist or the layer is already at the lowest level

See also
moveToFront(unsigned int)

◆ 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

◆ swap() [1/2]

void ime::RenderLayerContainer::swap ( const std::string &  layerOne,
const std::string &  layerTwo 
)

Swap the positions of two layers.

Parameters
layerOneThe name of the first layer
layerTwoThe name iof the second layer

This function has no effect of either of the layers cannot be found

See also
swap(unsigned int, unsigned int)

◆ swap() [2/2]

bool ime::RenderLayerContainer::swap ( unsigned int  layerOneIndex,
unsigned int  layerTwoIndex 
)

Swap the render positions of two layers.

Parameters
layerOneIndexThe index of the first layer
layerTwoIndexThe index of the second layer
Returns
True if the two layers were swapped successfully, or false if either one of the given layers does not exist in the container
See also
swap(const std::string&, const std::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);

Friends And Related Function Documentation

◆ Scene

friend class Scene
friend

Needs access to the constructor

Definition at line 359 of file RenderLayerContainer.h.


The documentation for this class was generated from the following file:
ime::RectangleShape
A 2D shape having four sides and four corners (90 degree angles)
Definition: RectangleShape.h:36