aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/include
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-10-25 20:59:34 +0200
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-10-25 20:59:34 +0200
commit15a4c4a1cf67e13be53ef423c51c56488f332ea9 (patch)
tree661084fc8171089ce32bf779f2b3a85d634b38e4 /mwe/events/include
parentf472e0ba3bf54fc5055cf9f08925bed3f98a1dbc (diff)
parentb3b762a34e7ccb4a0dcd041a693ac7180af16002 (diff)
merge conficts fixed
Diffstat (limited to 'mwe/events/include')
-rw-r--r--mwe/events/include/event.h84
-rw-r--r--mwe/events/include/eventHandler.h50
-rw-r--r--mwe/events/include/eventManager.h80
-rw-r--r--mwe/events/include/keyCodes.h248
-rw-r--r--mwe/events/include/loopManager.h5
5 files changed, 245 insertions, 222 deletions
diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h
index d060faa..b1d9cb9 100644
--- a/mwe/events/include/event.h
+++ b/mwe/events/include/event.h
@@ -1,90 +1,98 @@
#pragma once
+#include "keyCodes.h"
#include <cstdint>
#include <iostream>
#include <string>
#include <unordered_map>
#include <variant>
+<<<<<<< HEAD
#include "keyCodes.h"
#include "customTypes.h"
+=======
+>>>>>>> b3b762a34e7ccb4a0dcd041a693ac7180af16002
class UUIDGenerator {
public:
- static std::uint32_t getUniqueID() {
- static std::uint32_t id = 0;
- return ++id;
- }
+ static std::uint32_t getUniqueID() {
+ static std::uint32_t id = 0;
+ return ++id;
+ }
};
#define REGISTER_EVENT_TYPE(ClassName) \
+\
public: \
- static std::uint32_t getStaticEventType() { \
- static std::uint32_t typeID = UUIDGenerator::getUniqueID(); \
- return typeID; \
- } \
- virtual std::uint32_t getEventType() const override { \
- return getStaticEventType(); \
- }
+ static std::uint32_t getStaticEventType() { \
+ static std::uint32_t typeID = UUIDGenerator::getUniqueID(); \
+ return typeID; \
+ } \
+ virtual std::uint32_t getEventType() const override { \
+ return getStaticEventType(); \
+ }
class Event {
public:
- Event(std::string eventType);
- virtual ~Event() = default;
- virtual std::uint32_t getEventType() const = 0;
- virtual std::string toString() const;
- void addArgument(const std::string& key, const std::variant<int, std::string, float>& value);
+ Event(std::string eventType);
+ virtual ~Event() = default;
+ virtual std::uint32_t getEventType() const = 0;
+ virtual std::string toString() const;
+ void addArgument(const std::string & key,
+ const std::variant<int, std::string, float> & value);
- std::variant<int, std::string, float> getArgument(const std::string& key) const;
+ std::variant<int, std::string, float>
+ getArgument(const std::string & key) const;
- std::string getType() const;
- bool getHandled() const;
- void markHandled();
+ std::string getType() const;
+ bool getHandled() const;
+ void markHandled();
private:
- std::unordered_map<std::string, std::variant<int, std::string, float>> eventData;
- bool isHandled = false;
+ std::unordered_map<std::string, std::variant<int, std::string, float>>
+ eventData;
+ bool isHandled = false;
};
// KeyPressedEvent class
class KeyPressedEvent : public Event {
public:
- KeyPressedEvent(int keyCode);
+ KeyPressedEvent(int keyCode);
- REGISTER_EVENT_TYPE("KeyPressedEvent");
+ REGISTER_EVENT_TYPE("KeyPressedEvent");
- Keycode getKeyCode() const;
- int getRepeatCount() const;
+ Keycode getKeyCode() const;
+ int getRepeatCount() const;
private:
- Keycode keycode;
+ Keycode keycode;
public:
- Keycode key = 0;
- int repeatCount = 0;
+ Keycode key = 0;
+ int repeatCount = 0;
};
// KeyReleasedEvent class
class KeyReleasedEvent : public Event {
public:
- KeyReleasedEvent(int keyCode);
+ KeyReleasedEvent(int keyCode);
- REGISTER_EVENT_TYPE(KeyReleasedEvent);
+ REGISTER_EVENT_TYPE(KeyReleasedEvent);
- Keycode getKeyCode() const;
+ Keycode getKeyCode() const;
private:
- Keycode key = 0;
+ Keycode key = 0;
};
// MousePressedEvent class
class MousePressedEvent : public Event {
public:
- MousePressedEvent(int mouseX, int mouseY);
+ MousePressedEvent(int mouseX, int mouseY);
- REGISTER_EVENT_TYPE(MousePressedEvent)
+ REGISTER_EVENT_TYPE(MousePressedEvent)
- std::pair<int, int> getMousePosition() const;
+ std::pair<int, int> getMousePosition() const;
private:
- int mouseX = 0;
- int mouseY = 0;
+ int mouseX = 0;
+ int mouseY = 0;
};
class CollisionEvent : public Event {
public:
diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h
index e5b99d6..7414801 100644
--- a/mwe/events/include/eventHandler.h
+++ b/mwe/events/include/eventHandler.h
@@ -4,45 +4,43 @@
#include <functional>
#include <iostream>
-template<typename EventType>
-using EventHandler = std::function<void(const EventType& e)>;
+template <typename EventType>
+using EventHandler = std::function<void(const EventType & e)>;
class IEventHandlerWrapper {
public:
- virtual ~IEventHandlerWrapper() = default;
+ virtual ~IEventHandlerWrapper() = default;
- void exec(const Event& e);
+ void exec(const Event & e);
- virtual std::string getType() const = 0;
- virtual bool isDestroyOnSuccess() const = 0;
+ virtual std::string getType() const = 0;
+ virtual bool isDestroyOnSuccess() const = 0;
private:
- virtual void call(const Event& e) = 0;
+ virtual void call(const Event & e) = 0;
};
-template<typename EventType>
+template <typename EventType>
class EventHandlerWrapper : public IEventHandlerWrapper {
public:
- explicit EventHandlerWrapper(const EventHandler<EventType>& handler, const bool destroyOnSuccess = false)
- : m_handler(handler)
- , m_handlerType(m_handler.target_type().name())
- , m_destroyOnSuccess(destroyOnSuccess)
- {
+ explicit EventHandlerWrapper(const EventHandler<EventType> & handler,
+ const bool destroyOnSuccess = false)
+ : m_handler(handler), m_handlerType(m_handler.target_type().name()),
+ m_destroyOnSuccess(destroyOnSuccess) {
// std::cout << m_handlerType << std::endl;
}
private:
- void call(const Event& e) override
- {
- if (e.getEventType() == EventType::getStaticEventType()) {
- m_handler(static_cast<const EventType&>(e));
- }
- }
-
- std::string getType() const override { return m_handlerType; }
- bool isDestroyOnSuccess() const { return m_destroyOnSuccess; }
-
- EventHandler<EventType> m_handler;
- const std::string m_handlerType;
- bool m_destroyOnSuccess { false };
+ void call(const Event & e) override {
+ if (e.getEventType() == EventType::getStaticEventType()) {
+ m_handler(static_cast<const EventType &>(e));
+ }
+ }
+
+ std::string getType() const override { return m_handlerType; }
+ bool isDestroyOnSuccess() const { return m_destroyOnSuccess; }
+
+ EventHandler<EventType> m_handler;
+ const std::string m_handlerType;
+ bool m_destroyOnSuccess{false};
};
diff --git a/mwe/events/include/eventManager.h b/mwe/events/include/eventManager.h
index b8abccc..91bac8c 100644
--- a/mwe/events/include/eventManager.h
+++ b/mwe/events/include/eventManager.h
@@ -1,57 +1,73 @@
#pragma once
-#include <unordered_map>
-#include <memory>
#include "event.h"
-#include "keyCodes.h"
#include "eventHandler.h"
+#include "keyCodes.h"
+#include <memory>
+#include <unordered_map>
#include <vector>
// using EventType = std::uint32_t;
// using EventId = std::uint64_t;
class EventManager {
public:
- EventManager(const EventManager&) = delete;
- const EventManager& operator=(const EventManager&) = delete;
- static EventManager& getInstance() {
- static EventManager instance;
- return instance;
- }
+ EventManager(const EventManager &) = delete;
+ const EventManager & operator=(const EventManager &) = delete;
+ static EventManager & getInstance() {
+ static EventManager instance;
+ return instance;
+ }
- void shutdown();
- void subscribe(int eventType, std::unique_ptr<IEventHandlerWrapper>&& handler, int eventId);
- void unsubscribe(int eventType, const std::string& handlerName, int eventId);
- void triggerEvent(const Event& event_, int eventId);
- void queueEvent(std::unique_ptr<Event>&& event_, int eventId);
- void dispatchEvents();
+ void shutdown();
+ void subscribe(int eventType,
+ std::unique_ptr<IEventHandlerWrapper> && handler,
+ int eventId);
+ void unsubscribe(int eventType, const std::string & handlerName,
+ int eventId);
+ void triggerEvent(const Event & event_, int eventId);
+ void queueEvent(std::unique_ptr<Event> && event_, int eventId);
+ void dispatchEvents();
private:
+<<<<<<< HEAD
EventManager() = default;
std::vector<std::pair<std::unique_ptr<Event>, int>> eventsQueue;
std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> subscribers;
std::unordered_map<int, std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>> subscribersByEventId;
+=======
+ EventManager() = default;
+ std::vector<std::pair<std::unique_ptr<Event>, int>> m_eventsQueue;
+ std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>
+ m_subscribers;
+ std::unordered_map<
+ int, std::unordered_map<
+ int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>
+ m_subscribersByEventId;
+>>>>>>> b3b762a34e7ccb4a0dcd041a693ac7180af16002
};
-
-template<typename EventType>
-inline void subscribe(const EventHandler<EventType>& callback, int eventId = 0, const bool unsubscribeOnSuccess = false)
-{
- std::unique_ptr<IEventHandlerWrapper> handler = std::make_unique<EventHandlerWrapper<EventType>>(callback, unsubscribeOnSuccess);
- EventManager::getInstance().subscribe(EventType::getStaticEventType(), std::move(handler), eventId);
+template <typename EventType>
+inline void subscribe(const EventHandler<EventType> & callback, int eventId = 0,
+ const bool unsubscribeOnSuccess = false) {
+ std::unique_ptr<IEventHandlerWrapper> handler
+ = std::make_unique<EventHandlerWrapper<EventType>>(
+ callback, unsubscribeOnSuccess);
+ EventManager::getInstance().subscribe(EventType::getStaticEventType(),
+ std::move(handler), eventId);
}
-template<typename EventType>
-inline void unsubscribe(const EventHandler<EventType>& callback, int eventId = 0)
-{
- const std::string handlerName = callback.target_type().name();
- EventManager::getInstance().unsubscribe(EventType::getStaticEventType(), handlerName, eventId);
+template <typename EventType>
+inline void unsubscribe(const EventHandler<EventType> & callback,
+ int eventId = 0) {
+ const std::string handlerName = callback.target_type().name();
+ EventManager::getInstance().unsubscribe(EventType::getStaticEventType(),
+ handlerName, eventId);
}
-inline void triggerEvent(const Event& triggeredEvent, int eventId = 0)
-{
- EventManager::getInstance().triggerEvent(triggeredEvent, eventId);
+inline void triggerEvent(const Event & triggeredEvent, int eventId = 0) {
+ EventManager::getInstance().triggerEvent(triggeredEvent, eventId);
}
-inline void queueEvent(std::unique_ptr<Event>&& queuedEvent, int eventId = 0)
-{
- EventManager::getInstance().queueEvent(std::forward<std::unique_ptr<Event>>(queuedEvent), eventId);
+inline void queueEvent(std::unique_ptr<Event> && queuedEvent, int eventId = 0) {
+ EventManager::getInstance().queueEvent(
+ std::forward<std::unique_ptr<Event>>(queuedEvent), eventId);
}
diff --git a/mwe/events/include/keyCodes.h b/mwe/events/include/keyCodes.h
index c3a7826..0879efc 100644
--- a/mwe/events/include/keyCodes.h
+++ b/mwe/events/include/keyCodes.h
@@ -1,140 +1,140 @@
#pragma once
+#include <SDL2/SDL.h>
#include <cstdint>
#include <unordered_map>
-#include <SDL2/SDL.h>
using Keycode = uint16_t;
enum : Keycode {
- // From glfw3.h
- Space = 32,
- Apostrophe = 39, /* ' */
- Comma = 44, /* , */
- Minus = 45, /* - */
- Period = 46, /* . */
- Slash = 47, /* / */
+ // From glfw3.h
+ 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 */
+ 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, /* = */
+ 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,
+ 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,
- LeftBracket = 91, /* [ */
- Backslash = 92, /* \ */
- RightBracket = 93, /* ] */
- GraveAccent = 96, /* ` */
+ LeftBracket = 91, /* [ */
+ Backslash = 92, /* \ */
+ RightBracket = 93, /* ] */
+ GraveAccent = 96, /* ` */
- World1 = 161, /* non-US #1 */
- World2 = 162, /* non-US #2 */
+ 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,
- PageUp = 266,
- PageDown = 267,
- Home = 268,
- End = 269,
- CapsLock = 280,
- ScrollLock = 281,
- NumLock = 282,
- PrintScreen = 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,
+ /* Function keys */
+ Escape = 256,
+ Enter = 257,
+ Tab = 258,
+ Backspace = 259,
+ Insert = 260,
+ Delete = 261,
+ Right = 262,
+ Left = 263,
+ Down = 264,
+ Up = 265,
+ PageUp = 266,
+ PageDown = 267,
+ Home = 268,
+ End = 269,
+ CapsLock = 280,
+ ScrollLock = 281,
+ NumLock = 282,
+ PrintScreen = 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,
- KPDecimal = 330,
- KPDivide = 331,
- KPMultiply = 332,
- KPSubtract = 333,
- KPAdd = 334,
- KPEnter = 335,
- KPEqual = 336,
+ /* Keypad */
+ KP0 = 320,
+ KP1 = 321,
+ KP2 = 322,
+ KP3 = 323,
+ KP4 = 324,
+ KP5 = 325,
+ KP6 = 326,
+ KP7 = 327,
+ KP8 = 328,
+ KP9 = 329,
+ KPDecimal = 330,
+ KPDivide = 331,
+ KPMultiply = 332,
+ KPSubtract = 333,
+ KPAdd = 334,
+ KPEnter = 335,
+ KPEqual = 336,
- LeftShift = 340,
- LeftControl = 341,
- LeftAlt = 342,
- LeftSuper = 343,
- RightShift = 344,
- RightControl = 345,
- RightAlt = 346,
- RightSuper = 347,
- Menu = 348
+ LeftShift = 340,
+ LeftControl = 341,
+ LeftAlt = 342,
+ LeftSuper = 343,
+ RightShift = 344,
+ RightControl = 345,
+ RightAlt = 346,
+ RightSuper = 347,
+ Menu = 348
};
// Define the mapping
extern const std::unordered_map<SDL_Keycode, Keycode> sdlToCustom;
diff --git a/mwe/events/include/loopManager.h b/mwe/events/include/loopManager.h
index 5c519d9..baffb94 100644
--- a/mwe/events/include/loopManager.h
+++ b/mwe/events/include/loopManager.h
@@ -1,17 +1,18 @@
#pragma once
+#include "timer.h"
#include "window.h"
#include <SDL2/SDL.h>
-#include "timer.h"
//#include "combinedEvent.h"
+#include "eventHandler.h"
#include "eventManager.h"
#include "loopManager.h"
-#include "eventHandler.h"
class LoopManager {
public:
LoopManager();
void setup();
void loop();
void setRunning(bool running);
+
private:
void processInput();
void update();