Skip to content

Events

Events are essential objects that allow you to interact with the user's peripherals.


Overview

To interact with the different machine events, RE:MAKE 2D offers the singleton event of type EventManager, storing various event signals of type _EventSignal.

_EventSignal inherits from all methods of the Signal class, but also has its own method:

bool isActive(void) const noexcept;

Which allows checking if an event is active during the analysis of another one. This is particularly useful for key combinations:

rmk::event.onPressS.join([&](void) {
    if(rmk::event.onPressCtrl.isActive()) save();
});

Warning

isActive is preferred for key combinations because event handles one SDL event at a time. Two keys pressed simultaneously will be processed in separate calls, so a combination like Ctrl+S cannot be detected by connecting both events independently. isActive work with keyboard scancode and console press events only .


Different events

The EventManager class covers 300+ events, handling keyboard, mouse, gamepad, touch, window and application events. Each event is a _EventSignal instance that can be connected just like a regular Signal.

Keyboard

_EventSignal<> onPressA .. onPressZ;
_EventSignal<> onPress0 .. onPress9;
_EventSignal<> onPressSpace, onPressEnter, onPressEscape;
_EventSignal<> onPressBackspace, onPressTab, onPressCapsLock;
_EventSignal<> onPressDelete, onPressInsert, onPressHome;
_EventSignal<> onPressEnd, onPressPageUp, onPressPageDown;
_EventSignal<> onPressLeft, onPressRight, onPressUp, onPressDown;
_EventSignal<> onPressShift, onPressLShift, onPressRShift;
_EventSignal<> onPressCtrl, onPressLCtrl, onPressRCtrl;
_EventSignal<> onPressAlt, onPressLAlt, onPressRAlt;
_EventSignal<> onPressAny;
_EventSignal<> onPressF1 .. onPressF12;

// Equivalent onRelease* events
// Equivalent onPressScan*, onReleaseScan* events (scancode-based, layout-independent)

Text input

_EventSignal<std::string> onTextInput;   // UTF-8 text input
_EventSignal<std::string> onTextEdit;    // IME composition

Mouse

_EventSignal<Vec2d> onMouseMove;         // absolute position
_EventSignal<Vec2d> onMouseRawMove;      // relative motion
_EventSignal<Vec2d> onLeftDown, onLeftUp;
_EventSignal<Vec2d> onRightDown, onRightUp;
_EventSignal<Vec2d> onMiddleDown, onMiddleUp;
_EventSignal<Vec2d> onDoubleClick;
_EventSignal<Vec2d> onWheel;             // scroll delta (x, y)

Gamepad

// i32 : controller ID
_EventSignal<i32> onPressACtrl, onPressBCtrl, onPressXCtrl, onPressYCtrl;
_EventSignal<i32> onPressStart, onPressSelect;
_EventSignal<i32> onPressLShoulder, onPressRShoulder;
_EventSignal<i32> onPressDpadLeft, onPressDpadRight, onPressDpadUp, onPressDpadDown;
_EventSignal<i32> onPressLStick, onPressRStick;
// Equivalent onRelease* events

// i16 : axis value
_EventSignal<i32, i16> onAxisLeftX, onAxisLeftY;
_EventSignal<i32, i16> onAxisRightX, onAxisRightY;
_EventSignal<i32, i16> onAxisLTrigger, onAxisRTrigger;
_EventSignal<i32> onControllerAdded;
_EventSignal<i32> onControllerRemoved;

Touch

// Vec2d : click position
_EventSignal<Vec2d> onFingerDown;
_EventSignal<Vec2d> onFingerUp;
_EventSignal<Vec2d> onFingerMove;
_EventSignal<Vec2d> onMultiGesture;

Mouse and Touch (mobile / mouse compatibility)

// Vec2d : click position
_EventSignal<Vec2d> onPointerUp;    // left button or finger up
_EventSignal<Vec2d> onPointerDown;  // left button or finger down

Window

// u32 = window ID
_EventSignal<u32>        onWindowClose;
_EventSignal<u32>        onWindowMinimized, onWindowMaximized, onWindowRestored;
_EventSignal<u32>        onWindowShown, onWindowHidden, onWindowExposed;
_EventSignal<u32>        onWindowFocusGained, onWindowFocusLost;
_EventSignal<u32>        onWindowMouseEnter, onWindowMouseLeave;
_EventSignal<u32, Dim2d> onWindowResized;    // Dim2d = new size
_EventSignal<u32, Vec2d> onWindowMoved;      // Vec2d = new position

Application

_EventSignal<>            onQuit;
_EventSignal<std::string> onDropFile;
_EventSignal<>            onClipboardUpdate;
_EventSignal<>            onAppForeground, onAppBackground, onAppLowMemory;
_EventSignal<>            onLocaleChanged, onDisplayChanged, onRenderReset;
_EventSignal<>            onEventNone;

Usage

Connecting a function to an event works exactly the same way as with signals:

rmk::event.onQuit.connect(foo);
rmk::event.onPressSpace.join([&](void) { player.jump(400.0f); });
rmk::event.onLeftDown.join([&](Vec2d pos) { shoot(pos); });
rmk::event.onPressACtrl.join([&](i32 id) { player.jump(400.0f); });

Methods

The EventManager provides three ways to read events each frame, depending on how you want your loop to behave.

void poll(void);                        // process all pending events (non-blocking)
void wait(void);                        // wait indefinitely for an event
void wait(time::Second timeout);        // wait for an event with timeout
void update(void);                      // EventManager::poll alias

void textInput(bool);                   // Enable/disable text read mode
bool textInput(void);                   // check text read mode stat

Info

By default, loop uses poll to read events. To use a different mode, call it manually inside the main loop:

rmk::loop.execute(win, [&]() {
    rmk::event.wait();
});

Info

Text read mode allows you to enable or disable text input events (e.g., onTextInput). If disabled, keyboard inputs are no longer treated as literal text.


Property

Type Copiable Movable Bases and Traits
EventManager No No None

Previous chapter Next chapter