Loading...
Searching...
No Matches
ml::Positionable Class Referenceabstract

Trait that provides position, bounds, and animated movement. More...

#include <Positionable.h>

Inheritance diagram for ml::Positionable:

Public Member Functions

template<class T>
void center (T &obj)
 Center this object within obj (both axes).
template<class T>
void centerHorizonally (T &obj)
 Center this object horizontally within obj.
void centerText (sf::Text &obj)
 Center an sf::Text object within this object's bounds.
template<class T>
void centerVertically (T &obj)
 Center this object vertically within obj.
virtual sf::FloatRect getGlobalBounds () const =0
 Return the axis-aligned bounding box in world space.
virtual sf::Vector2f getPosition () const =0
 Return the current world-space position.
bool isScrolling ()
 Return true while a moveTo or moveDistance animation is in progress.
void moveDistance (sf::Vector2f distance, float seconds=1.f)
 Animate by a relative offset from the current position.
void moveTo (sf::FloatRect position, float seconds=1.f)
 Animate to a position defined by a FloatRect origin.
void moveTo (sf::Vector2f position, float seconds=1.f)
 Animate to an absolute world-space position.
template<class T>
void setAbove (T &obj, float spacing=0)
 Position this object immediately above obj.
template<class T>
void setBelow (T &obj, float spacing=0)
 Position this object immediately below obj.
void setFramerate (float framerate)
 Set the assumed framerate for movement interpolation.
template<class T>
void setLeftOf (T &obj, float spacing=0)
 Position this object immediately to the left of obj.
virtual void setPosition (const sf::Vector2f &position)=0
 Set the world-space position immediately (no animation).
template<class T>
void setRightOf (T &obj, float spacing=0)
 Position this object immediately to the right of obj.

Detailed Description

Trait that provides position, bounds, and animated movement.

Positionable is one of the three core traits on every ml::Core object. It serves two purposes:

  1. Spatial contract — declares the pure virtual setPosition, getPosition, and getGlobalBounds methods that the rest of the framework (hit-testing, layout, drag) depend on.
  2. Animated movement — provides moveTo and moveDistance for tweened position animation, with support for axis locking, easing curves, and a configurable framerate target.

Immediate positioning

// Implemented by the concrete shape/component class
myRect.setPosition({100.f, 200.f});
sf::Vector2f pos = myRect.getPosition();
sf::FloatRect bounds = myRect.getGlobalBounds();
Rect< float > FloatRect
Vector2< float > Vector2f

Animated movement

// Animate to an absolute position over 0.5 seconds
myRect.moveTo({400.f, 300.f}, 0.5f);
// Animate by a relative offset over 1 second (default)
myRect.moveDistance({50.f, 0.f});
// Poll whether animation is still running
if (myRect.isScrolling()) { ... }

Relative layout helpers

labelB.setRightOf(labelA, 10.f); // place B 10px to the right of A
button.setBelow(header, 20.f); // place button 20px below header
icon.center(panel); // center icon inside panel
icon.centerHorizonally(panel); // center horizontally only
See also
Draggable, CoreAdapter, Component

Definition at line 66 of file Positionable.h.

Member Function Documentation

◆ center()

template<class T>
void ml::Positionable::center ( T & obj)

Center this object within obj (both axes).

Template Parameters
TAny type with getGlobalBounds().
Parameters
objContainer to center within.

◆ centerHorizonally()

template<class T>
void ml::Positionable::centerHorizonally ( T & obj)

Center this object horizontally within obj.

Template Parameters
TAny type with getGlobalBounds().
Parameters
objContainer to center within.

◆ centerText()

void ml::Positionable::centerText ( sf::Text & obj)

Center an sf::Text object within this object's bounds.

Convenience overload for the common pattern of centering a text label inside a button or panel.

Parameters
objThe sf::Text to reposition.

◆ centerVertically()

template<class T>
void ml::Positionable::centerVertically ( T & obj)

Center this object vertically within obj.

Template Parameters
TAny type with getGlobalBounds().
Parameters
objContainer to center within.

◆ getGlobalBounds()

virtual sf::FloatRect ml::Positionable::getGlobalBounds ( ) const
pure virtual

Return the axis-aligned bounding box in world space.

Used by UIManager for hit-testing (click, hover) and by layout helpers for relative placement. Pure virtual — implemented by the concrete shape or adapter class.

Returns
Bounding rectangle in world coordinates.

