aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/include/event.h
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-04 08:28:18 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-04 08:28:18 +0100
commit06f65659fc6ffde7cabd2135040cbfbf089e5a24 (patch)
treee3570bea52b87b6919550ee81d17927ccbc11cc5 /mwe/events/include/event.h
parent128969619a22dfc17a9ea35335c0d21c6ad0c954 (diff)
parent6aa8fdd04728b6a499f526de727514ae3d0490b4 (diff)
merge `origin/master` into `master`
Diffstat (limited to 'mwe/events/include/event.h')
-rw-r--r--mwe/events/include/event.h103
1 files changed, 103 insertions, 0 deletions
diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h
new file mode 100644
index 0000000..730ee4b
--- /dev/null
+++ b/mwe/events/include/event.h
@@ -0,0 +1,103 @@
+#pragma once
+#include "customTypes.h"
+#include "keyCodes.h"
+#include <cstdint>
+#include <iostream>
+#include <string>
+#include <unordered_map>
+#include <variant>
+
+class UUIDGenerator {
+public:
+ 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(); \
+ }
+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);
+
+ std::variant<int, std::string, float>
+ getArgument(const std::string & key) const;
+
+ 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;
+};
+
+// KeyPressedEvent class
+class KeyPressedEvent : public Event {
+public:
+ KeyPressedEvent(int keyCode);
+
+ REGISTER_EVENT_TYPE("KeyPressedEvent");
+
+ Keycode getKeyCode() const;
+ int getRepeatCount() const;
+
+private:
+ Keycode keycode;
+
+public:
+ Keycode key = 0;
+ int repeatCount = 0;
+};
+
+// KeyReleasedEvent class
+class KeyReleasedEvent : public Event {
+public:
+ KeyReleasedEvent(int keyCode);
+
+ REGISTER_EVENT_TYPE(KeyReleasedEvent);
+
+ Keycode getKeyCode() const;
+
+private:
+ Keycode key = 0;
+};
+
+// MousePressedEvent class
+class MousePressedEvent : public Event {
+public:
+ MousePressedEvent(int mouseX, int mouseY);
+
+ REGISTER_EVENT_TYPE(MousePressedEvent)
+
+ std::pair<int, int> getMousePosition() const;
+
+private:
+ int mouseX = 0;
+ int mouseY = 0;
+};
+class CollisionEvent : public Event {
+public:
+ CollisionEvent(Collision);
+
+ REGISTER_EVENT_TYPE(CollisionEvent)
+
+ Collision getCollisionData() const;
+
+private:
+ Collision collisionData;
+};