aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/CMakeLists.txt3
-rw-r--r--src/crepe/api/Event.h94
-rw-r--r--src/crepe/api/EventHandler.cpp8
-rw-r--r--src/crepe/api/EventHandler.h53
-rw-r--r--src/crepe/api/EventHandler.hpp25
-rw-r--r--src/crepe/api/EventManager.hpp42
-rw-r--r--src/crepe/api/IKeyListener.h3
-rw-r--r--src/crepe/api/IMouseListener.h3
8 files changed, 155 insertions, 76 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 53b3041..edf4250 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -19,11 +19,11 @@ target_sources(crepe PUBLIC
Camera.cpp
Animator.cpp
EventManager.cpp
- EventHandler.cpp
IKeyListener.cpp
IMouseListener.cpp
LoopManager.cpp
LoopTimer.cpp
+ EventHandler.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -51,6 +51,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
EventManager.h
EventManager.hpp
EventHandler.h
+ EventHandler.hpp
Event.h
IKeyListener.h
IMouseListener.h
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h
index 701ecdf..e7cac25 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -1,56 +1,116 @@
#pragma once
-#include "KeyCodes.h"
+
#include <iostream>
#include <string>
#include <typeindex>
+#include "KeyCodes.h"
+/**
+ * \brief Base class for all event types in the system.
+ */
class Event {
public:
};
+/**
+ * \brief Event triggered when a key is pressed.
+ */
class KeyPressEvent : public Event {
public:
- int repeat = 0;
- Keycode key = Keycode::NONE;
+ //! 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;
};
+/**
+ * \brief Event triggered when a key is released.
+ */
class KeyReleaseEvent : public Event {
public:
- Keycode key = Keycode::NONE;
+ //! The key that was released.
+ Keycode key = Keycode::NONE;
};
+/**
+ * \brief Event triggered when a mouse button is pressed.
+ */
class MousePressEvent : public Event {
public:
- int mouse_x = 0;
- int mouse_y = 0;
- MouseButton button = MouseButton::NONE;
+ //! 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;
+
+ //! The mouse button that was pressed.
+ MouseButton button = MouseButton::NONE;
};
+/**
+ * \brief Event triggered when a mouse button is clicked (press and release).
+ */
class MouseClickEvent : public Event {
public:
- int mouse_x = 0;
- int mouse_y = 0;
- MouseButton button = MouseButton::NONE;
+ //! 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;
+
+ //! The mouse button that was clicked.
+ MouseButton button = MouseButton::NONE;
};
+
+/**
+ * \brief Event triggered when a mouse button is released.
+ */
class MouseReleaseEvent : public Event {
public:
- int mouse_x = 0;
- int mouse_y = 0;
- MouseButton button = MouseButton::NONE;
+ //! 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;
+
+ //! The mouse button that was released.
+ MouseButton button = MouseButton::NONE;
};
+
+/**
+ * \brief Event triggered when the mouse is moved.
+ */
class MouseMoveEvent : public Event {
public:
- int mouse_x = 0;
- int mouse_y = 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;
};
+
+/**
+ * \brief Event triggered during a collision between objects.
+ */
class CollisionEvent : public Event {
public:
- //Collision collisionData;
+ //! Data describing the collision (currently not implemented).
+ // Collision collisionData;
};
+
+/**
+ * \brief Event triggered when text is submitted, e.g., from a text input.
+ */
class TextSubmitEvent : public Event {
public:
- std::string text = "";
+ //! The submitted text.
+ std::string text = "";
};
+
+/**
+ * \brief Event triggered to indicate the application is shutting down.
+ */
class ShutDownEvent : public Event {
public:
};
diff --git a/src/crepe/api/EventHandler.cpp b/src/crepe/api/EventHandler.cpp
index 93a116a..d90d765 100644
--- a/src/crepe/api/EventHandler.cpp
+++ b/src/crepe/api/EventHandler.cpp
@@ -1,2 +1,8 @@
#include "EventHandler.h"
-bool IEventHandlerWrapper::exec(const Event & e) { return call(e); }
+
+using namespace crepe;
+
+// Implementation of IEventHandlerWrapper::exec
+bool IEventHandlerWrapper::exec(const Event & e) {
+ return call(e);
+}
diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h
index 0ab90de..2a684c0 100644
--- a/src/crepe/api/EventHandler.h
+++ b/src/crepe/api/EventHandler.h
@@ -1,9 +1,11 @@
#pragma once
-#include "Event.h"
+
#include <functional>
-#include <iostream>
-#include <typeindex>
+#include <string>
+
+#include "Event.h"
+namespace crepe {
/**
* \brief A type alias for an event handler function.
*
@@ -12,7 +14,6 @@
*
* \tparam EventType The type of event this handler will handle.
*/
-// TODO: typedef
template <typename EventType>
using EventHandler = std::function<bool(const EventType & e)>;
@@ -25,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.
@@ -38,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.
@@ -58,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;
};
/**
@@ -74,18 +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)
- : m_handler(handler), m_handler_type(m_handler.target_type().name()) {}
+ 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,20 +93,23 @@ private:
* \param e The event to be handled.
* \return A boolean value indicating whether the event is handled.
*/
- bool call(const Event & e) override {
- return m_handler(static_cast<const EventType &>(e));
- }
+ 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 { return m_handler_type; }
- //! The event handler function.
- EventHandler<EventType> m_handler;
- //! The type name of the handler function.
- const std::string m_handler_type;
+ 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;
};
+
+} // namespace crepe
+
+#include "EventHandler.hpp"
diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp
new file mode 100644
index 0000000..1b5702e
--- /dev/null
+++ b/src/crepe/api/EventHandler.hpp
@@ -0,0 +1,25 @@
+
+#include <typeindex>
+
+#include "EventHandler.h"
+
+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()) {}
+
+// Implementation of EventHandlerWrapper::call
+template <typename EventType>
+bool EventHandlerWrapper<EventType>::call(const Event & 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;
+}
+
+} //namespace crepe
diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp
index b509097..70f0a31 100644
--- a/src/crepe/api/EventManager.hpp
+++ b/src/crepe/api/EventManager.hpp
@@ -12,10 +12,7 @@ void EventManager::subscribe(EventHandler<EventType> && callback, int channel) {
std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
handlers_map
= this->subscribers_by_event_id[event_type];
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
- handlers
- = handlers_map.find(channel);
+ auto handlers = handlers_map.find(channel);
if (handlers != handlers_map.end()) {
handlers->second.emplace_back(std::move(handler));
} else {
@@ -49,16 +46,12 @@ void EventManager::trigger_event(const EventType & event, int channel) {
std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
handlers_map
= this->subscribers_by_event_id[event_type];
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
- handlers_it
- = handlers_map.find(channel);
+ auto handlers_it = handlers_map.find(channel);
if (handlers_it != handlers_map.end()) {
std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
= handlers_it->second;
- for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it
- = handlers.begin();
+ for (auto it = handlers.begin();
it != handlers.end();++it) {
// stops when callback returns true
if((*it)->exec(event)){
@@ -69,7 +62,7 @@ void EventManager::trigger_event(const EventType & event, int channel) {
} else {
std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
= this->subscribers[event_type];
- for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it
+ for (auto it
= handlers.begin();
it != handlers.end();++it) {
// stops when callback returns true
@@ -87,28 +80,17 @@ void EventManager::unsubscribe(const EventHandler<EventType> & callback,
std::string handler_name = callback.target_type().name();
if (channel) {
- std::unordered_map<
- std::type_index,
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>::
- iterator subscriber_list
- = this->subscribers_by_event_id.find(event_type);
+ 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;
- std::unordered_map<
- int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
- handlers
- = handlers_map.find(channel);
+ 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 it
- = callbacks.begin();
+ for (auto it = callbacks.begin();
it != callbacks.end(); ++it) {
if ((*it)->get_type() == handler_name) {
it = callbacks.erase(it);
@@ -118,15 +100,11 @@ void EventManager::unsubscribe(const EventHandler<EventType> & callback,
}
}
} else {
- 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 it
- = handlers.begin();
+ for (auto it = handlers.begin();
it != handlers.end(); ++it) {
if ((*it)->get_type() == handler_name) {
it = handlers.erase(it);
@@ -137,4 +115,4 @@ void EventManager::unsubscribe(const EventHandler<EventType> & callback,
}
}
-}
+} // namespace crepe
diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h
index 839acbf..5a2cafa 100644
--- a/src/crepe/api/IKeyListener.h
+++ b/src/crepe/api/IKeyListener.h
@@ -3,6 +3,7 @@
#include "EventHandler.h"
#include "EventManager.h"
+namespace crepe {
/**
* \class IKeyListener
* \brief Interface for keyboard event handling in the application.
@@ -76,3 +77,5 @@ private:
//!< Key release event handler.
EventHandler<KeyReleaseEvent> key_released_handler;
};
+
+} // namespace crepe
diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h
index 7e92956..1df55af 100644
--- a/src/crepe/api/IMouseListener.h
+++ b/src/crepe/api/IMouseListener.h
@@ -4,6 +4,7 @@
#include "EventHandler.h"
#include "EventManager.h"
+namespace crepe {
/**
* \class IMouseListener
* \brief Interface for mouse event handling in the application.
@@ -115,3 +116,5 @@ private:
//! Mouse move event handler.
EventHandler<MouseMoveEvent> mouse_move_handler;
};
+
+} //namespace crepe