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

Trait that allows a component to subscribe to and publish framework events. More...

#include <Subscribable.h>

Inheritance diagram for ml::Subscribable:

Public Member Functions

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.
template<typename ENUM_TYPE>
void publish (ENUM_TYPE event, FilterCallback filter=[](EventReceiver &){ return true;}, SystemCallback resolve=nullptr, SystemCallback reject=nullptr)
 Fire an enum-keyed event to all matching subscribers.
template<typename ENUM_TYPE>
void subscribe (ENUM_TYPE event, Callback callback)
 Subscribe to an enum-keyed event with a no-argument callback.
template<typename ENUM_TYPE>
void subscribe (ENUM_TYPE event, EventCallback callback)
 Subscribe to an enum-keyed event with a full SFML event callback.

Detailed Description

Trait that allows a component to subscribe to and publish framework events.

Subscribable is one of the three core traits automatically included in every ml::Core object (Subscribable, Flaggable, Positionable). It is the low-level event subscription layer — most user code will interact with the higher-level trait convenience methods (onClick, onHover, onUpdate, etc.) rather than calling subscribe and publish directly.

Both ml::Event built-in values and user-defined manifest event enums work identically — any enum value is accepted.

Subscribing to a built-in event

// Preferred — use the trait convenience methods
myRect.onClick([]{ std::cout << "clicked\n"; });
myRect.onUpdate([]{ /* per-frame logic *\/ });
*
* // Advanced — subscribe with a raw ml::Event value
* myRect.subscribe(ml::Event::CLICK, [](const std::optional<sf::Event>& e){
* // handle click with SFML event data
* });
*

Publishing a custom event

enum class MyEvent { GameStarted, ScoreChanged };
// Publish to all subscribers of MyEvent::GameStarted
myRect.publish(MyEvent::GameStarted);
// Publish with a filter — only deliver to components within a rect
myRect.publish(MyEvent::ScoreChanged,
auto* c = dynamic_cast<ml::Core*>(&r);
return c && zone.contains(c->getPosition());
});
Virtual base class for all Malena framework objects.
Definition Core.h:67
Base class for all event-receiving traits.
See also
Clickable, Hoverable, Updatable, Unsubscribable, EventsManager

Definition at line 58 of file Subscribable.h.

Member Function Documentation

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

◆ publish()

template<typename ENUM_TYPE>
void ml::Subscribable::publish ( ENUM_TYPE event,
FilterCallback filter = [](EventReceiver &){ return true;},
SystemCallback resolve = nullptr,
SystemCallback reject = nullptr )

Fire an enum-keyed event to all matching subscribers.

Iterates all subscribers registered for event and invokes their callbacks for each subscriber that passes the optional filter. Optional resolve and reject system callbacks are called after each component passes or fails the filter respectively.

Template Parameters
ENUM_TYPEAny enum type.
Parameters
eventThe enum value identifying the event to fire.
filterOptional predicate; only subscribers for which this returns true receive the event. Defaults to accepting all subscribers.
resolveOptional callback invoked for each subscriber that passes the filter.
rejectOptional callback invoked for each subscriber that fails the filter.

◆ subscribe() [1/2]

template<typename ENUM_TYPE>
void ml::Subscribable::subscribe ( ENUM_TYPE event,
Callback callback )

Subscribe to an enum-keyed event with a no-argument callback.

Convenience overload for callbacks that do not need access to the raw SFML event.

Template Parameters
ENUM_TYPEAny enum type.
Parameters
eventThe enum value identifying the event to subscribe to.
callbackFunction invoked with no arguments when the event fires.

◆ subscribe() [2/2]

template<typename ENUM_TYPE>
void ml::Subscribable::subscribe ( ENUM_TYPE event,
EventCallback callback )

Subscribe to an enum-keyed event with a full SFML event callback.

The callback is invoked each time the event fires and the framework's filter (defined by the associated EventDispatcher) passes this component. The std::optional<sf::Event> parameter carries the raw SFML event when one is available, or std::nullopt for frame events.

Template Parameters
ENUM_TYPEAny enum type — ml::Event for built-in events, or a manifest event enum for custom events.
Parameters
eventThe enum value identifying the event to subscribe to.
callbackFunction invoked with the SFML event when the event fires.

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