Skip to content

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.

struct WindowCloseEvent {};

WindowResizeEvent

Fired when the window is resized.

struct WindowResizeEvent
{
    std::uint32_t width;
    std::uint32_t height;
    bool handled = false;
};

WindowFocusEvent

Fired when the window gains or loses focus.

struct WindowFocusEvent
{
    bool focused;
};

Keyboard Events

KeyPressedEvent

Fired when a key is pressed.

struct KeyPressedEvent
{
    KeyCode key;
    bool held;  // true if key is being held down
};

KeyReleasedEvent

Fired when a key is released.

struct KeyReleasedEvent
{
    KeyCode key;
};

KeyTypedEvent

Fired when a key is typed (for text input).

struct KeyTypedEvent
{
    char32_t codepoint;
};

Mouse Events

MouseButtonPressedEvent

Fired when a mouse button is pressed.

struct MouseButtonPressedEvent
{
    MouseButton button;
    glm::vec2 position;
};

MouseButtonReleasedEvent

Fired when a mouse button is released.

struct MouseButtonReleasedEvent
{
    MouseButton button;
    glm::vec2 position;
};

MouseMovedEvent

Fired when the mouse moves.

struct MouseMovedEvent
{
    glm::vec2 position;
    glm::vec2 delta;
};

MouseScrolledEvent

Fired when the mouse wheel is scrolled.

struct MouseScrolledEvent
{
    glm::vec2 offset;
};

Gamepad Events

GamepadConnectedEvent

Fired when a gamepad is connected.

struct GamepadConnectedEvent
{
    std::uint32_t gamepadId;
};

GamepadDisconnectedEvent

Fired when a gamepad is disconnected.

struct GamepadDisconnectedEvent
{
    std::uint32_t gamepadId;
};

GamepadButtonPressedEvent

Fired when a gamepad button is pressed.

struct GamepadButtonPressedEvent
{
    std::uint32_t gamepadId;
    GamepadButton button;
};

GamepadButtonReleasedEvent

Fired when a gamepad button is released.

struct GamepadButtonReleasedEvent
{
    std::uint32_t gamepadId;
    GamepadButton button;
};

GamepadAxisEvent

Fired when a gamepad axis changes.

struct GamepadAxisEvent
{
    std::uint32_t gamepadId;
    GamepadAxis axis;
    float value;  // -1.0 to 1.0
};

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.

enum class MouseButton
{
    Left,
    Right,
    Middle,
    X1,
    X2
};

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.

enum class GamepadAxis
{
    LeftX, LeftY,
    RightX, RightY,
    LeftTrigger,
    RightTrigger
};

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;
    });
}