event.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifndef SIMPLE_INTERACTIVE_EVENT_H
  2. #define SIMPLE_INTERACTIVE_EVENT_H
  3. #include "codes.h"
  4. #include <chrono>
  5. #include <variant>
  6. #include <optional>
  7. #include "simple/support/range.hpp"
  8. #include "simple/geom/vector.hpp"
  9. namespace simple::interactive
  10. {
  11. using int2 = simple::geom::vector<int, 2>;
  12. using float2 = simple::geom::vector<float, 2>;
  13. enum class keystate : uint8_t
  14. {
  15. pressed = SDL_PRESSED,
  16. released = SDL_RELEASED
  17. };
  18. #if SDL_VERSION_ATLEAST(2,0,4)
  19. enum class wheel_direction : uint32_t
  20. {
  21. normal = SDL_MOUSEWHEEL_NORMAL,
  22. flipped = SDL_MOUSEWHEEL_FLIPPED
  23. };
  24. #endif
  25. enum class mouse_button : uint8_t
  26. {
  27. left = SDL_BUTTON_LEFT,
  28. right = SDL_BUTTON_RIGHT,
  29. middle = SDL_BUTTON_MIDDLE,
  30. x1 = SDL_BUTTON_X1,
  31. x2 = SDL_BUTTON_X2
  32. };
  33. enum class mouse_button_mask : uint32_t
  34. {
  35. none = 0,
  36. left = SDL_BUTTON_LMASK,
  37. right = SDL_BUTTON_RMASK,
  38. middle = SDL_BUTTON_MMASK,
  39. x1 = SDL_BUTTON_X1MASK,
  40. x2 = SDL_BUTTON_X2MASK
  41. };
  42. using ::operator |;
  43. using ::operator &;
  44. using ::operator &&;
  45. constexpr uint32_t touch_mouse_id = SDL_TOUCH_MOUSEID;
  46. struct event_data
  47. {
  48. std::chrono::milliseconds timestamp;
  49. };
  50. struct window_event_data : public event_data
  51. {
  52. uint32_t window_id;
  53. };
  54. struct key_data : public window_event_data
  55. {
  56. enum keycode keycode;
  57. enum scancode scancode;
  58. keystate state;
  59. uint8_t repeat;
  60. };
  61. struct mouse_data : public window_event_data
  62. {
  63. uint32_t device_id;
  64. int2 position;
  65. };
  66. struct mouse_button_data : public mouse_data
  67. {
  68. mouse_button button;
  69. keystate state;
  70. #if SDL_VERSION_ATLEAST(2,0,2)
  71. uint8_t clicks;
  72. #endif
  73. };
  74. struct mouse_motion_data : public mouse_data
  75. {
  76. int2 motion;
  77. mouse_button_mask button_state;
  78. };
  79. struct mouse_wheel_data : public mouse_data
  80. {
  81. #if SDL_VERSION_ATLEAST(2,0,4)
  82. wheel_direction direction;
  83. #endif
  84. };
  85. struct text_input_data : public window_event_data
  86. {
  87. std::array<char, 32> text;
  88. };
  89. struct text_edit_data : public text_input_data
  90. {
  91. simple::support::range<int> edit_range;
  92. };
  93. struct pointer_data : public event_data
  94. {
  95. int64_t device_id;
  96. int64_t pointer_id;
  97. float2 position;
  98. float2 motion;
  99. float pressure;
  100. };
  101. struct key_event
  102. {
  103. const key_data data;
  104. };
  105. struct key_pressed : key_event
  106. {};
  107. struct key_released : key_event
  108. {};
  109. std::optional<float2> window_normalized_position(const mouse_data& data) noexcept;
  110. std::optional<float2> screen_normalized_position(const mouse_data& data) noexcept;
  111. std::optional<float2> window_normalized_motion(const mouse_motion_data& data) noexcept;
  112. std::optional<float2> screen_normalized_motion(const mouse_motion_data& data) noexcept;
  113. template<typename Data>
  114. struct mouse_position_event
  115. {
  116. const Data data;
  117. auto window_normalized_position() const noexcept { return interactive::window_normalized_position(data); }
  118. auto screen_normalized_position() const noexcept { return interactive::screen_normalized_position(data); }
  119. };
  120. struct mouse_motion : public mouse_position_event<mouse_motion_data>
  121. {
  122. auto window_normalized_motion() const noexcept { return interactive::window_normalized_motion(data); }
  123. auto screen_normalized_motion() const noexcept { return interactive::screen_normalized_motion(data); }
  124. };
  125. struct mouse_wheel
  126. {
  127. const mouse_wheel_data data;
  128. #if SDL_VERSION_ATLEAST(2,0,4)
  129. int2 motion() const noexcept;
  130. #endif
  131. };
  132. struct mouse_button_event : public mouse_position_event<mouse_button_data>
  133. {
  134. };
  135. struct mouse_down : public mouse_button_event
  136. {};
  137. struct mouse_up : public mouse_button_event
  138. {};
  139. struct text_input { const text_input_data data; };
  140. struct text_edit { const text_edit_data data; };
  141. struct pointer_motion { const pointer_data data; };
  142. struct pointer_down { const pointer_data data; };
  143. struct pointer_up { const pointer_data data; };
  144. struct quit_request { const event_data data; };
  145. // TODO: screen normalized position/size getters as with mouse?
  146. struct window_vector_data : public window_event_data { int2 value; };
  147. struct window_shown { const window_event_data data; };
  148. struct window_hidden { const window_event_data data; };
  149. struct window_exposed { const window_event_data data; };
  150. struct window_moved { const window_vector_data data; };
  151. struct window_resized { const window_vector_data data; };
  152. struct window_size_changed { const window_vector_data data; };
  153. struct window_minimized { const window_event_data data; };
  154. struct window_maximized { const window_event_data data; };
  155. struct window_restored { const window_event_data data; };
  156. struct window_entered { const window_event_data data; };
  157. struct window_left { const window_event_data data; };
  158. struct window_focus_gained { const window_event_data data; };
  159. struct window_focus_lost { const window_event_data data; };
  160. struct window_closed { const window_event_data data; };
  161. #if SDL_VERSION_ATLEAST(2,0,5)
  162. struct window_take_focus { const window_event_data data; };
  163. struct window_hit_test { const window_event_data data; };
  164. #endif
  165. using event = std::variant<
  166. key_pressed
  167. ,key_released
  168. ,mouse_down
  169. ,mouse_up
  170. ,mouse_motion
  171. ,mouse_wheel
  172. ,text_input
  173. ,text_edit
  174. ,pointer_motion
  175. ,pointer_down
  176. ,pointer_up
  177. ,quit_request
  178. ,window_shown
  179. ,window_hidden
  180. ,window_exposed
  181. ,window_moved
  182. ,window_resized
  183. ,window_size_changed
  184. ,window_minimized
  185. ,window_maximized
  186. ,window_restored
  187. ,window_entered
  188. ,window_left
  189. ,window_focus_gained
  190. ,window_focus_lost
  191. ,window_closed
  192. #if SDL_VERSION_ATLEAST(2,0,5)
  193. ,window_take_focus
  194. ,window_hit_test
  195. #endif
  196. >;
  197. std::optional<event> next_event() noexcept;
  198. // better to use expected<bool, error>
  199. bool relative_mouse_mode() noexcept;
  200. bool relative_mouse_mode(bool enable) noexcept;
  201. void require_relative_mouse_mode(bool enable);
  202. // dubious API that probably shouldn't be used, see note on implementation
  203. mouse_motion_data last_mouse_state() noexcept;
  204. #if SDL_VERSION_ATLEAST(2,0,4)
  205. // better to use expected<bool, error>
  206. bool mouse_capture(bool enable) noexcept;
  207. void require_mouse_capture(bool enable);
  208. #endif
  209. } // namespace simple::interactive
  210. template<> struct simple::support::define_enum_flags_operators<simple::interactive::mouse_button_mask>
  211. : std::true_type {};
  212. #endif /* end of include guard */