SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Sleak::EventDispatcher Class Reference

#include <Event.hpp>

Static Public Member Functions

static void ClearEventHandlers ()
 Drops every handler for every event type; equivalent to UnregisterAllEvents().
template<typename EventT>
static void DispatchEvent (const EventT &event)
 Invokes every handler registered for event's type, in registration order.
template<typename EventT>
static std::string RegisterEventCallback (std::function< void(const EventT &)> callback)
 Registers a free-function/lambda callback for EventT, returning an ID for later unregistration.
template<typename T, typename EventT>
static std::string RegisterEventHandler (T *instance, void(T::*memberFunction)(const EventT &))
 Registers a member-function handler bound to instance, returning an ID for later unregistration.
static void UnregisterAllEvents ()
 Drops every handler for every event type.
static void UnregisterEvent (EventType type, std::string id)
 Removes the single handler with matching id from type's handler list.
static void UnregisterEvents (EventType type)
 Drops every handler registered for type.

Detailed Description

Static registry mapping EventType to subscribed handlers; dispatches events synchronously to every handler registered for its type.

Everything here is static, so there is no dispatcher instance to pass around: subscribe from anywhere, and the window layer's keyboard, mouse, and window events reach you. RegisterEventHandler() binds a member function and is the form you will use most; RegisterEventCallback() takes a std::function for lambdas and free functions.

Both return a string id. Keep it and pass it to UnregisterEvent() when the subscriber goes away, typically in a scene's OnDeactivate() or destructor. Handlers outlive the objects they were bound to otherwise, and the next dispatch calls into freed memory.

Dispatch is synchronous and runs in registration order on the calling thread. The handler list is copied before iteration, so a handler may register or unregister during dispatch safely.

class WorldScene : public Sleak::Scene {
public:
void Begin() override {
this, &WorldScene::OnKeyPressed);
}
void OnDeactivate() override {
if (!m_keyId.empty()) {
m_keyId.clear();
}
}
void OnKeyPressed(
const Sleak::Events::Input::KeyPressedEvent& e) {
if_key_press(KEY__F) { ToggleFlashlight(); }
}
private:
std::string m_keyId;
};
#define if_key_press(key)
static void UnregisterEvent(EventType type, std::string id)
Removes the single handler with matching id from type's handler list.
Definition Event.hpp:163
static std::string RegisterEventHandler(T *instance, void(T::*memberFunction)(const EventT &))
Registers a member-function handler bound to instance, returning an ID for later unregistration.
Definition Event.hpp:149
void Begin() override
Activates every owned object; runs once after Initialize().
Definition Scene.cpp:26
void OnDeactivate() override
Override for logic that should run when the scene stops being active.
Definition Scene.cpp:18
See also
Event, EventType, Events::Input::KeyPressedEvent, Events::Input::MouseMovedEvent

Definition at line 132 of file Event.hpp.

Member Function Documentation

◆ ClearEventHandlers()

void Sleak::EventDispatcher::ClearEventHandlers ( )
inlinestatic

Drops every handler for every event type; equivalent to UnregisterAllEvents().

Definition at line 205 of file Event.hpp.

◆ DispatchEvent()

template<typename EventT>
void Sleak::EventDispatcher::DispatchEvent ( const EventT & event)
inlinestatic

Invokes every handler registered for event's type, in registration order.

Definition at line 185 of file Event.hpp.

◆ RegisterEventCallback()

template<typename EventT>
std::string Sleak::EventDispatcher::RegisterEventCallback ( std::function< void(const EventT &)> callback)
inlinestatic

Registers a free-function/lambda callback for EventT, returning an ID for later unregistration.

Definition at line 137 of file Event.hpp.

◆ RegisterEventHandler()

template<typename T, typename EventT>
std::string Sleak::EventDispatcher::RegisterEventHandler ( T * instance,
void(T::* memberFunction )(const EventT &) )
inlinestatic

Registers a member-function handler bound to instance, returning an ID for later unregistration.

Definition at line 149 of file Event.hpp.

◆ UnregisterAllEvents()

void Sleak::EventDispatcher::UnregisterAllEvents ( )
inlinestatic

Drops every handler for every event type.

Definition at line 178 of file Event.hpp.

◆ UnregisterEvent()

void Sleak::EventDispatcher::UnregisterEvent ( EventType type,
std::string id )
inlinestatic

Removes the single handler with matching id from type's handler list.

Definition at line 163 of file Event.hpp.

◆ UnregisterEvents()

void Sleak::EventDispatcher::UnregisterEvents ( EventType type)
inlinestatic

Drops every handler registered for type.

Definition at line 173 of file Event.hpp.