forked from floft/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent.h
279 lines (248 loc) · 6.49 KB
/
event.h
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "platform.h"
#ifndef EVENT_H_INCLUDED
#define EVENT_H_INCLUDED
#include <memory>
/**************
*Description:
*Input:
*Output:
**************/
using namespace std;
class KeyDownEvent;
class MouseUpEvent;
class MouseDownEvent;
class MouseMoveEvent;
class MouseScrollEvent;
class KeyUpEvent;
class KeyPressEvent;
class QuitEvent;
struct EventHandler : public enable_shared_from_this<EventHandler>
{
virtual bool handleMouseUp(MouseUpEvent &event) = 0;
virtual bool handleMouseDown(MouseDownEvent &event) = 0;
virtual bool handleMouseMove(MouseMoveEvent &event) = 0;
virtual bool handleMouseScroll(MouseScrollEvent &event) = 0;
virtual bool handleKeyUp(KeyUpEvent &event) = 0;
virtual bool handleKeyDown(KeyDownEvent &event) = 0;
virtual bool handleKeyPress(KeyPressEvent &event) = 0;
virtual bool handleQuit(QuitEvent &event) = 0;
};
class Event
{
public:
enum Type
{
Type_MouseUp,
Type_MouseDown,
Type_MouseMove,
Type_MouseScroll,
Type_KeyUp,
Type_KeyDown,
Type_KeyPress,
Type_Quit,
};
const Type type;
protected:
Event(Type type)
: type(type)
{
}
public:
virtual bool dispatch(shared_ptr<EventHandler> eventHandler) = 0;
};
class MouseEvent : public Event
{
public:
const float x, y;
const float deltaX, deltaY;
protected:
MouseEvent(Type type, float x, float y, float deltaX, float deltaY)
: Event(type), x(x), y(y), deltaX(deltaX), deltaY(deltaY)
{
}
};
class KeyEvent : public Event
{
public:
const KeyboardKey key;
const KeyboardModifiers mods;
protected:
KeyEvent(Type type, KeyboardKey key, KeyboardModifiers mods): Event(type), key(key), mods(mods)
{
}
};
class KeyDownEvent final : public KeyEvent
{
public:
const bool isRepetition;
KeyDownEvent(KeyboardKey key, KeyboardModifiers mods, bool isRepetition = false) : KeyEvent(Type_KeyDown, key, mods), isRepetition(isRepetition)
{
}
virtual bool dispatch(shared_ptr<EventHandler> eventHandler) override
{
return eventHandler->handleKeyDown(*this);
}
};
class KeyUpEvent : public KeyEvent
{
public:
KeyUpEvent(KeyboardKey key, KeyboardModifiers mods)
: KeyEvent(Type_KeyUp, key, mods)
{
}
virtual bool dispatch(shared_ptr<EventHandler> eventHandler) override
{
return eventHandler->handleKeyUp(*this);
}
};
struct KeyPressEvent : public Event
{
const wchar_t character;
KeyPressEvent(wchar_t character)
: Event(Type_KeyPress), character(character)
{
}
virtual bool dispatch(shared_ptr<EventHandler> eventHandler) override
{
return eventHandler->handleKeyPress(*this);
}
};
struct MouseButtonEvent : public MouseEvent
{
const MouseButton button;
protected:
MouseButtonEvent(Type type, float x, float y, float deltaX, float deltaY, MouseButton button)
: MouseEvent(type, x, y, deltaX, deltaY), button(button)
{
}
};
struct MouseUpEvent : public MouseButtonEvent
{
MouseUpEvent(float x, float y, float deltaX, float deltaY, MouseButton button) : MouseButtonEvent(Type_MouseUp, x, y, deltaX, deltaY, button)
{
}
virtual bool dispatch(shared_ptr<EventHandler> eventHandler) override
{
return eventHandler->handleMouseUp(*this);
}
};
struct MouseDownEvent : public MouseButtonEvent
{
MouseDownEvent(float x, float y, float deltaX, float deltaY, MouseButton button)
: MouseButtonEvent(Type_MouseDown, x, y, deltaX, deltaY, button)
{
}
virtual bool dispatch(shared_ptr<EventHandler> eventHandler) override
{
return eventHandler->handleMouseDown(*this);
}
};
struct MouseMoveEvent : public MouseEvent
{
MouseMoveEvent(float x, float y, float deltaX, float deltaY)
: MouseEvent(Type_MouseMove, x, y, deltaX, deltaY)
{
}
virtual bool dispatch(shared_ptr<EventHandler> eventHandler) override
{
return eventHandler->handleMouseMove(*this);
}
};
struct MouseScrollEvent : public MouseEvent
{
const int scrollX, scrollY;
MouseScrollEvent(float x, float y, float deltaX, float deltaY, int scrollX, int scrollY)
: MouseEvent(Type_MouseScroll, x, y, deltaX, deltaY), scrollX(scrollX), scrollY(scrollY)
{
}
virtual bool dispatch(shared_ptr<EventHandler> eventHandler) override
{
return eventHandler->handleMouseScroll(*this);
}
};
struct QuitEvent : public Event
{
QuitEvent()
: Event(Type_Quit)
{
}
virtual bool dispatch(shared_ptr<EventHandler> eventHandler) override
{
return eventHandler->handleQuit(*this);
}
};
class CombinedEventHandler final : public EventHandler
{
private:
shared_ptr<EventHandler> first, second;
public:
CombinedEventHandler(shared_ptr<EventHandler> first, shared_ptr<EventHandler> second)
: first(first), second(second)
{
}
virtual bool handleMouseUp(MouseUpEvent &event) override
{
if(first->handleMouseUp(event))
{
return true;
}
return second->handleMouseUp(event);
}
virtual bool handleMouseDown(MouseDownEvent &event) override
{
if(first->handleMouseDown(event))
{
return true;
}
return second->handleMouseDown(event);
}
virtual bool handleMouseMove(MouseMoveEvent &event) override
{
if(first->handleMouseMove(event))
{
return true;
}
return second->handleMouseMove(event);
}
virtual bool handleMouseScroll(MouseScrollEvent &event)override
{
if(first->handleMouseScroll(event))
{
return true;
}
return second->handleMouseScroll(event);
}
virtual bool handleKeyUp(KeyUpEvent &event)override
{
if(first->handleKeyUp(event))
{
return true;
}
return second->handleKeyUp(event);
}
virtual bool handleKeyDown(KeyDownEvent &event)override
{
if(first->handleKeyDown(event))
{
return true;
}
return second->handleKeyDown(event);
}
virtual bool handleKeyPress(KeyPressEvent &event)override
{
if(first->handleKeyPress(event))
{
return true;
}
return second->handleKeyPress(event);
}
virtual bool handleQuit(QuitEvent &event)override
{
if(first->handleQuit(event))
{
return true;
}
return second->handleQuit(event);
}
};
#endif // EVENT_H_INCLUDED