GlPortal Class Reference  0.1.1
Source code documentation of the free and open 3D action puzzle game.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InputSource.hpp
Go to the documentation of this file.
1 #ifndef RADIX_INPUT_SOURCE_HPP
2 #define RADIX_INPUT_SOURCE_HPP
3 
4 #include <functional>
5 #include <string>
6 #include <vector>
7 
8 #include <SDL2/SDL.h>
9 
11 
12 namespace radix {
13 
14 class EventDispatcher;
15 
16 class InputSource {
17 protected:
18  std::vector<std::reference_wrapper<EventDispatcher>> dispatchers;
19 
20 public:
21  /* ============ */
22  /* Keyboard */
23  /* ============ */
24  using KeyboardKey = int;
25  using KeyboardModifier = int;
26 
27  struct KeyPressedEvent : public Event {
28  static constexpr StaticEventTypeName TypeName = "radix/InputSource:KeyPressed";
29  const EventTypeName getTypeName() const {
30  return TypeName;
31  }
32  static constexpr StaticEventType Type = TypeNameHash(TypeName);
33  const EventType getType() const {
34  return Type;
35  }
36 
41  : source(source), key(key), mod(mod) {}
42  };
43  struct KeyReleasedEvent : public Event {
44  static constexpr StaticEventTypeName TypeName = "radix/InputSource:KeyReleased";
45  const EventTypeName getTypeName() const {
46  return TypeName;
47  }
48  static constexpr StaticEventType Type = TypeNameHash(TypeName);
49  const EventType getType() const {
50  return Type;
51  }
52 
57  : source(source), key(key), mod(mod) {}
58  };
59 
60  /* ================= */
61  /* Mouse buttons */
62  /* ================= */
63  enum class MouseButton : uint8_t {
64  Left = 0,
65  Right = 1,
66  Middle = 2,
67  Aux1,
68  Aux2,
69  Aux3,
70  Aux4,
71  Aux5,
72  Aux6,
73  Aux7,
74  Aux8,
75  Unknown = 0xFF
76  };
77  struct MouseButtonPressedEvent : public Event {
78  static constexpr StaticEventTypeName TypeName = "radix/InputSource:MouseButtonPressed";
79  const EventTypeName getTypeName() const {
80  return TypeName;
81  }
82  static constexpr StaticEventType Type = TypeNameHash(TypeName);
83  const EventType getType() const {
84  return Type;
85  }
86 
90  : source(source), button(button) {}
91  };
92  struct MouseButtonReleasedEvent : public Event {
93  static constexpr StaticEventTypeName TypeName = "radix/InputSource:MouseButtonReleased";
94  const EventTypeName getTypeName() const {
95  return TypeName;
96  }
97  static constexpr StaticEventType Type = TypeNameHash(TypeName);
98  const EventType getType() const {
99  return Type;
100  }
101 
105  : source(source), button(button) {}
106  };
107 
108  /* =============== */
109  /* Mouse wheel */
110  /* =============== */
111  struct MouseWheelScrolledEvent : public Event {
112  static constexpr StaticEventTypeName TypeName = "radix/InputSource:MouseWheelScrolled";
113  const EventTypeName getTypeName() const {
114  return TypeName;
115  }
116  static constexpr StaticEventType Type = TypeNameHash(TypeName);
117  const EventType getType() const {
118  return Type;
119  }
120 
122  const int x, y;
123  MouseWheelScrolledEvent(InputSource &source, int x, int y)
124  : source(source), x(x), y(y) {}
125  };
126 
127  /* =============== */
128  /* Window */
129  /* =============== */
130  struct WindowShownEvent : public Event {
131  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowShown";
132  const EventTypeName getTypeName() const {
133  return TypeName;
134  }
135  static constexpr StaticEventType Type = TypeNameHash(TypeName);
136  const EventType getType() const {
137  return Type;
138  }
139 
141  Uint32 windowID;
142  WindowShownEvent(InputSource &source, Uint32 windowID)
143  : source(source), windowID(windowID) {}
144  };
145  struct WindowHiddenEvent : public Event {
146  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowHidden";
147  const EventTypeName getTypeName() const {
148  return TypeName;
149  }
150  static constexpr StaticEventType Type = TypeNameHash(TypeName);
151  const EventType getType() const {
152  return Type;
153  }
154 
156  Uint32 windowID;
157  WindowHiddenEvent(InputSource &source, Uint32 windowID)
158  : source(source), windowID(windowID) {}
159  };
160  struct WindowExposedEvent : public Event {
161  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowExposed";
162  const EventTypeName getTypeName() const {
163  return TypeName;
164  }
165  static constexpr StaticEventType Type = TypeNameHash(TypeName);
166  const EventType getType() const {
167  return Type;
168  }
169 
171  Uint32 windowID;
172  WindowExposedEvent(InputSource &source, Uint32 windowID)
173  : source(source), windowID(windowID) {}
174  };
175  struct WindowMovedEvent : public Event {
176  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowMoved";
177  const EventTypeName getTypeName() const {
178  return TypeName;
179  }
180  static constexpr StaticEventType Type = TypeNameHash(TypeName);
181  const EventType getType() const {
182  return Type;
183  }
184 
186  Uint32 windowID;
187  Sint32 to;
188  Sint32 from;
189  WindowMovedEvent(InputSource &source, Uint32 windowID, Sint32 to, Sint32 from)
190  : source(source), windowID(windowID), to(to), from(from) {}
191  };
192  struct WindowResizedEvent : public Event {
193  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowResized";
194  const EventTypeName getTypeName() const {
195  return TypeName;
196  }
197  static constexpr StaticEventType Type = TypeNameHash(TypeName);
198  const EventType getType() const {
199  return Type;
200  }
201 
203  Uint32 windowID;
204  Sint32 x;
205  Sint32 y;
206  WindowResizedEvent(InputSource &source, Uint32 windowID, Sint32 x, Sint32 y)
207  : source(source), windowID(windowID), x(x), y(y) {}
208  };
209  struct WindowSizeChangedEvent : public Event {
210  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowSizeChanged";
211  const EventTypeName getTypeName() const {
212  return TypeName;
213  }
214  static constexpr StaticEventType Type = TypeNameHash(TypeName);
215  const EventType getType() const {
216  return Type;
217  }
218 
220  Uint32 windowID;
221  WindowSizeChangedEvent(InputSource &source, Uint32 windowID)
222  : source(source), windowID(windowID) {}
223  };
224 
225  struct WindowMinimizedEvent : public Event {
226  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowMinimized";
227  const EventTypeName getTypeName() const {
228  return TypeName;
229  }
230  static constexpr StaticEventType Type = TypeNameHash(TypeName);
231  const EventType getType() const {
232  return Type;
233  }
234 
236  Uint32 windowID;
237  WindowMinimizedEvent(InputSource &source, Uint32 windowID)
238  : source(source), windowID(windowID) {}
239  };
240  struct WindowMaximizedEvent : public Event {
241  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowMaximized";
242  const EventTypeName getTypeName() const {
243  return TypeName;
244  }
245  static constexpr StaticEventType Type = TypeNameHash(TypeName);
246  const EventType getType() const {
247  return Type;
248  }
249 
251  Uint32 windowID;
252  WindowMaximizedEvent(InputSource &source, Uint32 windowID)
253  : source(source), windowID(windowID) {}
254  };
255  struct WindowRestoredEvent : public Event {
256  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowRestored";
257  const EventTypeName getTypeName() const {
258  return TypeName;
259  }
260  static constexpr StaticEventType Type = TypeNameHash(TypeName);
261  const EventType getType() const {
262  return Type;
263  }
264 
266  Uint32 windowID;
267  WindowRestoredEvent(InputSource &source, Uint32 windowID)
268  : source(source), windowID(windowID) {}
269  };
270  struct WindowEnterEvent : public Event {
271  static constexpr StaticEventTypeName TypeName = "radix/InputSource:EnterExposed";
272  const EventTypeName getTypeName() const {
273  return TypeName;
274  }
275  static constexpr StaticEventType Type = TypeNameHash(TypeName);
276  const EventType getType() const {
277  return Type;
278  }
279 
281  Uint32 windowID;
282  WindowEnterEvent(InputSource &source, Uint32 windowID)
283  : source(source), windowID(windowID) {}
284  };
285  struct WindowLeaveEvent : public Event {
286  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowLeave";
287  const EventTypeName getTypeName() const {
288  return TypeName;
289  }
290  static constexpr StaticEventType Type = TypeNameHash(TypeName);
291  const EventType getType() const {
292  return Type;
293  }
294 
296  Uint32 windowID;
297  WindowLeaveEvent(InputSource &source, Uint32 windowID)
298  : source(source), windowID(windowID) {}
299  };
300  struct WindowFocusGainedEvent : public Event {
301  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowFocusGained";
302  const EventTypeName getTypeName() const {
303  return TypeName;
304  }
305  static constexpr StaticEventType Type = TypeNameHash(TypeName);
306  const EventType getType() const {
307  return Type;
308  }
309 
311  Uint32 windowID;
312  WindowFocusGainedEvent(InputSource &source, Uint32 windowID)
313  : source(source), windowID(windowID) {}
314  };
315  struct WindowFocusLostEvent : public Event {
316  static constexpr StaticEventTypeName TypeName = "radix/InputSource:FocusLost";
317  const EventTypeName getTypeName() const {
318  return TypeName;
319  }
320  static constexpr StaticEventType Type = TypeNameHash(TypeName);
321  const EventType getType() const {
322  return Type;
323  }
324 
326  Uint32 windowID;
327  WindowFocusLostEvent(InputSource &source, Uint32 windowID)
328  : source(source), windowID(windowID) {}
329  };
330  struct WindowCloseEvent : public Event {
331  static constexpr StaticEventTypeName TypeName = "radix/InputSource:WindowClose";
332  const EventTypeName getTypeName() const {
333  return TypeName;
334  }
335  static constexpr StaticEventType Type = TypeNameHash(TypeName);
336  const EventType getType() const {
337  return Type;
338  }
339 
341  Uint32 windowID;
342  WindowCloseEvent(InputSource &source, Uint32 windowID)
343  : source(source), windowID(windowID) {}
344  };
345 
347  dispatchers.push_back(d);
348  }
350 
351  virtual void processEvents() = 0;
352  virtual void keyPressed(KeyboardKey key, KeyboardModifier mod) = 0;
353  virtual void keyReleased(KeyboardKey key, KeyboardModifier mod) = 0;
354  virtual bool isKeyDown(KeyboardKey key) = 0;
355  virtual std::string getCharBuffer() = 0;
356  virtual void addToBuffer(const std::string &character) = 0;
357  virtual void clearBuffer() = 0;
358  virtual void truncateCharBuffer() = 0;
359  virtual void clear() = 0;
360 };
361 
362 } /* namespace radix */
363 
364 #endif /* RADIX_INPUT_SOURCE_HPP */
std::vector< std::reference_wrapper< EventDispatcher > > dispatchers
Definition: InputSource.hpp:18
InputSource & source
Definition: InputSource.hpp:87
const EventType getType() const
Definition: InputSource.hpp:181
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:241
const KeyboardModifier mod
Definition: InputSource.hpp:55
InputSource & source
Definition: InputSource.hpp:325
InputSource & source
Definition: InputSource.hpp:235
static constexpr StaticEventType Type
Definition: InputSource.hpp:97
const EventTypeName getTypeName() const
Definition: InputSource.hpp:257
Definition: GameController.hpp:7
const MouseButton button
Definition: InputSource.hpp:88
Uint32 windowID
Definition: InputSource.hpp:326
Definition: InputSource.hpp:255
Sint32 x
Definition: InputSource.hpp:204
Uint32 windowID
Definition: InputSource.hpp:341
Definition: InputSource.hpp:285
MouseWheelScrolledEvent(InputSource &source, int x, int y)
Definition: InputSource.hpp:123
Definition: InputSource.hpp:130
static constexpr StaticEventType Type
Definition: InputSource.hpp:197
const EventType getType() const
Definition: InputSource.hpp:246
const EventType getType() const
Definition: InputSource.hpp:306
const EventType getType() const
Definition: InputSource.hpp:33
const EventTypeName getTypeName() const
Definition: InputSource.hpp:242
Definition: InputSource.hpp:92
virtual std::string getCharBuffer()=0
const EventType getType() const
Definition: InputSource.hpp:198
void addDispatcher(EventDispatcher &d)
Definition: InputSource.hpp:346
WindowMaximizedEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:252
const EventType getType() const
Definition: InputSource.hpp:261
static constexpr StaticEventType Type
Definition: InputSource.hpp:214
KeyReleasedEvent(InputSource &source, KeyboardKey key, KeyboardModifier mod)
Definition: InputSource.hpp:56
static constexpr StaticEventType Type
Definition: InputSource.hpp:135
Definition: Event.hpp:15
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:176
Definition: InputSource.hpp:16
const EventType getType() const
Definition: InputSource.hpp:231
const EventType getType() const
Definition: InputSource.hpp:336
Uint32 windowID
Definition: InputSource.hpp:220
Definition: InputSource.hpp:77
InputSource & source
Definition: InputSource.hpp:140
const EventType getType() const
Definition: InputSource.hpp:117
const EventTypeName getTypeName() const
Definition: InputSource.hpp:211
InputSource & source
Definition: InputSource.hpp:185
const EventTypeName getTypeName() const
Definition: InputSource.hpp:287
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:93
const EventTypeName getTypeName() const
Definition: InputSource.hpp:113
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:146
InputSource & source
Definition: InputSource.hpp:155
static constexpr StaticEventType Type
Definition: InputSource.hpp:305
Sint32 y
Definition: InputSource.hpp:205
const EventTypeName getTypeName() const
Definition: InputSource.hpp:177
Definition: InputSource.hpp:27
InputSource & source
Definition: InputSource.hpp:102
Sint32 from
Definition: InputSource.hpp:188
WindowMovedEvent(InputSource &source, Uint32 windowID, Sint32 to, Sint32 from)
Definition: InputSource.hpp:189
const EventTypeName getTypeName() const
Definition: InputSource.hpp:79
Uint32 windowID
Definition: InputSource.hpp:171
InputSource & source
Definition: InputSource.hpp:310
Uint32 windowID
Definition: InputSource.hpp:156
const EventTypeName getTypeName() const
Definition: InputSource.hpp:147
Definition: InputSource.hpp:43
Definition: EventDispatcher.hpp:13
static constexpr StaticEventType Type
Definition: InputSource.hpp:245
Definition: InputSource.hpp:175
Definition: InputSource.hpp:160
static constexpr StaticEventType Type
Definition: InputSource.hpp:260
virtual void clearBuffer()=0
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:131
const EventTypeName getTypeName() const
Definition: InputSource.hpp:162
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:210
const EventTypeName getTypeName() const
Definition: InputSource.hpp:94
const uint32_t StaticEventType
Definition: Event.hpp:10
static constexpr StaticEventType Type
Definition: InputSource.hpp:116
const EventTypeName getTypeName() const
Definition: InputSource.hpp:317
static constexpr StaticEventType Type
Definition: InputSource.hpp:180
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:331
const EventType getType() const
Definition: InputSource.hpp:49
const MouseButton button
Definition: InputSource.hpp:103
Uint32 windowID
Definition: InputSource.hpp:266
WindowRestoredEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:267
static constexpr StaticEventType Type
Definition: InputSource.hpp:275
const EventType getType() const
Definition: InputSource.hpp:83
const EventTypeName getTypeName() const
Definition: InputSource.hpp:272
static constexpr StaticEventType Type
Definition: InputSource.hpp:335
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:301
WindowHiddenEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:157
virtual void truncateCharBuffer()=0
const EventTypeName getTypeName() const
Definition: InputSource.hpp:332
std::string EventTypeName
Definition: Event.hpp:13
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:28
const EventType getType() const
Definition: InputSource.hpp:276
const EventType getType() const
Definition: InputSource.hpp:291
WindowSizeChangedEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:221
WindowMinimizedEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:237
const EventType getType() const
Definition: InputSource.hpp:215
const EventType getType() const
Definition: InputSource.hpp:136
WindowShownEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:142
Definition: InputSource.hpp:300
const EventType getType() const
Definition: InputSource.hpp:166
const EventTypeName getTypeName() const
Definition: InputSource.hpp:302
static constexpr StaticEventType Type
Definition: InputSource.hpp:32
const EventType getType() const
Definition: InputSource.hpp:151
Definition: InputSource.hpp:209
WindowCloseEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:342
Uint32 windowID
Definition: InputSource.hpp:236
InputSource & source
Definition: InputSource.hpp:250
int KeyboardKey
Definition: InputSource.hpp:24
Definition: InputSource.hpp:270
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:271
MouseButtonReleasedEvent(InputSource &source, MouseButton button)
Definition: InputSource.hpp:104
const EventTypeName getTypeName() const
Definition: InputSource.hpp:227
static constexpr StaticEventType Type
Definition: InputSource.hpp:165
Definition: InputSource.hpp:225
const EventTypeName getTypeName() const
Definition: InputSource.hpp:45
InputSource & source
Definition: InputSource.hpp:202
static constexpr StaticEventType Type
Definition: InputSource.hpp:48
InputSource & source
Definition: InputSource.hpp:37
WindowLeaveEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:297
virtual void keyPressed(KeyboardKey key, KeyboardModifier mod)=0
InputSource & source
Definition: InputSource.hpp:53
Definition: InputSource.hpp:192
KeyPressedEvent(InputSource &source, KeyboardKey key, KeyboardModifier mod)
Definition: InputSource.hpp:40
const EventTypeName getTypeName() const
Definition: InputSource.hpp:194
virtual void keyReleased(KeyboardKey key, KeyboardModifier mod)=0
Uint32 windowID
Definition: InputSource.hpp:296
Uint32 windowID
Definition: InputSource.hpp:186
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:286
void removeDispatcher(EventDispatcher &d)
Definition: InputSource.cpp:11
int KeyboardModifier
Definition: InputSource.hpp:25
WindowFocusGainedEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:312
InputSource & source
Definition: InputSource.hpp:295
InputSource & source
Definition: InputSource.hpp:265
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:226
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:193
static constexpr StaticEventType Type
Definition: InputSource.hpp:150
static constexpr EventType TypeNameHash(StaticEventTypeName etn)
Definition: Event.hpp:16
const EventTypeName getTypeName() const
Definition: InputSource.hpp:29
WindowResizedEvent(InputSource &source, Uint32 windowID, Sint32 x, Sint32 y)
Definition: InputSource.hpp:206
const KeyboardModifier mod
Definition: InputSource.hpp:39
static constexpr StaticEventType Type
Definition: InputSource.hpp:230
Uint32 windowID
Definition: InputSource.hpp:251
Definition: InputSource.hpp:240
WindowEnterEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:282
Definition: InputSource.hpp:330
InputSource & source
Definition: InputSource.hpp:170
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:78
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:44
const EventType getType() const
Definition: InputSource.hpp:98
virtual void clear()=0
static constexpr StaticEventType Type
Definition: InputSource.hpp:82
WindowFocusLostEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:327
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:256
WindowExposedEvent(InputSource &source, Uint32 windowID)
Definition: InputSource.hpp:172
uint32_t EventType
Definition: Event.hpp:11
const EventType getType() const
Definition: InputSource.hpp:321
Sint32 to
Definition: InputSource.hpp:187
const int x
Definition: InputSource.hpp:122
Uint32 windowID
Definition: InputSource.hpp:141
Uint32 windowID
Definition: InputSource.hpp:311
const EventTypeName getTypeName() const
Definition: InputSource.hpp:132
static constexpr StaticEventType Type
Definition: InputSource.hpp:320
InputSource & source
Definition: InputSource.hpp:280
const char *const StaticEventTypeName
Definition: Event.hpp:12
virtual bool isKeyDown(KeyboardKey key)=0
Definition: InputSource.hpp:315
InputSource & source
Definition: InputSource.hpp:219
Uint32 windowID
Definition: InputSource.hpp:281
InputSource & source
Definition: InputSource.hpp:340
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:316
MouseButtonPressedEvent(InputSource &source, MouseButton button)
Definition: InputSource.hpp:89
virtual void processEvents()=0
const KeyboardKey key
Definition: InputSource.hpp:38
const int y
Definition: InputSource.hpp:122
Definition: InputSource.hpp:111
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:112
MouseButton
Definition: InputSource.hpp:63
const KeyboardKey key
Definition: InputSource.hpp:54
Definition: InputSource.hpp:145
Uint32 windowID
Definition: InputSource.hpp:203
virtual void addToBuffer(const std::string &character)=0
static constexpr StaticEventType Type
Definition: InputSource.hpp:290
InputSource & source
Definition: InputSource.hpp:121
static constexpr StaticEventTypeName TypeName
Definition: InputSource.hpp:161