Event System¶
Freya provides a flexible pub/sub event system for handling application events.
EventManager¶
Central hub for subscribing to and publishing events.
auto eventManager = serviceProvider->GetService<fra::EventManager>();
// Subscribe to an event
eventManager->Subscribe<WindowResizeEvent>([](WindowResizeEvent event) {
std::cout << "Window resized to " << event.width << "x" << event.height << std::endl;
});
// Publish an event
eventManager->Send(MyEvent{ .data = 42 });
Event Types¶
Window Events¶
WindowCloseEvent¶
Fired when the window is closed.
WindowResizeEvent¶
Fired when the window is resized.
WindowFocusEvent¶
Fired when the window gains or loses focus.
Keyboard Events¶
KeyPressedEvent¶
Fired when a key is pressed.
KeyReleasedEvent¶
Fired when a key is released.
KeyTypedEvent¶
Fired when a key is typed (for text input).
Mouse Events¶
MouseButtonPressedEvent¶
Fired when a mouse button is pressed.
MouseButtonReleasedEvent¶
Fired when a mouse button is released.
MouseMovedEvent¶
Fired when the mouse moves.
MouseScrolledEvent¶
Fired when the mouse wheel is scrolled.
Gamepad Events¶
GamepadConnectedEvent¶
Fired when a gamepad is connected.
GamepadDisconnectedEvent¶
Fired when a gamepad is disconnected.
GamepadButtonPressedEvent¶
Fired when a gamepad button is pressed.
GamepadButtonReleasedEvent¶
Fired when a gamepad button is released.
GamepadAxisEvent¶
Fired when a gamepad axis changes.
KeyCode¶
Key codes for keyboard input. See KeyCode.hpp for the full enumeration.
Common key codes: - KeyCode::A through KeyCode::Z - KeyCode::Num0 through KeyCode::Num9 - KeyCode::Space - KeyCode::Enter - KeyCode::Escape - KeyCode::Left, KeyCode::Right, KeyCode::Up, KeyCode::Down
MouseButton¶
Mouse button identifiers.
GamepadButton¶
Gamepad button identifiers.
enum class GamepadButton
{
A, B, X, Y,
LeftShoulder, RightShoulder,
LeftTrigger, RightTrigger,
DPadUp, DPadDown, DPadLeft, DPadRight,
Start, Select,
LeftThumb, RightThumb
};
GamepadAxis¶
Gamepad axis identifiers.
Event Subscriptions¶
Subscribe to events in StartUp():
void StartUp() override
{
mEventManager->Subscribe<WindowCloseEvent>([this](WindowCloseEvent) {
std::cout << "Window closed!" << std::endl;
});
mEventManager->Subscribe<KeyPressedEvent>([this](KeyPressedEvent event) {
if (event.key == KeyCode::Escape)
{
// Handle escape key
}
});
mEventManager->Subscribe<MouseMovedEvent>([this](MouseMovedEvent event) {
std::cout << "Mouse at " << event.position.x << ", " << event.position.y << std::endl;
});
}