Implemented in ml::CoreAdapter, ml::DrawableWrapper< ENTITY >, ml::Graphic< T >, ml::Graphic< ml::RoundedRectangle >, ml::Graphic< sf::CircleShape >, ml::Graphic< sf::ConvexShape >, ml::Graphic< sf::Sprite >, ml::Graphic< sf::Text >, ml::Grid, and ml::TextInput.

◆ getPosition()

virtual sf::Vector2f ml::Positionable::getPosition ( ) const
pure virtual

Return the current world-space position.

Pure virtual — implemented by the concrete shape or adapter class.

Returns
Current position in world coordinates.

Implemented in ml::CoreAdapter, ml::DrawableWrapper< ENTITY >, ml::Graphic< T >, ml::Graphic< ml::RoundedRectangle >, ml::Graphic< sf::CircleShape >, ml::Graphic< sf::ConvexShape >, ml::Graphic< sf::Sprite >, ml::Graphic< sf::Text >, ml::Grid, and ml::TextInput.

◆ isScrolling()

bool ml::Positionable::isScrolling ( )

Return true while a moveTo or moveDistance animation is in progress.

Returns
true if the waypoint queue is non-empty.

◆ moveDistance()

void ml::Positionable::moveDistance ( sf::Vector2f distance,
float seconds = 1.f )

Animate by a relative offset from the current position.

Equivalent to moveTo(getPosition() + distance, seconds).

Parameters
distanceOffset in world coordinates to move by.
secondsDuration of the animation. Defaults to 1.0 s.

◆ moveTo() [1/2]

void ml::Positionable::moveTo ( sf::FloatRect position,
float seconds = 1.f )

Animate to a position defined by a FloatRect origin.

Convenience overload that uses the top-left of position as the target. Useful when positioning relative to layout rects.

Parameters
positionRect whose origin is the animation target.
secondsDuration of the animation. Defaults to 1.0 s.

◆ moveTo() [2/2]

void ml::Positionable::moveTo ( sf::Vector2f position,
float seconds = 1.f )

Animate to an absolute world-space position.

Populates an internal waypoint queue and advances through it on successive onUpdate ticks. The object's position at the start of the call is the implicit start point.

Parameters
positionTarget position in world coordinates.
secondsDuration of the animation. Defaults to 1.0 s.

◆ setAbove()

template<class T>
void ml::Positionable::setAbove ( T & obj,
float spacing = 0 )

Position this object immediately above obj.

Template Parameters
TAny type with getGlobalBounds().
Parameters
objReference object.
spacingGap in pixels between the two objects. Defaults to 0.

◆ setBelow()

template<class T>
void ml::Positionable::setBelow ( T & obj,
float spacing = 0 )

Position this object immediately below obj.

Template Parameters
TAny type with getGlobalBounds().
Parameters
objReference object.
spacingGap in pixels between the two objects. Defaults to 0.

◆ setFramerate()

void ml::Positionable::setFramerate ( float framerate)

Set the assumed framerate for movement interpolation.

moveTo generates a queue of intermediate positions based on this value. Defaults to 60 fps. Call this if your application runs at a different target rate to keep animation timing accurate.

Parameters
framerateTarget framerate in frames per second.

◆ setLeftOf()

template<class T>
void ml::Positionable::setLeftOf ( T & obj,
float spacing = 0 )

Position this object immediately to the left of obj.

Template Parameters
TAny type with getGlobalBounds().
Parameters
objReference object.
spacingGap in pixels between the two objects. Defaults to 0.

◆ setPosition()

virtual void ml::Positionable::setPosition ( const sf::Vector2f & position)
pure virtual

Set the world-space position immediately (no animation).

Pure virtual — implemented by the concrete shape or adapter class.

Parameters
positionNew position in world coordinates.

Implemented in ml::Button< Circle, float >, ml::Button< Convex, std::size_t >, ml::Button< Rectangle, sf::Vector2f >, ml::CoreAdapter, ml::DrawableWrapper< ENTITY >, ml::Graphic< T >, ml::Graphic< ml::RoundedRectangle >, ml::Graphic< sf::CircleShape >, ml::Graphic< sf::ConvexShape >, ml::Graphic< sf::Sprite >, ml::Graphic< sf::Text >, ml::Grid, and ml::TextInput.

◆ setRightOf()

template<class T>
void ml::Positionable::setRightOf ( T & obj,
float spacing = 0 )

Position this object immediately to the right of obj.

Template Parameters
TAny type with getGlobalBounds().
Parameters
objReference object.
spacingGap in pixels between the two objects. Defaults to 0.

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