aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/include/event.h
blob: f5f426a5a60a0db89cb444eb158b4d380cb9845d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#pragma once
#include <cstdint>
#include <iostream>
#include <string>
#include <unordered_map>
#include <variant>
using Keycode = uint16_t;
enum : Keycode {
    // 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 */

    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,

    LeftBracket = 91,  /* [ */
    Backslash = 92,    /* \ */
    RightBracket = 93, /* ] */
    GraveAccent = 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,
    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,

    LeftShift = 340,
    LeftControl = 341,
    LeftAlt = 342,
    LeftSuper = 343,
    RightShift = 344,
    RightControl = 345,
    RightAlt = 346,
    RightSuper = 347,
    Menu = 348
};

class UUIDGenerator {
public:
    static std::uint32_t GetUniqueID()
    {
        static std::uint32_t id = 0;
        return ++id;
    }
};
#define EVENT_TYPE(event_type)                  \
    static std::uint32_t GetStaticEventType()   \
    {                                           \
        static std::uint32_t type = UUIDGenerator::GetUniqueID(); \
        return type;                            \
    }                                           \
    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:
    Keycode key = 0;
    int repeatCount = 0;
};

class KeyReleasedEvent : public Event {
	public:
    	EVENT_TYPE("KeyReleasedEvent");
		KeyReleasedEvent(int keyCode);
		Keycode getKeyCode();
	private:
		Keycode keycode;
public:
    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;
   
};