diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-02 16:10:22 +0200 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-02 16:10:22 +0200 |
commit | 94d591b69c45470480db477bc4000ef35019e18b (patch) | |
tree | 93732b405747cbd97a5b0aa786f2399823c0661b | |
parent | 4628638f952f882e494788ca1cf880a029eba5ab (diff) |
created event classes and start of eventManager
-rw-r--r-- | mwe/events/include/event.h | 93 | ||||
-rw-r--r-- | mwe/events/include/eventHandler.h | 4 | ||||
-rw-r--r-- | mwe/events/include/eventManager.h | 18 |
3 files changed, 72 insertions, 43 deletions
diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 8774a7d..f5f426a 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -3,6 +3,7 @@ #include <iostream> #include <string> #include <unordered_map> +#include <variant> using Keycode = uint16_t; enum : Keycode { // From glfw3.h @@ -146,62 +147,72 @@ public: return ++id; } }; -class Event { -public: - virtual ~Event() = default; - virtual std::uint32_t GetEventType() const = 0; - - virtual std::string ToString() const { - return std::to_string(GetEventType()); - }; - - bool isHandled { false }; -}; #define EVENT_TYPE(event_type) \ static std::uint32_t GetStaticEventType() \ { \ static std::uint32_t type = UUIDGenerator::GetUniqueID(); \ return type; \ } \ - std::uint32_t GetEventType() const override \ + std::uint32_t GetEventId() const override \ { \ return GetStaticEventType(); \ } +class Event { + public: + Event(std::string eventType) { + eventData["eventType"] = eventType; + } + virtual ~Event() = default; + + virtual std::uint32_t GetEventId() const = 0; + virtual std::string toString() const { + return std::to_string(GetEventId()); + } + + 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::string getType() const; + bool isHandled() const; + void markHandled(); + private: + std::unordered_map<std::string, std::variant<int, std::string, float>> eventData; + bool isHandled = false; +}; class KeyPressedEvent : public Event { + public: + EVENT_TYPE("KeyPressedEvent"); + KeyPressedEvent(int keyCode); + Keycode getKeyCode() const; + int getRepeatCount() const; + private: + Keycode keycode; public: - EVENT_TYPE("KeyPressedEvent"); - - KeyPressedEvent(int keyCode, int repeatCount) - : key(keyCode) - , repeatCount(repeatCount) - { - } - std::string ToString() const override - { - return "KeyPressedEvent KeyPressed" + std::to_string(key); - } - -public: - Keycode key { 0 }; - int repeatCount { 0 }; + Keycode key = 0; + int repeatCount = 0; }; class KeyReleasedEvent : public Event { + public: + EVENT_TYPE("KeyReleasedEvent"); + KeyReleasedEvent(int keyCode); + Keycode getKeyCode(); + private: + Keycode keycode; public: - EVENT_TYPE("KeyReleased"); - - KeyReleasedEvent(int keyCode) - : key(keyCode) - { - } - std::string ToString() const override - { - return "KeyPressedEvent KeyPressed" + std::to_string(key); - } - -public: - Keycode key { 0 }; + Keycode key = 0; }; - +class MousePressedEvent : public Event { + public: + EVENT_TYPE("MousePressedEvent"); + MousePressedEvent(int mouseX,int mouseY); + std::pair<int,int> getMousePosition(); + private: + Keycode keycode; + int mouseX = 0; + int mouseY = 0; + +}; diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h index c17c586..03017dd 100644 --- a/mwe/events/include/eventHandler.h +++ b/mwe/events/include/eventHandler.h @@ -4,7 +4,7 @@ #include <functional> template<typename EventType> using EventHandler = std::function<void(const EventType& e)>; -class EventHandlerWrapperInterface { +class IEventHandlerWrapper{ public: void Exec(const Event& e) { @@ -18,7 +18,7 @@ private: }; template<typename EventType> -class EventHandlerWrapper : public EventHandlerWrapperInterface { +class EventHandlerWrapper : public IEventHandlerWrapper { public: explicit EventHandlerWrapper(const EventHandler<EventType>& handler) : m_handler(handler) diff --git a/mwe/events/include/eventManager.h b/mwe/events/include/eventManager.h new file mode 100644 index 0000000..93def36 --- /dev/null +++ b/mwe/events/include/eventManager.h @@ -0,0 +1,18 @@ +#pragma once +#include <unordered_map> +#include <memory> +#include "event.h" +#include "eventHandler.h" +using EventType = int; +using EventId = int; +class EventManager{ + public: + + void subscribe(EventId eventId,std::unique_ptr<IEventHandlerWrapper>&& handler); + void unsubscribe(EventId eventId, const std::string& handlerName); + void dispatchEvents(); + void triggerEvent(EventId eventId); + void queueEvent(); + private: + std::unordered_map<EventType, std::vector<std::unique_ptr<IEventHandlerWrapper>>> m_subscribers; +}; |