QuasarEngine
Cross-platform 3D engine and editor
Loading...
Searching...
No Matches
Quasar::EventDispatcher Class Reference

Calls a handler only when an event has the type that the handler expects. More...

#include <quasar/events/EventDispatcher.h>

Public Member Functions

 EventDispatcher (Event &event)
 Creates a dispatcher for an event. The event must stay alive while the dispatcher is used.
template<EventClass T, std::predicate< T & > Handler>
bool Dispatch (Handler &&handler)
 Calls the handler if the event is of type T.

Detailed Description

Calls a handler only when an event has the type that the handler expects.

Create a dispatcher for the received event, then call Dispatch() once for each event class to handle:

void OnEvent(Event& event)
{
EventDispatcher dispatcher(event);
dispatcher.Dispatch<WindowResizeEvent>([this](WindowResizeEvent& resize) { return OnResize(resize); });
}
EventDispatcher(Event &event)
Creates a dispatcher for an event. The event must stay alive while the dispatcher is used.
Definition EventDispatcher.h:41
Base class of all events.
Definition Event.h:111
Sent when the size of the drawing area of the window changes.
Definition WindowEvent.h:27

Adding a new event class does not require any change here: Dispatch() only relies on GetStaticType().

Member Function Documentation

◆ Dispatch()

template<EventClass T, std::predicate< T & > Handler>
bool Quasar::EventDispatcher::Dispatch ( Handler && handler)
inline

Calls the handler if the event is of type T.

When the handler returns true, the event is marked as handled. A handled event stays handled: a handler that returns false never clears the flag set by another one.

Template Parameters
TEvent class expected by the handler.
Parameters
handlerFunction that takes a T& and returns true if it handled the event.
Returns
True if the event is of type T and the handler was called.

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