aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-16 15:21:49 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-16 15:21:49 +0100
commit027e67b32a9cca3cac4a186e73fdcc42faeab8c4 (patch)
tree99f775bd358ee623bcd14f9092dd80d03a360bff /src
parent6e7003c73110cf9ad7e4c72de703dfdc2292d8ca (diff)
added eventManager reference to interfaces
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Event.h67
-rw-r--r--src/crepe/api/EventHandler.cpp4
-rw-r--r--src/crepe/api/EventHandler.h36
-rw-r--r--src/crepe/api/EventHandler.hpp10
-rw-r--r--src/crepe/api/EventManager.cpp49
-rw-r--r--src/crepe/api/EventManager.h12
-rw-r--r--src/crepe/api/EventManager.hpp69
-rw-r--r--src/crepe/api/IKeyListener.cpp81
-rw-r--r--src/crepe/api/IKeyListener.h3
-rw-r--r--src/crepe/api/IMouseListener.cpp45
-rw-r--r--src/crepe/api/IMouseListener.h2
-rw-r--r--src/crepe/api/KeyCodes.h318
-rw-r--r--src/example/events.cpp12
13 files changed, 365 insertions, 343 deletions
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h
index e7cac25..d5ddf0a 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -1,10 +1,9 @@
#pragma once
-#include <iostream>
#include <string>
-#include <typeindex>
#include "KeyCodes.h"
+
/**
* \brief Base class for all event types in the system.
*/
@@ -17,11 +16,11 @@ public:
*/
class KeyPressEvent : public Event {
public:
- //! Number of times the key press is repeated (e.g., for long presses).
- int repeat = 0;
+ //! Number of times the key press is repeated (e.g., for long presses).
+ int repeat = 0;
- //! The key that was pressed.
- Keycode key = Keycode::NONE;
+ //! The key that was pressed.
+ Keycode key = Keycode::NONE;
};
/**
@@ -29,8 +28,8 @@ public:
*/
class KeyReleaseEvent : public Event {
public:
- //! The key that was released.
- Keycode key = Keycode::NONE;
+ //! The key that was released.
+ Keycode key = Keycode::NONE;
};
/**
@@ -38,14 +37,14 @@ public:
*/
class MousePressEvent : public Event {
public:
- //! X-coordinate of the mouse position at the time of the event.
- int mouse_x = 0;
+ //! X-coordinate of the mouse position at the time of the event.
+ int mouse_x = 0;
- //! Y-coordinate of the mouse position at the time of the event.
- int mouse_y = 0;
+ //! Y-coordinate of the mouse position at the time of the event.
+ int mouse_y = 0;
- //! The mouse button that was pressed.
- MouseButton button = MouseButton::NONE;
+ //! The mouse button that was pressed.
+ MouseButton button = MouseButton::NONE;
};
/**
@@ -53,14 +52,14 @@ public:
*/
class MouseClickEvent : public Event {
public:
- //! X-coordinate of the mouse position at the time of the event.
- int mouse_x = 0;
+ //! X-coordinate of the mouse position at the time of the event.
+ int mouse_x = 0;
- //! Y-coordinate of the mouse position at the time of the event.
- int mouse_y = 0;
+ //! Y-coordinate of the mouse position at the time of the event.
+ int mouse_y = 0;
- //! The mouse button that was clicked.
- MouseButton button = MouseButton::NONE;
+ //! The mouse button that was clicked.
+ MouseButton button = MouseButton::NONE;
};
/**
@@ -68,14 +67,14 @@ public:
*/
class MouseReleaseEvent : public Event {
public:
- //! X-coordinate of the mouse position at the time of the event.
- int mouse_x = 0;
+ //! X-coordinate of the mouse position at the time of the event.
+ int mouse_x = 0;
- //! Y-coordinate of the mouse position at the time of the event.
- int mouse_y = 0;
+ //! Y-coordinate of the mouse position at the time of the event.
+ int mouse_y = 0;
- //! The mouse button that was released.
- MouseButton button = MouseButton::NONE;
+ //! The mouse button that was released.
+ MouseButton button = MouseButton::NONE;
};
/**
@@ -83,11 +82,11 @@ public:
*/
class MouseMoveEvent : public Event {
public:
- //! X-coordinate of the mouse position at the time of the event.
- int mouse_x = 0;
+ //! X-coordinate of the mouse position at the time of the event.
+ int mouse_x = 0;
- //! Y-coordinate of the mouse position at the time of the event.
- int mouse_y = 0;
+ //! Y-coordinate of the mouse position at the time of the event.
+ int mouse_y = 0;
};
/**
@@ -95,8 +94,8 @@ public:
*/
class CollisionEvent : public Event {
public:
- //! Data describing the collision (currently not implemented).
- // Collision collisionData;
+ //! Data describing the collision (currently not implemented).
+ // Collision collisionData;
};
/**
@@ -104,8 +103,8 @@ public:
*/
class TextSubmitEvent : public Event {
public:
- //! The submitted text.
- std::string text = "";
+ //! The submitted text.
+ std::string text = "";
};
/**
diff --git a/src/crepe/api/EventHandler.cpp b/src/crepe/api/EventHandler.cpp
index d90d765..186ec9c 100644
--- a/src/crepe/api/EventHandler.cpp
+++ b/src/crepe/api/EventHandler.cpp
@@ -3,6 +3,4 @@
using namespace crepe;
// Implementation of IEventHandlerWrapper::exec
-bool IEventHandlerWrapper::exec(const Event & e) {
- return call(e);
-}
+bool IEventHandlerWrapper::exec(const Event & e) { return call(e); }
diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h
index 2a684c0..db51d04 100644
--- a/src/crepe/api/EventHandler.h
+++ b/src/crepe/api/EventHandler.h
@@ -26,12 +26,12 @@ using EventHandler = std::function<bool(const EventType & e)>;
*/
class IEventHandlerWrapper {
public:
- /**
+ /**
* \brief Virtual destructor for IEventHandlerWrapper.
*/
- virtual ~IEventHandlerWrapper() = default;
+ virtual ~IEventHandlerWrapper() = default;
- /**
+ /**
* \brief Executes the handler with the given event.
*
* This method calls the `call()` method of the derived class, passing the event to the handler.
@@ -39,19 +39,19 @@ public:
* \param e The event to be processed.
* \return A boolean value indicating whether the event is handled.
*/
- bool exec(const Event & e);
+ bool exec(const Event & e);
- /**
+ /**
* \brief Get the type of the event handler.
*
* This method returns the type of the event handler as a string.
*
* \return A string representing the handler's type.
*/
- virtual std::string get_type() const = 0;
+ virtual std::string get_type() const = 0;
private:
- /**
+ /**
* \brief The method responsible for handling the event.
*
* This method is implemented by derived classes to process the event.
@@ -59,7 +59,7 @@ private:
* \param e The event to be processed.
* \return A boolean value indicating whether the event is handled.
*/
- virtual bool call(const Event & e) = 0;
+ virtual bool call(const Event & e) = 0;
};
/**
@@ -75,17 +75,17 @@ private:
template <typename EventType>
class EventHandlerWrapper : public IEventHandlerWrapper {
public:
- /**
+ /**
* \brief Constructs an EventHandlerWrapper with a given handler.
*
* The constructor takes an event handler function and stores it in the wrapper.
*
* \param handler The event handler function.
*/
- explicit EventHandlerWrapper(const EventHandler<EventType> & handler);
+ explicit EventHandlerWrapper(const EventHandler<EventType> & handler);
private:
- /**
+ /**
* \brief Calls the stored event handler with the event.
*
* This method casts the event to the appropriate type and calls the handler.
@@ -93,21 +93,21 @@ private:
* \param e The event to be handled.
* \return A boolean value indicating whether the event is handled.
*/
- bool call(const Event & e) override;
+ bool call(const Event & e) override;
- /**
+ /**
* \brief Returns the type of the handler.
*
* This method returns a string representing the type of the event handler.
*
* \return The handler type as a string.
*/
- std::string get_type() const override;
+ std::string get_type() const override;
- //! The event handler function.
- EventHandler<EventType> m_handler;
- //! The type name of the handler function.
- const std::string m_handler_type;
+ //! The event handler function.
+ EventHandler<EventType> m_handler;
+ //! The type name of the handler function.
+ const std::string m_handler_type;
};
} // namespace crepe
diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp
index 1b5702e..564d3d7 100644
--- a/src/crepe/api/EventHandler.hpp
+++ b/src/crepe/api/EventHandler.hpp
@@ -7,19 +7,21 @@ namespace crepe {
// Implementation of EventHandlerWrapper constructor
template <typename EventType>
-EventHandlerWrapper<EventType>::EventHandlerWrapper(const EventHandler<EventType> & handler)
- : m_handler(handler), m_handler_type(m_handler.target_type().name()) {}
+EventHandlerWrapper<EventType>::EventHandlerWrapper(
+ const EventHandler<EventType> & handler)
+ : m_handler(handler),
+ m_handler_type(m_handler.target_type().name()) {}
// Implementation of EventHandlerWrapper::call
template <typename EventType>
bool EventHandlerWrapper<EventType>::call(const Event & e) {
- return m_handler(static_cast<const EventType &>(e));
+ return m_handler(static_cast<const EventType &>(e));
}
// Implementation of EventHandlerWrapper::get_type
template <typename EventType>
std::string EventHandlerWrapper<EventType>::get_type() const {
- return m_handler_type;
+ return m_handler_type;
}
} //namespace crepe
diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp
index e881d49..a04f08b 100644
--- a/src/crepe/api/EventManager.cpp
+++ b/src/crepe/api/EventManager.cpp
@@ -8,10 +8,10 @@ EventManager & EventManager::get_instance() {
}
void EventManager::dispatch_events() {
- for (std::vector<std::tuple<std::unique_ptr<Event>, int,
- std::type_index>>::iterator event_it
- = this->events_queue.begin();
- event_it != this->events_queue.end();) {
+ using HandlersMap = std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>;
+ using HandlersVec = std::vector<std::unique_ptr<IEventHandlerWrapper>>;
+
+ for (auto event_it = this->events_queue.begin(); event_it != this->events_queue.end();) {
std::unique_ptr<Event> & event = std::get<0>(*event_it);
int channel = std::get<1>(*event_it);
std::type_index event_type = std::get<2>(*event_it);
@@ -19,29 +19,13 @@ void EventManager::dispatch_events() {
bool event_handled = false;
if (channel) {
- std::unordered_map<
- std::type_index,
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>::
- iterator handlers_it
- = subscribers_by_event_id.find(event_type);
+ auto handlers_it = subscribers_by_event_id.find(event_type);
if (handlers_it != subscribers_by_event_id.end()) {
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = handlers_it->second;
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::
- iterator handlers
- = handlers_map.find(channel);
+ HandlersMap & handlers_map = handlers_it->second;
+ auto handlers = handlers_map.find(channel);
if (handlers != handlers_map.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> &
- callbacks
- = handlers->second;
- for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::
- iterator handler_it
- = callbacks.begin();
- handler_it != callbacks.end(); ++handler_it) {
+ HandlersVec & callbacks = handlers->second;
+ for (auto handler_it = callbacks.begin(); handler_it != callbacks.end(); ++handler_it) {
if ((*handler_it)->exec(*event)) {
event_it = events_queue.erase(event_it);
event_handled = true;
@@ -52,18 +36,10 @@ void EventManager::dispatch_events() {
}
} else {
// Handle event for all channels
- std::unordered_map<
- std::type_index,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
- handlers_it
- = this->subscribers.find(event_type);
+ auto handlers_it = this->subscribers.find(event_type);
if (handlers_it != this->subscribers.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = handlers_it->second;
- for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::
- iterator handler_it
- = handlers.begin();
- handler_it != handlers.end(); ++handler_it) {
+ HandlersVec & handlers = handlers_it->second;
+ for (auto handler_it = handlers.begin(); handler_it != handlers.end(); ++handler_it) {
// remove event from queue since and continue when callback returns true
if ((*handler_it)->exec(*event)) {
event_it = this->events_queue.erase(event_it);
@@ -79,3 +55,4 @@ void EventManager::dispatch_events() {
}
}
}
+
diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h
index 38d2e64..1ff4031 100644
--- a/src/crepe/api/EventManager.h
+++ b/src/crepe/api/EventManager.h
@@ -10,8 +10,8 @@
#include "Event.h"
#include "EventHandler.h"
-
namespace crepe {
+
/**
* \class EventManager
* \brief The EventManager class is responsible for managing the subscription, triggering,
@@ -20,16 +20,6 @@ namespace crepe {
class EventManager {
public:
/**
- * \brief Deleted copy constructor to prevent copying of the EventManager instance.
- */
- EventManager(const EventManager &) = delete;
-
- /**
- * \brief Deleted copy assignment operator to prevent assignment of the EventManager instance.
- */
- const EventManager & operator=(const EventManager &) = delete;
-
- /**
* \brief Get the singleton instance of the EventManager.
*
* This method returns the unique instance of the EventManager, creating it on the first call.
diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp
index 70f0a31..1e505f4 100644
--- a/src/crepe/api/EventManager.hpp
+++ b/src/crepe/api/EventManager.hpp
@@ -1,17 +1,18 @@
#include "EventManager.h"
+
namespace crepe {
template <typename EventType>
void EventManager::subscribe(EventHandler<EventType> && callback, int channel) {
+ using HandlersMap = std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>;
+ using HandlersVec = std::vector<std::unique_ptr<IEventHandlerWrapper>>;
+
std::type_index event_type = typeid(EventType);
std::unique_ptr<EventHandlerWrapper<EventType>> handler
= std::make_unique<EventHandlerWrapper<EventType>>(callback);
if (channel) {
- std::unordered_map<int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = this->subscribers_by_event_id[event_type];
+ HandlersMap & handlers_map = this->subscribers_by_event_id[event_type];
auto handlers = handlers_map.find(channel);
if (handlers != handlers_map.end()) {
handlers->second.emplace_back(std::move(handler));
@@ -19,8 +20,7 @@ void EventManager::subscribe(EventHandler<EventType> && callback, int channel) {
handlers_map[channel].emplace_back(std::move(handler));
}
} else {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = this->subscribers[event_type];
+ HandlersVec & handlers = this->subscribers[event_type];
handlers.emplace_back(std::move(handler));
}
}
@@ -29,8 +29,7 @@ template <typename EventType>
void EventManager::queue_event(EventType && event, int channel) {
std::type_index event_type = std::type_index(typeid(EventType));
- std::unique_ptr<EventType> event_ptr
- = std::make_unique<EventType>(std::forward<EventType>(event));
+ auto event_ptr = std::make_unique<EventType>(std::forward<EventType>(event));
std::tuple<std::unique_ptr<Event>, int, std::type_index> tuple(
std::move(event_ptr), channel, event_type);
@@ -39,59 +38,51 @@ void EventManager::queue_event(EventType && event, int channel) {
template <typename EventType>
void EventManager::trigger_event(const EventType & event, int channel) {
+ using HandlersMap = std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>;
+ using HandlersVec = std::vector<std::unique_ptr<IEventHandlerWrapper>>;
+
std::type_index event_type = std::type_index(typeid(EventType));
if (channel > 0) {
- std::unordered_map<int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = this->subscribers_by_event_id[event_type];
+ HandlersMap & handlers_map = this->subscribers_by_event_id[event_type];
auto handlers_it = handlers_map.find(channel);
if (handlers_it != handlers_map.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = handlers_it->second;
- for (auto it = handlers.begin();
- it != handlers.end();++it) {
+ HandlersVec & handlers = handlers_it->second;
+ for (auto it = handlers.begin(); it != handlers.end(); ++it) {
// stops when callback returns true
- if((*it)->exec(event)){
+ if ((*it)->exec(event)) {
break;
}
}
}
} else {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = this->subscribers[event_type];
- for (auto it
- = handlers.begin();
- it != handlers.end();++it) {
+ HandlersVec & handlers = this->subscribers[event_type];
+ for (auto it = handlers.begin(); it != handlers.end(); ++it) {
// stops when callback returns true
- if((*it)->exec(event)){
- break;
- }
+ if ((*it)->exec(event)) {
+ break;
+ }
}
}
}
template <typename EventType>
-void EventManager::unsubscribe(const EventHandler<EventType> & callback,
- int channel) {
+void EventManager::unsubscribe(const EventHandler<EventType> & callback, int channel) {
+ using HandlersMap = std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>;
+ using HandlersVec = std::vector<std::unique_ptr<IEventHandlerWrapper>>;
+
std::type_index event_type(typeid(EventType));
std::string handler_name = callback.target_type().name();
if (channel) {
auto subscriber_list = this->subscribers_by_event_id.find(event_type);
if (subscriber_list != this->subscribers_by_event_id.end()) {
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = subscriber_list->second;
- auto handlers = handlers_map.find(channel);
+ HandlersMap & handlers_map = subscriber_list->second;
+ auto handlers = handlers_map.find(channel);
if (handlers != handlers_map.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & callbacks
- = handlers->second;
- for (auto it = callbacks.begin();
- it != callbacks.end(); ++it) {
+ HandlersVec & callbacks = handlers->second;
+ for (auto it = callbacks.begin(); it != callbacks.end(); ++it) {
if ((*it)->get_type() == handler_name) {
it = callbacks.erase(it);
return;
@@ -102,10 +93,8 @@ void EventManager::unsubscribe(const EventHandler<EventType> & callback,
} else {
auto handlers_it = this->subscribers.find(event_type);
if (handlers_it != this->subscribers.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = handlers_it->second;
- for (auto it = handlers.begin();
- it != handlers.end(); ++it) {
+ HandlersVec & handlers = handlers_it->second;
+ for (auto it = handlers.begin(); it != handlers.end(); ++it) {
if ((*it)->get_type() == handler_name) {
it = handlers.erase(it);
return;
diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp
index 4fd9855..81d30b8 100644
--- a/src/crepe/api/IKeyListener.cpp
+++ b/src/crepe/api/IKeyListener.cpp
@@ -2,50 +2,65 @@
using namespace crepe;
-IKeyListener::IKeyListener() {
- this->channel = channel;
- this->subscribe_events();
+// Constructor with default channel
+IKeyListener::IKeyListener() : channel(0), active(true), event_manager(EventManager::get_instance()) {
+ this->subscribe_events();
}
-IKeyListener::IKeyListener(int channel) {
- this->channel = channel;
- this->subscribe_events();
+
+// Constructor with specified channel
+IKeyListener::IKeyListener(int channel) : channel(channel), active(true), event_manager(EventManager::get_instance()) {
+ this->subscribe_events();
+}
+
+// Destructor, unsubscribe events
+IKeyListener::~IKeyListener() {
+ this->unsubscribe_events();
}
-IKeyListener::~IKeyListener() { this->unsubscribe_events(); }
+// Subscribe to key events
void IKeyListener::subscribe_events() {
- key_pressed_handler = [this](const KeyPressEvent & event) {
- return this->on_key_pressed(event);
- };
- key_released_handler = [this](const KeyReleaseEvent & event) {
- return this->on_key_released(event);
- };
- EventManager::get_instance().subscribe<KeyPressEvent>(
- std::move(this->key_pressed_handler), this->channel);
- EventManager::get_instance().subscribe<KeyReleaseEvent>(
- std::move(this->key_released_handler), this->channel);
+ key_pressed_handler = [this](const KeyPressEvent& event) {
+ return this->on_key_pressed(event);
+ };
+ key_released_handler = [this](const KeyReleaseEvent& event) {
+ return this->on_key_released(event);
+ };
+
+ event_manager.subscribe<KeyPressEvent>(
+ std::move(this->key_pressed_handler), this->channel);
+ event_manager.subscribe<KeyReleaseEvent>(
+ std::move(this->key_released_handler), this->channel);
}
+// Unsubscribe from key events
void IKeyListener::unsubscribe_events() {
- EventManager::get_instance().unsubscribe<KeyPressEvent>(
- this->key_pressed_handler, channel);
- EventManager::get_instance().unsubscribe<KeyReleaseEvent>(
- this->key_released_handler, channel);
- std::cout << std::endl;
+ event_manager.unsubscribe<KeyPressEvent>(
+ this->key_pressed_handler, this->channel);
+ event_manager.unsubscribe<KeyReleaseEvent>(
+ this->key_released_handler, this->channel);
}
+
+// Activate key listening
void IKeyListener::activate_keys() {
- if (this->active) {
- return;
- }
- this->subscribe_events();
+ if (this->active) {
+ return;
+ }
+ this->active = true;
+ this->subscribe_events();
}
+
+// Deactivate key listening
void IKeyListener::deactivate_keys() {
- if (!this->active) {
- return;
- }
- this->unsubscribe_events();
+ if (!this->active) {
+ return;
+ }
+ this->active = false;
+ this->unsubscribe_events();
}
+
+// Set a new channel for key events
void IKeyListener::set_channel(int channel) {
- this->unsubscribe_events();
- this->channel = channel;
- this->subscribe_events();
+ this->unsubscribe_events();
+ this->channel = channel;
+ this->subscribe_events();
}
diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h
index 5a2cafa..77fbf1e 100644
--- a/src/crepe/api/IKeyListener.h
+++ b/src/crepe/api/IKeyListener.h
@@ -1,9 +1,11 @@
#pragma once
+
#include "Event.h"
#include "EventHandler.h"
#include "EventManager.h"
namespace crepe {
+
/**
* \class IKeyListener
* \brief Interface for keyboard event handling in the application.
@@ -76,6 +78,7 @@ private:
EventHandler<KeyPressEvent> key_pressed_handler;
//!< Key release event handler.
EventHandler<KeyReleaseEvent> key_released_handler;
+ EventManager& event_manager;
};
} // namespace crepe
diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp
index 489e55b..f49004a 100644
--- a/src/crepe/api/IMouseListener.cpp
+++ b/src/crepe/api/IMouseListener.cpp
@@ -2,11 +2,19 @@
using namespace crepe;
-IMouseListener::IMouseListener(int channel) { this->channel = channel; }
+IMouseListener::IMouseListener(int channel)
+ : event_manager(EventManager::get_instance()), channel(channel) {
+ this->subscribe_events();
+}
-IMouseListener::IMouseListener() { this->subscribe_events(); }
+IMouseListener::IMouseListener()
+ : event_manager(EventManager::get_instance()) {
+ this->subscribe_events();
+}
-IMouseListener::~IMouseListener() { this->unsubscribe_events(); }
+IMouseListener::~IMouseListener() {
+ this->unsubscribe_events();
+}
void IMouseListener::subscribe_events() {
// Define handler lambdas and subscribe them
@@ -22,25 +30,20 @@ void IMouseListener::subscribe_events() {
mouse_move_handler = [this](const MouseMoveEvent & event) {
return this->on_mouse_moved(event);
};
- EventManager::get_instance().subscribe<MouseClickEvent>(
- std::move(this->mouse_click_handler), this->channel);
- EventManager::get_instance().subscribe<MousePressEvent>(
- std::move(this->mouse_press_handler), this->channel);
- EventManager::get_instance().subscribe<MouseReleaseEvent>(
- std::move(this->mouse_release_handler), this->channel);
- EventManager::get_instance().subscribe<MouseMoveEvent>(
- std::move(this->mouse_move_handler), this->channel);
+
+ // Subscribe event handlers (no need for std::move)
+ event_manager.subscribe<MouseClickEvent>(std::move(mouse_click_handler), this->channel);
+ event_manager.subscribe<MousePressEvent>(std::move(mouse_press_handler), this->channel);
+ event_manager.subscribe<MouseReleaseEvent>(std::move(mouse_release_handler), this->channel);
+ event_manager.subscribe<MouseMoveEvent>(std::move(mouse_move_handler), this->channel);
}
-// TODO: reference voor singleton
+
void IMouseListener::unsubscribe_events() {
- EventManager::get_instance().unsubscribe<MouseClickEvent>(
- this->mouse_click_handler, this->channel);
- EventManager::get_instance().unsubscribe<MousePressEvent>(
- this->mouse_press_handler, this->channel);
- EventManager::get_instance().unsubscribe<MouseReleaseEvent>(
- this->mouse_release_handler, this->channel);
- EventManager::get_instance().unsubscribe<MouseMoveEvent>(
- this->mouse_move_handler, this->channel);
+ // Unsubscribe event handlers
+ event_manager.unsubscribe<MouseClickEvent>(mouse_click_handler, this->channel);
+ event_manager.unsubscribe<MousePressEvent>(mouse_press_handler, this->channel);
+ event_manager.unsubscribe<MouseReleaseEvent>(mouse_release_handler, this->channel);
+ event_manager.unsubscribe<MouseMoveEvent>(mouse_move_handler, this->channel);
}
void IMouseListener::activate_mouse() {
@@ -48,6 +51,7 @@ void IMouseListener::activate_mouse() {
return;
}
this->subscribe_events();
+ this->active = true;
}
void IMouseListener::deactivate_mouse() {
@@ -55,6 +59,7 @@ void IMouseListener::deactivate_mouse() {
return;
}
this->unsubscribe_events();
+ this->active = false;
}
void IMouseListener::set_channel(int channel) {
diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h
index 1df55af..3b9e317 100644
--- a/src/crepe/api/IMouseListener.h
+++ b/src/crepe/api/IMouseListener.h
@@ -5,6 +5,7 @@
#include "EventManager.h"
namespace crepe {
+
/**
* \class IMouseListener
* \brief Interface for mouse event handling in the application.
@@ -115,6 +116,7 @@ private:
EventHandler<MouseReleaseEvent> mouse_release_handler;
//! Mouse move event handler.
EventHandler<MouseMoveEvent> mouse_move_handler;
+ EventManager& event_manager;
};
} //namespace crepe
diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h
index e5a91fc..a326527 100644
--- a/src/crepe/api/KeyCodes.h
+++ b/src/crepe/api/KeyCodes.h
@@ -1,146 +1,186 @@
#pragma once
+//! \file InputEnums.h
+//! \brief Defines enums for mouse buttons and keyboard keycodes used in the application.
+
+//! \enum MouseButton
+//! \brief Enumeration for mouse button inputs, including standard and extended buttons.
enum class MouseButton {
- NONE = 0,
- LEFT_MOUSE = 1,
- RIGHT_MOUSE = 2,
- MIDDLE_MOUSE = 3,
- X1_MOUSE = 4,
- X2_MOUSE = 5,
- SCROLL_UP = 6,
- SCROLL_DOWN = 7,
+ //! No mouse button input.
+ NONE = 0,
+ //! Left mouse button.
+ LEFT_MOUSE = 1,
+ //! Right mouse button.
+ RIGHT_MOUSE = 2,
+ //! Middle mouse button (scroll wheel press).
+ MIDDLE_MOUSE = 3,
+ //! First extended mouse button.
+ X1_MOUSE = 4,
+ //! Second extended mouse button.
+ X2_MOUSE = 5,
+ //! Scroll wheel upward movement.
+ SCROLL_UP = 6,
+ //! Scroll wheel downward movement.
+ SCROLL_DOWN = 7
};
+//! \enum Keycode
+//! \brief Enumeration for keyboard key inputs, including printable characters, function keys, and keypad keys.
enum class Keycode : int {
- NONE = 0,
- SPACE = 32,
- APOSTROPHE = 39, /* ' */
- COMMA = 44, /* , */
- MINUS = 45, /* - */
- PERIOD = 46, /* . */
- SLASH = 47, /* / */
-
- D0 = 48, /* 0 */
- D1 = 49, /* 1 */
- D2 = 50, /* 2 */
- D3 = 51, /* 3 */
- D4 = 52, /* 4 */
- D5 = 53, /* 5 */
- D6 = 54, /* 6 */
- D7 = 55, /* 7 */
- D8 = 56, /* 8 */
- D9 = 57, /* 9 */
-
- SEMICOLON = 59, /* ; */
- EQUAL = 61, /* = */
-
- A = 65,
- B = 66,
- C = 67,
- D = 68,
- E = 69,
- F = 70,
- G = 71,
- H = 72,
- I = 73,
- J = 74,
- K = 75,
- L = 76,
- M = 77,
- N = 78,
- O = 79,
- P = 80,
- Q = 81,
- R = 82,
- S = 83,
- T = 84,
- U = 85,
- V = 86,
- W = 87,
- X = 88,
- Y = 89,
- Z = 90,
-
- LEFT_BRACKET = 91, /* [ */
- BACKSLASH = 92, /* \ */
- RIGHT_BRACKET = 93, /* ] */
- GRAVE_ACCENT = 96, /* ` */
-
- WORLD1 = 161, /* non-US #1 */
- WORLD2 = 162, /* non-US #2 */
-
- /* Function keys */
- ESCAPE = 256,
- ENTER = 257,
- TAB = 258,
- BACKSPACE = 259,
- INSERT = 260,
- DELETE = 261,
- RIGHT = 262,
- LEFT = 263,
- DOWN = 264,
- UP = 265,
- PAGE_UP = 266,
- PAGE_DOWN = 267,
- HOME = 268,
- END = 269,
- CAPS_LOCK = 280,
- SCROLL_LOCK = 281,
- NUM_LOCK = 282,
- PRINT_SCREEN = 283,
- PAUSE = 284,
- F1 = 290,
- F2 = 291,
- F3 = 292,
- F4 = 293,
- F5 = 294,
- F6 = 295,
- F7 = 296,
- F8 = 297,
- F9 = 298,
- F10 = 299,
- F11 = 300,
- F12 = 301,
- F13 = 302,
- F14 = 303,
- F15 = 304,
- F16 = 305,
- F17 = 306,
- F18 = 307,
- F19 = 308,
- F20 = 309,
- F21 = 310,
- F22 = 311,
- F23 = 312,
- F24 = 313,
- F25 = 314,
-
- /* Keypad */
- KP0 = 320,
- KP1 = 321,
- KP2 = 322,
- KP3 = 323,
- KP4 = 324,
- KP5 = 325,
- KP6 = 326,
- KP7 = 327,
- KP8 = 328,
- KP9 = 329,
- KP_DECIMAL = 330,
- KP_DIVIDE = 331,
- KP_MULTIPLY = 332,
- KP_SUBTRACT = 333,
- KP_ADD = 334,
- KP_ENTER = 335,
- KP_EQUAL = 336,
-
- LEFT_SHIFT = 340,
- LEFT_CONTROL = 341,
- LEFT_ALT = 342,
- LEFT_SUPER = 343,
- RIGHT_SHIFT = 344,
- RIGHT_CONTROL = 345,
- RIGHT_ALT = 346,
- RIGHT_SUPER = 347,
- MENU = 348
+ //! No key input.
+ NONE = 0,
+ //! Spacebar.
+ SPACE = 32,
+ //! Apostrophe (').
+ APOSTROPHE = 39,
+ //! Comma (,).
+ COMMA = 44,
+ //! Minus (-).
+ MINUS = 45,
+ //! Period (.).
+ PERIOD = 46,
+ //! Slash (/).
+ SLASH = 47,
+ //! Digit 0.
+ D0 = 48,
+ //! Digit 1.
+ D1 = 49,
+ //! Digit 2.
+ D2 = 50,
+ //! Digit 3.
+ D3 = 51,
+ //! Digit 4.
+ D4 = 52,
+ //! Digit 5.
+ D5 = 53,
+ //! Digit 6.
+ D6 = 54,
+ //! Digit 7.
+ D7 = 55,
+ //! Digit 8.
+ D8 = 56,
+ //! Digit 9.
+ D9 = 57,
+ //! Semicolon (;).
+ SEMICOLON = 59,
+ //! Equal sign (=).
+ EQUAL = 61,
+ //! Key 'A'.
+ A = 65,
+ //! Key 'B'.
+ B = 66,
+ //! Key 'C'.
+ C = 67,
+ //! Key 'D'.
+ D = 68,
+ //! Key 'E'.
+ E = 69,
+ //! Key 'F'.
+ F = 70,
+ //! Key 'G'.
+ G = 71,
+ //! Key 'H'.
+ H = 72,
+ //! Key 'I'.
+ I = 73,
+ //! Key 'J'.
+ J = 74,
+ //! Key 'K'.
+ K = 75,
+ //! Key 'L'.
+ L = 76,
+ //! Key 'M'.
+ M = 77,
+ //! Key 'N'.
+ N = 78,
+ //! Key 'O'.
+ O = 79,
+ //! Key 'P'.
+ P = 80,
+ //! Key 'Q'.
+ Q = 81,
+ //! Key 'R'.
+ R = 82,
+ //! Key 'S'.
+ S = 83,
+ //! Key 'T'.
+ T = 84,
+ //! Key 'U'.
+ U = 85,
+ //! Key 'V'.
+ V = 86,
+ //! Key 'W'.
+ W = 87,
+ //! Key 'X'.
+ X = 88,
+ //! Key 'Y'.
+ Y = 89,
+ //! Key 'Z'.
+ Z = 90,
+ //! Left bracket ([).
+ LEFT_BRACKET = 91,
+ //! Backslash (\).
+ BACKSLASH = 92,
+ //! Right bracket (]).
+ RIGHT_BRACKET = 93,
+ //! Grave accent (`).
+ GRAVE_ACCENT = 96,
+ //! Non-US key #1.
+ WORLD1 = 161,
+ //! Non-US key #2.
+ WORLD2 = 162,
+ //! Escape key.
+ ESCAPE = 256,
+ //! Enter key.
+ ENTER = 257,
+ //! Tab key.
+ TAB = 258,
+ //! Backspace key.
+ BACKSPACE = 259,
+ //! Insert key.
+ INSERT = 260,
+ //! Delete key.
+ DELETE = 261,
+ //! Right arrow key.
+ RIGHT = 262,
+ //! Left arrow key.
+ LEFT = 263,
+ //! Down arrow key.
+ DOWN = 264,
+ //! Up arrow key.
+ UP = 265,
+ //! Page Up key.
+ PAGE_UP = 266,
+ //! Page Down key.
+ PAGE_DOWN = 267,
+ //! Home key.
+ HOME = 268,
+ //! End key.
+ END = 269,
+ //! Caps Lock key.
+ CAPS_LOCK = 280,
+ //! Scroll Lock key.
+ SCROLL_LOCK = 281,
+ //! Num Lock key.
+ NUM_LOCK = 282,
+ //! Print Screen key.
+ PRINT_SCREEN = 283,
+ //! Pause key.
+ PAUSE = 284,
+ //! Function keys (F1-F25).
+ F1 = 290, F2 = 291, F3 = 292, F4 = 293, F5 = 294,
+ F6 = 295, F7 = 296, F8 = 297, F9 = 298, F10 = 299,
+ F11 = 300, F12 = 301, F13 = 302, F14 = 303, F15 = 304,
+ F16 = 305, F17 = 306, F18 = 307, F19 = 308, F20 = 309,
+ F21 = 310, F22 = 311, F23 = 312, F24 = 313, F25 = 314,
+ //! Keypad digits and operators.
+ KP0 = 320, KP1 = 321, KP2 = 322, KP3 = 323, KP4 = 324,
+ KP5 = 325, KP6 = 326, KP7 = 327, KP8 = 328, KP9 = 329,
+ KP_DECIMAL = 330, KP_DIVIDE = 331, KP_MULTIPLY = 332,
+ KP_SUBTRACT = 333, KP_ADD = 334, KP_ENTER = 335, KP_EQUAL = 336,
+ //! Modifier keys.
+ LEFT_SHIFT = 340, LEFT_CONTROL = 341, LEFT_ALT = 342, LEFT_SUPER = 343,
+ RIGHT_SHIFT = 344, RIGHT_CONTROL = 345, RIGHT_ALT = 346, RIGHT_SUPER = 347,
+ //! Menu key.
+ MENU = 348
};
diff --git a/src/example/events.cpp b/src/example/events.cpp
index ed519ff..5a0a748 100644
--- a/src/example/events.cpp
+++ b/src/example/events.cpp
@@ -89,11 +89,13 @@ int main() {
// Trigger the events while `testListener` is in scope
EventManager::get_instance().trigger_event<KeyPressEvent>(key_press, 1);
- EventManager::get_instance().trigger_event(MouseClickEvent{
- .mouse_x = 100,
- .mouse_y = 100,
- .button = MouseButton::LEFT_MOUSE,
- },1);
+ EventManager::get_instance().trigger_event(
+ MouseClickEvent{
+ .mouse_x = 100,
+ .mouse_y = 100,
+ .button = MouseButton::LEFT_MOUSE,
+ },
+ 1);
}
// custom lambda event handler
EventHandler<KeyPressEvent> event_handler = [](const KeyPressEvent & e) {