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)
-
inline Event(obscura::EventCategory category, obscura::EventCause cause)
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
-
enumerator None
-
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
-
enumerator None
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
-
inline explicit KeyEvent(int key)
-
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
-
MouseMovedEvent(float xPosition, float yPosition, float xOffset, float yOffset)
-
class MouseClickedEvent : public obscura::KeyEvent<EventCause::MouseClicked>
Public Functions
-
MouseClickedEvent(float xPosition, float yPosition, int key)
-
auto getPosition() const -> const glm::vec2&
-
MouseClickedEvent(float xPosition, float yPosition, int key)
Window events
-
class WindowEvent : public obscura::Event
Public Functions
-
explicit WindowEvent(const EventCause &cause)
-
explicit WindowEvent(const EventCause &cause)
Loop events
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;
}