rolmodl
C++ API for the Simple Directmedia Library 2 (SDL2)
Event.hpp
1 #pragma once
2 
3 #include "forwarddecl/Event.hpp"
4 
5 #include "Base.hpp"
6 #include "Geom.hpp"
7 #include "Kb.hpp"
8 #include "Mouse.hpp"
9 
10 namespace rolmodl {
11  enum class ButtonState {
12  Up, Down
13  };
14  namespace button_state::unsafe {
15  constexpr ButtonState fromSDLEnum(const uint8_t s) noexcept {
16  if (s == SDL_RELEASED)
17  return ButtonState::Up;
18  // if (s == SDL_PRESSED) // unsafe for a reason
19  return ButtonState::Down;
20  }
21  constexpr uint8_t toSDLEnum(const ButtonState s) noexcept {
22  if (s == ButtonState::Up)
23  return SDL_RELEASED;
24  // if (s == ButtonState::Down)
25  return SDL_PRESSED;
26  }
27  }
28 
29  struct Keysym {
30  public:
31  kb::Key key;
32  kb::Scancode scancode;
33  uint16_t mod; // todo
34  private:
35  };
36 
37  struct HatState {
38  public:
39  enum class Position {
40  TopLeft, Top, TopRight,
41  Left, Center, Right,
42  BottomLeft, Bottom, BottomRight
43  };
44 
45  constexpr explicit HatState(const uint8_t v) noexcept : // fixme:: unsafe! arbitrary default if the SDL enum is invalid
46  data_(unsafe_fromSDLEnum_(v))
47  {}
48  constexpr uint8_t unsafe_toSDLEnum() const {
49  if (data_ == Position::TopLeft)
50  return SDL_HAT_LEFTUP;
51  if (data_ == Position::Top)
52  return SDL_HAT_UP;
53  if (data_ == Position::TopRight)
54  return SDL_HAT_RIGHTUP;
55 
56  if (data_ == Position::Left)
57  return SDL_HAT_LEFT;
58  if (data_ == Position::Center)
59  return SDL_HAT_CENTERED;
60  if (data_ == Position::Right)
61  return SDL_HAT_RIGHTUP;
62 
63  if (data_ == Position::BottomLeft)
64  return SDL_HAT_LEFTDOWN;
65  if (data_ == Position::Bottom)
66  return SDL_HAT_DOWN;
67  // if (data_ == Position::BottomRight)
68  return SDL_HAT_RIGHTDOWN;
69  }
70 
71  constexpr bool pointsLeft() const {
72  return data_ == Position::TopLeft || data_ == Position::Left || data_ == Position::BottomLeft;
73  }
74  constexpr bool pointsRight() const {
75  return data_ == Position::TopRight || data_ == Position::Right || data_ == Position::BottomRight;
76  }
77 
78  constexpr bool pointsUp() const {
79  return data_ == Position::TopLeft || data_ == Position::Top || data_ == Position::TopRight;
80  }
81  constexpr bool pointsDown() const {
82  return data_ == Position::BottomLeft || data_ == Position::Bottom || data_ == Position::BottomRight;
83  }
84 
85  constexpr Position pos() const {
86  return data_;
87  }
88 
89  private:
90  Position data_;
91 
92  constexpr Position unsafe_fromSDLEnum_(const uint8_t v) noexcept {
93  if (v == SDL_HAT_LEFTUP)
94  return Position::TopLeft;
95  else if (v == SDL_HAT_UP)
96  return Position::Top;
97  else if (v == SDL_HAT_RIGHTUP)
98  return Position::TopRight;
99 
100  else if (v == SDL_HAT_LEFT)
101  return Position::Left;
102  else if (v == SDL_HAT_CENTERED)
103  return Position::Center;
104  else if (v == SDL_HAT_RIGHT)
105  return Position::Right;
106 
107  else if (v == SDL_HAT_LEFTDOWN)
108  return Position::BottomLeft;
109  else if (v == SDL_HAT_DOWN)
110  return Position::Bottom;
111  // else if (v == SDL_HAT_RIGHTDOWN)
112  return Position::BottomRight;
113  }
114  };
115  namespace hatState::position {
116  constexpr const char* toString(const HatState::Position p) {
117  if (p == HatState::Position::TopLeft)
118  return "top-left";
119  if (p == HatState::Position::Top)
120  return "top";
121  if (p == HatState::Position::TopRight)
122  return "top-right";
123 
124  if (p == HatState::Position::Left)
125  return "left";
126  if (p == HatState::Position::Center)
127  return "center";
128  if (p == HatState::Position::Right)
129  return "right";
130 
131  if (p == HatState::Position::BottomLeft)
132  return "bottom-left";
133  if (p == HatState::Position::Bottom)
134  return "bottom";
135  // if (p == HatState::Position::BottomRight)
136  return "bottom-right";
137  }
138  }
139 
140  namespace event {
141  struct Timestamped {
142  uint32_t timestamp;
143  };
144 
145  struct WindowSpecific {
146  uint32_t unsafe_winId;
147  };
148 
149  struct Quit : public Timestamped {};
150 
151  // todo: type from SDL_RegisterEvents()
152  struct User : public Timestamped, WindowSpecific {
153  int32_t code;
154  void* data1;
155  void* data2;
156  };
157 
158  struct Edit : public Timestamped, WindowSpecific {
159  char text[32];
160  int32_t start, len;
161  };
162  struct Text : public Timestamped, WindowSpecific {
163  char text[32];
164  };
165 
166  namespace touch {
167  struct Motion : public Timestamped {
168  SDL_TouchID touchId; // todo
169  SDL_FingerID fingerId; // todo
170 
171  geom::XYFloats pos;
172  geom::XYFloats dpos;
173  float pressure;
174  };
175  struct Up : public Timestamped {
176  SDL_TouchID touchId; // todo
177  SDL_FingerID fingerId; // todo
178 
179  geom::XYFloats pos;
180  // geom::XYFloats dpos; // todo: is this always (0, 0)?
181  float pressure;
182  };
183  struct Down : public Timestamped {
184  SDL_TouchID touchId; // todo
185  SDL_FingerID fingerId; // todo
186 
187  geom::XYFloats pos;
188  // geom::XYFloats dpos; // todo: is this always (0, 0)?
189  float pressure;
190  };
191  }
192 
193  namespace key {
194  struct Up : public Timestamped, WindowSpecific {
195  ButtonState state;
196  bool repeat;
197 
198  Keysym sym;
199  };
200  struct Down : public Timestamped, WindowSpecific {
201  ButtonState state;
202  bool repeat;
203 
204  Keysym sym;
205  };
206  }
207 
208  namespace mouse {
209  enum class WheelDirection {
210  Normal, Reverse
211  };
212  namespace wheel_direction::unsafe {
213  constexpr WheelDirection fromSDLEnum(const uint32_t v) noexcept {
214  if (v == SDL_MOUSEWHEEL_NORMAL)
215  return WheelDirection::Normal;
216  // if (v == SDL_MOUSEWHEEL_FLIPPED) // unsafe for a reason
217  return WheelDirection::Reverse;
218  }
219  constexpr uint32_t toSDLEnum(const WheelDirection v) noexcept {
220  if (v == WheelDirection::Normal)
221  return SDL_MOUSEWHEEL_NORMAL;
222  // if (v == WheelDirection::Reverse)
223  return SDL_MOUSEWHEEL_FLIPPED;
224  }
225  }
226 
227  namespace button {
228  struct Up : public Timestamped, WindowSpecific {
229  uint32_t mouseId; // todo: support SDL_TouchMouseID elegantly
230  ::rolmodl::mouse::Btn button;
231  ButtonState state;
232  uint8_t clicks;
233 
234  geom::XYInt32 pos;
235  };
236  struct Down : public Timestamped, WindowSpecific {
237  uint32_t mouseId; // todo: support SDL_TouchMouseID elegantly
238  ::rolmodl::mouse::Btn button;
239  ButtonState state;
240  uint8_t clicks;
241 
242  geom::XYInt32 pos;
243  };
244  }
245  struct Motion : public Timestamped, WindowSpecific {
246  uint32_t mouseId; // todo: support SDL_TouchMouseID elegantly
248 
249  geom::XYInt32 pos;
250  geom::XYInt32 dpos;
251  };
252  struct Wheel : public Timestamped, WindowSpecific {
253  uint32_t mouseId; // todo: support SDL_TouchMouseID elegantly
254 
255  geom::XYInt32 dpos;
256  WheelDirection direction;
257  };
258  }
259 
260  namespace joystick {
261  struct Axis : public Timestamped {
262  SDL_JoystickID joystickId; // todo
263  uint8_t axisN;
264  int16_t x;
265  };
266  struct Ball : public Timestamped {
267  SDL_JoystickID joystickId; // todo
268  uint8_t ballN;
269  int16_t dx, dy;
270  };
271  struct Hat : public Timestamped {
272  SDL_JoystickID joystickId; // todo
273  uint8_t hatN;
274  HatState x;
275  };
276  namespace button {
277  struct Up : public Timestamped {
278  SDL_JoystickID joystickId; // todo
279  uint8_t buttonN;
280  ButtonState state;
281  };
282  struct Down : public Timestamped {
283  SDL_JoystickID joystickId; // todo
284  uint8_t buttonN;
285  ButtonState state;
286  };
287  }
288  namespace device {
289  struct Added : public Timestamped {
290  int32_t joystickId; // todo
291  };
292  struct Removed : public Timestamped {
293  int32_t joystickId; // todo
294  };
295  }
296  }
297 
298  namespace controller {
299  struct Axis : public Timestamped {
300  SDL_JoystickID controllerId; // todo
301  uint8_t axisN; // todo: should be enum
302  int16_t x;
303  };
304  namespace button {
305  struct Up : public Timestamped {
306  SDL_JoystickID controllerId; // todo
307  uint8_t buttonN; // todo: should be enum
308  ButtonState state;
309  };
310  struct Down : public Timestamped {
311  SDL_JoystickID controllerId; // todo
312  uint8_t buttonN; // todo: should be enum
313  ButtonState state;
314  };
315  }
316  namespace device {
317  struct Added : public Timestamped {
318  SDL_JoystickID controllerId; // todo
319  };
320  struct Removed : public Timestamped {
321  SDL_JoystickID controllerId; // todo
322  };
323  struct Remapped : public Timestamped {
324  SDL_JoystickID controllerId; // todo
325  };
326  }
327  }
328 
329  namespace gesture {
330  struct Builtin : public Timestamped {
331  SDL_TouchID touchId; // todo
332 
333  float dRotation;
334  float dPinch;
335 
336  uint16_t nFingers;
337  geom::XYFloats pos;
338  };
339  namespace custom {
340  struct Recorded : public Timestamped {
341  SDL_TouchID touchId; // todo
342  SDL_GestureID gestureId; // todo
343 
344  uint32_t nFingers;
345  float error;
346  geom::XYFloats pos;
347  };
348  struct Detected : public Timestamped {
349  SDL_TouchID touchId; // todo
350  SDL_GestureID gestureId; // todo
351 
352  uint32_t nFingers;
353  float error;
354  geom::XYFloats pos;
355  };
356  }
357  }
358 
359  namespace window {
360  struct Close : public Timestamped, WindowSpecific {};
361  struct HitTest : public Timestamped, WindowSpecific {};
362 
363  struct Shown : public Timestamped, WindowSpecific {};
364  struct Hidden : public Timestamped, WindowSpecific {};
365  struct Exposed : public Timestamped, WindowSpecific {};
366 
367  struct Moved : public Timestamped, WindowSpecific {
368  geom::Pos loc;
369  };
370  struct Resized : public Timestamped, WindowSpecific {
371  geom::Size size;
372  };
374  geom::Size size;
375  };
376 
377  struct Minimized : public Timestamped, WindowSpecific {};
378  struct Maximized : public Timestamped, WindowSpecific {};
379  struct Restored : public Timestamped, WindowSpecific {};
380 
381  namespace mouse {
382  struct Entered : public Timestamped, WindowSpecific {};
383  struct Left : public Timestamped, WindowSpecific {};
384  }
385 
386  namespace focus {
387  struct Gained : public Timestamped, WindowSpecific {};
388  struct Lost : public Timestamped, WindowSpecific {};
389  struct Offered : public Timestamped, WindowSpecific {};
390  }
391  }
392  struct SystemWindow : public Timestamped {
393  SDL_SysWMmsg* msg; // todo?
394  };
395 
396  namespace drag_n_drop {
397  struct File : public Timestamped, WindowSpecific {
398  SDLString path;
399  };
400  struct Text : public Timestamped, WindowSpecific {
401  SDLString x;
402  };
403  struct Begin : public Timestamped, WindowSpecific {};
404  struct Complete : public Timestamped, WindowSpecific {};
405  }
406 
407  namespace audio_device {
408  struct Added : public Timestamped {
409  uint32_t index; // todo
410  bool isCapture;
411  };
412  struct Removed : public Timestamped {
413  uint32_t id; // todo
414  bool isCapture;
415  };
416  }
417  }
418 }
rolmodl::event::drag_n_drop::File
Definition: Event.hpp:397
rolmodl::event::joystick::device::Removed
Definition: Event.hpp:292
rolmodl::event::mouse::button::Up
Definition: Event.hpp:228
rolmodl::event::Edit
Definition: Event.hpp:158
rolmodl::event::window::Close
Definition: Event.hpp:360
rolmodl::geom::XYFloats
float point data type. Used by touch events.
Definition: Geom.hpp:41
rolmodl::event::WindowSpecific
Definition: Event.hpp:145
rolmodl::event::controller::device::Added
Definition: Event.hpp:317
rolmodl::event::joystick::Ball
Definition: Event.hpp:266
rolmodl::event::gesture::Builtin
Definition: Event.hpp:330
rolmodl::event::window::Resized
Definition: Event.hpp:370
rolmodl::event::controller::Axis
Definition: Event.hpp:299
rolmodl::event::window::Exposed
Definition: Event.hpp:365
rolmodl::event::Timestamped
Definition: Event.hpp:141
rolmodl::event::key::Down
Definition: Event.hpp:200
rolmodl::geom::XYInt32
int32 point data type. The value is in pixels. Used by mouse events.
Definition: Geom.hpp:49
rolmodl::event::Quit
Definition: Event.hpp:149
rolmodl::event::window::SizeChanged
Definition: Event.hpp:373
rolmodl::event::touch::Down
Definition: Event.hpp:183
rolmodl::event::touch::Up
Definition: Event.hpp:175
rolmodl::event::joystick::Hat
Definition: Event.hpp:271
rolmodl::geom::Size
int dimensions data type. The value is in pixels. Semantically different from rolmodl::geom::Pos.
Definition: Geom.hpp:31
rolmodl::event::audio_device::Removed
Definition: Event.hpp:412
rolmodl::event::gesture::custom::Detected
Definition: Event.hpp:348
Geom.hpp
rolmodl::SDLString
Container for SDL-owned strings that must be freed with SDL_free.
Definition: Base.hpp:425
rolmodl::event::window::focus::Lost
Definition: Event.hpp:388
rolmodl::event::controller::device::Remapped
Definition: Event.hpp:323
rolmodl::event::window::Restored
Definition: Event.hpp:379
rolmodl::event::controller::button::Down
Definition: Event.hpp:310
rolmodl::event::joystick::button::Up
Definition: Event.hpp:277
rolmodl::event::gesture::custom::Recorded
Definition: Event.hpp:340
rolmodl::event::Text
Definition: Event.hpp:162
rolmodl::event::joystick::button::Down
Definition: Event.hpp:282
rolmodl::event::drag_n_drop::Begin
Definition: Event.hpp:403
rolmodl::event::touch::Motion
Definition: Event.hpp:167
rolmodl::event::SystemWindow
Definition: Event.hpp:392
rolmodl::event::window::Maximized
Definition: Event.hpp:378
rolmodl::event::window::Hidden
Definition: Event.hpp:364
rolmodl::event::joystick::device::Added
Definition: Event.hpp:289
rolmodl::mouse::BtnState
Definition: Mouse.hpp:159
rolmodl::event::window::Shown
Definition: Event.hpp:363
rolmodl::event::drag_n_drop::Complete
Definition: Event.hpp:404
rolmodl
Main namespace.
Definition: Base.cpp:7
rolmodl::event::key::Up
Definition: Event.hpp:194
rolmodl::event::window::Moved
Definition: Event.hpp:367
rolmodl::event::User
Definition: Event.hpp:152
rolmodl::event::window::focus::Gained
Definition: Event.hpp:387
rolmodl::event::joystick::Axis
Definition: Event.hpp:261
rolmodl::event::window::Minimized
Definition: Event.hpp:377
rolmodl::event::controller::device::Removed
Definition: Event.hpp:320
rolmodl::event::window::focus::Offered
Definition: Event.hpp:389
rolmodl::geom::Pos
int point data type. The value is in pixels. Semantically different from rolmodl::geom::Size.
Definition: Geom.hpp:18
rolmodl::HatState
Definition: Event.hpp:37
rolmodl::event::drag_n_drop::Text
Definition: Event.hpp:400
rolmodl::event::window::HitTest
Definition: Event.hpp:361
rolmodl::Keysym
Definition: Event.hpp:29
rolmodl::event::window::mouse::Entered
Definition: Event.hpp:382
rolmodl::event::audio_device::Added
Definition: Event.hpp:408
rolmodl::event::window::mouse::Left
Definition: Event.hpp:383
rolmodl::event::mouse::button::Down
Definition: Event.hpp:236
rolmodl::event::mouse::Motion
Definition: Event.hpp:245
rolmodl::event::mouse::Wheel
Definition: Event.hpp:252
rolmodl::event::controller::button::Up
Definition: Event.hpp:305