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

Base class for all per-event dispatchers in the Malena event system. More...

#include <EventDispatcher.h>

Inheritance diagram for ml::EventDispatcher:

Public Member Functions

void fire (const std::optional< sf::Event > &event) override=0
 Deliver this event to all matching registered components.
bool occurred (const std::optional< sf::Event > &event) override=0
 Return true when the incoming SFML event should trigger this dispatcher.

Detailed Description

Base class for all per-event dispatchers in the Malena event system.

EventDispatcher is the class you inherit from to create a new event trait. It narrows Fireable to the SFML-event–driven path only: the no-argument fire() and occurred() overloads inherited from Fireable are sealed final so you only need to implement the two event-taking versions.

How it fits together

A complete event trait requires three pieces:

  1. A receiver class — inherits EventReceiver and exposes a friendly callback registration method (onClick, onHover, etc.) that stores a callback via Fireable::addCallback.
  2. A dispatcher class — inherits EventDispatcher and implements occurred(), filter(), and fire(). Registered as a singleton with the event system via ML_EXPORT.
  3. ML_EXPORT(MyDispatcher) — placed after the class definition in the header. Creates the singleton and registers it with Fireable at startup.

What each method does

Method Called by Purpose
occurred(event) AppManager each frame Return true when this SFML event (or frame tick) should trigger processing.
filter(event, component) Framework, after occurred Return true for each Core component that should receive this event. Default returns true for all.
fire(event) Framework, after filtering Iterate registered components and call process() on those that pass the filter.

Minimal example

// ── MyTrait.h ─────────────────────────────────────────────────────────────
// 1. Receiver — stores the callback and exposes the friendly API
class MyTrait : public ml::EventReceiver
{
public:
void onMyEvent(std::function<void()> f)
{
Fireable::addCallback(ml::Event::CLICK, this,
[f](const std::optional<sf::Event>&){ f(); });
}
};
// 2. Dispatcher — decides when and to whom to deliver
class MyTraitDispatcher : public ml::EventDispatcher
{
public:
bool occurred(const std::optional<sf::Event>& event) override
{
// Return true when the triggering SFML event arrives
return event && event->is<sf::Event::MouseButtonReleased>();
}
bool filter(const std::optional<sf::Event>& event, ml::Core* component) override
{
// Optional: return false to skip components that shouldn't receive this
return !component->checkFlag(ml::Flag::HIDDEN);
}
void fire(const std::optional<sf::Event>& event) override
{
for (auto* c : ml::EventsManager::getComponents())
if (filter(event, c))
c->process(ml::Event::CLICK, event);
}
};
// 3. Register the singleton — must be outside the namespace
ML_EXPORT(MyTraitDispatcher)
#define ML_EXPORT(ClassName)
Register a Malena type with the framework.
Definition Export.h:38
Base class for all event-receiving traits.
bool checkFlag(State state) const
Return the current value of a flag.
@ CLICK
Mouse button released over component.
Definition Event.h:36
@ HIDDEN
Component should not be drawn.
Definition Flag.h:63
See also
Fireable, EventReceiver, ML_EXPORT, Clickable, Hoverable

Definition at line 88 of file EventDispatcher.h.

Member Function Documentation

◆ fire()

void ml::EventDispatcher::fire ( const std::optional< sf::Event > & event)
overridepure virtual

Deliver this event to all matching registered components.

Called by the framework after occurred() returns true. Typically iterates the component list and calls process() on each component that passes filter().

Parameters
eventThe SFML event that triggered this dispatch, or std::nullopt for synthetic/frame-driven events.

Implemented in ml::DraggableDispatcher.

◆ occurred()

bool ml::EventDispatcher::occurred ( const std::optional< sf::Event > & event)
overridepure virtual

Return true when the incoming SFML event should trigger this dispatcher.

Called once per event by the framework before any components are visited. Return false to skip the entire dispatch pass for this event.

Parameters
eventThe SFML event to evaluate, or std::nullopt.
Returns
true if this dispatcher should fire for event.

Implemented in ml::DraggableDispatcher.


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