Events

class Event

Base class for all event_system.

Subclassed by obscura::KeyEvent< EventCause::MouseClicked >, obscura::KeyEvent< compile_time_cause >, obscura::LoopStoppedEvent, obscura::MouseMovedEvent, obscura::MouseScrolledEvent, obscura::TickEvent, obscura::WindowEvent

Public Functions

inline Event(obscura::EventCategory category, obscura::EventCause cause)
inline auto getCategory() const -> obscura::EventCategory
inline auto getCause() const -> obscura::EventCause
inline auto getCode() const

Returns a unique code that is a combination of the event’s category and cause.

Returns:

The event’s code.

inline void setCategory(EventCategory eventCategory)
inline void setCause(EventCause eventCause)

Available event categories and causes

enum class obscura::EventCategory

The available event categories for Obscura. SIZE must be at the end of the enum. It is used to determine the number of categories.

Values:

enumerator None
enumerator Application
enumerator KeyInput
enumerator ScrollInput
enumerator CoordinateInput
enumerator Window
enumerator SIZE
enum class obscura::EventCause

The available event causes for Obscura.

Values:

enumerator None
enumerator KeyPressed
enumerator KeyReleased
enumerator KeyRepeated
enumerator WindowClosed
enumerator WindowResized
enumerator WindowMoved
enumerator WindowFocus
enumerator WindowLostFocus
enumerator MouseMoved
enumerator MouseScrolled
enumerator MouseClicked
enumerator LoopTicked
enumerator LoopStopped
enumerator Rendered

Available events in Obscura

Keyboard events

template<obscura::EventCause compile_time_cause>
class KeyEvent : public obscura::Event

Public Functions

inline explicit KeyEvent(int key)
inline auto getKey() const -> int
using obscura::KeyPressedEvent = KeyEvent<EventCause::KeyPressed>
using obscura::KeyReleasedEvent = KeyEvent<EventCause::KeyReleased>
using obscura::KeyRepeatedEvent = KeyEvent<EventCause::KeyRepeated>

Mouse events

class MouseMovedEvent : public obscura::Event

Public Functions

MouseMovedEvent(float xPosition, float yPosition, float xOffset, float yOffset)
auto getOffset() const -> const glm::vec2&
auto getPreviousPosition() const -> const glm::vec2&
auto getCurrentPosition() const -> glm::vec2
class MouseClickedEvent : public obscura::KeyEvent<EventCause::MouseClicked>

Public Functions

MouseClickedEvent(float xPosition, float yPosition, int key)
auto getPosition() const -> const glm::vec2&
class MouseScrolledEvent : public obscura::Event

Public Functions

MouseScrolledEvent(float verticalOffset, float horizontalOffset)
auto getVerticalOffset() const -> const float&
auto getHorizontalOffset() const -> const float&

Window events

class WindowEvent : public obscura::Event

Public Functions

explicit WindowEvent(const EventCause &cause)

Loop events

class TickEvent : public obscura::Event

Public Functions

explicit TickEvent(std::chrono::microseconds durationSinceLastTick)
auto getDurationSinceLastTick() const -> std::chrono::microseconds

Helper functions

inline constexpr auto obscura::eventCode(EventCategory category, EventCause cause) -> std::size_t

Returns a unique code that is a combination of the event’s category and cause.

Parameters:
  • category – Category of the event.

  • cause – Cause of the event.

Returns:

Unique code for the event.

inline auto obscura::getCategoryFromCode(std::size_t code) -> EventCategory

Determines and returns the category of an event from its code.

Parameters:

code – The event’s code.

Returns:

An event category.

inline auto obscura::getCauseFromCode(std::size_t code) -> EventCause

Determines and returns the cause of an event from its code.

Parameters:

code – The event’s code.

Returns:

An event cause.

Example

The following code shows the creation of a custom event class:

#include <obscura/obscura.hxx>
#include <utility>

class CustomEvent: public obscura::Event {
  public:
    explicit CustomEvent(std::string message)
      : obscura::Event(obscura::EventCategory::None, obscura::EventCause::None),
        message(std::move(message)) {
    }

    [[nodiscard]] auto getMessage() const -> const std::string& {
        return message;
    }

  private:
    std::string message;
};

template<class Listener>
using CustomEventCallback = CallbackWrapper<Listener, CustomEvent, &Listener::onCustomEvent>;
using CustomEventDefinition = EventDefinition<"CustomEventDefinition", CustomEvent, CustomEventCallback>;

class CustomListener {
  public:
    void onCustomEvent(const CustomEvent& event) {  // NOLINT(readability-convert-member-functions-to-static)
        // The function cannot be static because it needs to be registered as a callback.
        obscura::logInfo("{}", event.getMessage());
    }
};

auto main() -> int {
    try {
        auto event = CustomEvent("I traveled through the dispatcher!");

        auto listener = CustomListener();

        // All event definitions that are used in a project should be passed to the
        // ConfigurableDispatcher template. Here, we only need one event definition.
        auto dispatcher = obscura::ConfigurableDispatcher<CustomEventDefinition>();
        dispatcher.registerListener(listener);
        dispatcher.enqueue(event);

        obscura::logInfo("Event is in queue but not yet dispatched!");

        dispatcher.dispatch();

        obscura::logInfo("Event was dispatched!");
    } catch (const std::exception& e) {
        obscura::logError("Exception caught: {}", e.what());
    }

    return EXIT_SUCCESS;
}