Loading...
Searching...
No Matches
ml::Draggable Class Reference

Receiver trait that adds mouse-drag behavior to any Component. More...

#include <Draggable.h>

Inheritance diagram for ml::Draggable:

Public Types

using Flag = DraggableManifest::Flag
using manifest_type
 The manifest type, used by GatherFlags and GatherStates to locate this class's flag and state declarations.
using State = DraggableManifest::State

Public Member Functions

 Draggable ()=default
virtual ~Draggable ()=default
void clearDragBounds ()
 Remove any previously set drag bounds.
std::enable_if_t<!std::is_void_v< T >, T > getState () const
 Return the current state value.
std::enable_if_t<!std::is_void_v< T >, bool > isState (T state) const
 Return true if currently in state.
std::enable_if_t<!std::is_void_v< T > > onStateEnter (std::function< void(T)> callback)
 Register a callback invoked after each state transition.
std::enable_if_t<!std::is_void_v< T > > onStateExit (std::function< void(T)> callback)
 Register a callback invoked before each state transition.
virtual void process (const std::string &key, const std::optional< sf::Event > &event)
 Invoke all callbacks registered for key.
template<typename ENUM_TYPE>
void process (ENUM_TYPE eventName, const std::optional< sf::Event > &event)
 Invoke all callbacks registered for eventName.
void setDragBounds (const sf::FloatRect &bounds)
 Constrain drag movement to an axis-aligned bounding rectangle.
std::enable_if_t<!std::is_void_v< T > > setState (T newState)
 Transition to a new state.

Friends

class DraggableDispatcher

Detailed Description

Receiver trait that adds mouse-drag behavior to any Component.

Dragging is disabled by default. Enable via:

myComponent.setFlag(ml::Flag::DRAGGABLE);
@ DRAGGABLE
Enables drag handling in ComponentCore.
Definition Flag.h:67

Axis locking

myComponent.setState(ml::Draggable::State::LOCK_X); // Y axis only
myComponent.setState(ml::Draggable::State::LOCK_Y); // X axis only
myComponent.setState(ml::Draggable::State::FREE); // both axes

Drag bounds

myComponent.setDragBounds(sf::FloatRect{{50.f, 50.f}, {400.f, 300.f}});
myComponent.clearDragBounds();
Rect< float > FloatRect
See also
DraggableManifest, DraggableDispatcher

Definition at line 65 of file Draggable.h.

Member Typedef Documentation

◆ Flag

Definition at line 69 of file Draggable.h.

◆ manifest_type

using ml::Customizable< DraggableManifest >::manifest_type
inherited

The manifest type, used by GatherFlags and GatherStates to locate this class's flag and state declarations.

Definition at line 64 of file Customizable.h.

◆ State

Definition at line 70 of file Draggable.h.

Constructor & Destructor Documentation

◆ Draggable()

ml::Draggable::Draggable ( )
default

◆ ~Draggable()

virtual ml::Draggable::~Draggable ( )
virtualdefault

Member Function Documentation

◆ clearDragBounds()

void ml::Draggable::clearDragBounds ( )

Remove any previously set drag bounds.

After this call the component can be dragged freely across the window with no position clamping. Has no effect if no bounds were set.

◆ getState()

std::enable_if_t<!std::is_void_v< T >, T > ml::StateManager< extract_State< DraggableManifest >::type >::getState ( ) const
inherited

Return the current state value.

Returns
The active StateEnum value.

◆ isState()

std::enable_if_t<!std::is_void_v< T >, bool > ml::StateManager< extract_State< DraggableManifest >::type >::isState ( T state) const
inherited

Return true if currently in state.

Parameters
stateThe state value to compare against.
Returns
true if the current state equals state.

◆ onStateEnter()

std::enable_if_t<!std::is_void_v< T > > ml::StateManager< extract_State< DraggableManifest >::type >::onStateEnter ( std::function< void(T)> callback)
inherited

Register a callback invoked after each state transition.

The callback receives the newly entered state. Only one enter callback is active at a time; calling this again replaces it.

Parameters
callbackFunction called with the new state after each transition.

◆ onStateExit()

std::enable_if_t<!std::is_void_v< T > > ml::StateManager< extract_State< DraggableManifest >::type >::onStateExit ( std::function< void(T)> callback)
inherited

Register a callback invoked before each state transition.

The callback receives the state being left. Only one exit callback is active at a time; calling this again replaces it.

Parameters
callbackFunction called with the old state before each transition.

◆ process() [1/2]

virtual void ml::EventReceiver::process ( const std::string & key,
const std::optional< sf::Event > & event )
virtualinherited

Invoke all callbacks registered for key.

Called by framework dispatchers. key is the string produced by EnumKey::get(eventEnum).

Parameters
keyString key identifying the event.
eventSFML event forwarded to each callback.

◆ process() [2/2]

template<typename ENUM_TYPE>
void ml::EventReceiver::process ( ENUM_TYPE eventName,
const std::optional< sf::Event > & event )
inherited

Invoke all callbacks registered for eventName.

Template overload that accepts any enum value directly, converting to a string key via EnumKey::get() internally.

Template Parameters
ENUM_TYPEAny enum type.
Parameters
eventNameThe enum value identifying the event.
eventSFML event forwarded to each callback.

◆ setDragBounds()

void ml::Draggable::setDragBounds ( const sf::FloatRect & bounds)

Constrain drag movement to an axis-aligned bounding rectangle.

While bounds are set, the component's position is clamped so that its top-left corner never moves outside bounds during a drag gesture. Bounds are applied in world coordinates.

Call clearDragBounds() to remove the constraint and allow free movement again.

Parameters
boundsThe world-space rectangle within which the component may be dragged.

◆ setState()

std::enable_if_t<!std::is_void_v< T > > ml::StateManager< extract_State< DraggableManifest >::type >::setState ( T newState)
inherited

Transition to a new state.

Fires the exit callback with the current state, updates the stored state to newState, then fires the enter callback with the new state. Either callback may be unset (nullptr), in which case it is skipped.

Parameters
newStateThe state to transition into.

◆ DraggableDispatcher

friend class DraggableDispatcher
friend

Definition at line 103 of file Draggable.h.


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