InputEvent.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2023 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_GUI_INPUT_EVENT
  24. #define DFPSR_GUI_INPUT_EVENT
  25. #include "../../math/IVector.h"
  26. #include <functional>
  27. namespace dsr {
  28. class InputEvent {
  29. public:
  30. InputEvent() {}
  31. virtual ~InputEvent() {}
  32. };
  33. enum class KeyboardEventType { KeyDown, KeyUp, KeyType };
  34. // The DsrKey enumeration is convertible to integers and allow certain well defined math operations
  35. // Safe assumptions:
  36. // * DsrKey_0 to DsrKey_9 are guaranteed to be in an increasing serial order (so that "key - DsrKey_0" is the key's number)
  37. // * DsrKey_F1 to DsrKey_F12 are guaranteed to be in an increasing serial order (so that "key - (DsrKey_F1 - 1)" is the key's number)
  38. // * DsrKey_A to DsrKey_Z are guaranteed to be in an increasing serial order
  39. // Characters are case insensitive, because DsrKey refers to the physical key.
  40. // Use the decoded Unicode value in DsrChar if you want to distinguish between upper and lower case or use special characters.
  41. // Control, shift and alt combines left and right sides, because sometimes the system does not say if the key is left or right.
  42. // On MacOS:
  43. // * Both command and control buttons are mapped to DsrKey_Control.
  44. // * Option and alt is the same DsrKey_Alt button on Macintosh keyboards.
  45. // * Function keys F1 to F12 must be mapped to applications and removed from system shortcuts before they are sent to applications.
  46. // * F13 becomes DsrKey_Pause.
  47. // * F16 becomes DsrKey_Insert.
  48. enum DsrKey {
  49. DsrKey_LeftArrow, DsrKey_RightArrow, DsrKey_UpArrow, DsrKey_DownArrow, DsrKey_PageUp, DsrKey_PageDown,
  50. DsrKey_Control, DsrKey_Shift, DsrKey_Alt, DsrKey_Escape, DsrKey_Pause, DsrKey_Space, DsrKey_Tab,
  51. DsrKey_Return, DsrKey_BackSpace, DsrKey_Delete, DsrKey_Insert, DsrKey_Home, DsrKey_End,
  52. DsrKey_0, DsrKey_1, DsrKey_2, DsrKey_3, DsrKey_4, DsrKey_5, DsrKey_6, DsrKey_7, DsrKey_8, DsrKey_9,
  53. DsrKey_F1, DsrKey_F2, DsrKey_F3, DsrKey_F4, DsrKey_F5, DsrKey_F6, DsrKey_F7, DsrKey_F8, DsrKey_F9, DsrKey_F10, DsrKey_F11, DsrKey_F12,
  54. DsrKey_A, DsrKey_B, DsrKey_C, DsrKey_D, DsrKey_E, DsrKey_F, DsrKey_G, DsrKey_H, DsrKey_I, DsrKey_J, DsrKey_K, DsrKey_L, DsrKey_M,
  55. DsrKey_N, DsrKey_O, DsrKey_P, DsrKey_Q, DsrKey_R, DsrKey_S, DsrKey_T, DsrKey_U, DsrKey_V, DsrKey_W, DsrKey_X, DsrKey_Y, DsrKey_Z,
  56. DsrKey_Unhandled
  57. };
  58. class KeyboardEvent : public InputEvent {
  59. public:
  60. // What the user did to the key.
  61. KeyboardEventType keyboardEventType;
  62. // The raw unicode value without any encoding.
  63. DsrChar character;
  64. // Minimal set of keys for portability.
  65. DsrKey dsrKey;
  66. KeyboardEvent(KeyboardEventType keyboardEventType, DsrChar character, DsrKey dsrKey)
  67. : keyboardEventType(keyboardEventType), character(character), dsrKey(dsrKey) {}
  68. };
  69. enum class MouseKeyEnum { NoKey, Left, Right, Middle, ScrollUp, ScrollDown };
  70. enum class MouseEventType { MouseDown, MouseUp, MouseMove, Scroll };
  71. class MouseEvent : public InputEvent {
  72. public:
  73. MouseEventType mouseEventType;
  74. MouseKeyEnum key;
  75. IVector2D position; // Pixel coordinates relative to upper left corner of parent container
  76. MouseEvent(MouseEventType mouseEventType, MouseKeyEnum key, IVector2D position)
  77. : mouseEventType(mouseEventType), key(key), position(position) {}
  78. };
  79. inline MouseEvent operator+(const MouseEvent &old, const IVector2D &offset) {
  80. MouseEvent result = old;
  81. result.position = result.position + offset;
  82. return result;
  83. }
  84. inline MouseEvent operator-(const MouseEvent &old, const IVector2D &offset) {
  85. MouseEvent result = old;
  86. result.position = result.position - offset;
  87. return result;
  88. }
  89. inline MouseEvent operator*(const MouseEvent &old, int scale) {
  90. MouseEvent result = old;
  91. result.position = result.position * scale;
  92. return result;
  93. }
  94. inline MouseEvent operator/(const MouseEvent &old, int scale) {
  95. MouseEvent result = old;
  96. result.position = result.position / scale;
  97. return result;
  98. }
  99. enum class WindowEventType { Close, Redraw };
  100. class WindowEvent : public InputEvent {
  101. public:
  102. WindowEventType windowEventType;
  103. WindowEvent(WindowEventType windowEventType)
  104. : windowEventType(windowEventType) {}
  105. };
  106. // A macro for declaring a virtual callback from the base method.
  107. // Use the getter for registering methods so that they can be forwarded to a wrapper without inheritance.
  108. // Use the actual variable beginning with `callback_` when calling the method from inside.
  109. #define DECLARE_CALLBACK(NAME, LAMBDA) \
  110. decltype(LAMBDA) callback_##NAME = LAMBDA; \
  111. decltype(LAMBDA)& NAME() { return callback_##NAME; }
  112. // The callback types.
  113. using EmptyCallback = std::function<void()>;
  114. using IndexCallback = std::function<void(int index)>;
  115. using SizeCallback = std::function<void(int width, int height)>;
  116. using KeyboardCallback = std::function<void(const KeyboardEvent& event)>;
  117. using MouseCallback = std::function<void(const MouseEvent& event)>;
  118. // The default functions to call until a callback has been selected.
  119. static EmptyCallback emptyCallback = []() {};
  120. static IndexCallback indexCallback = [](int64_t index) {};
  121. static SizeCallback sizeCallback = [](int width, int height) {};
  122. static KeyboardCallback keyboardCallback = [](const KeyboardEvent& event) {};
  123. static MouseCallback mouseCallback = [](const MouseEvent& event) {};
  124. // Conversion to text for easy debugging.
  125. String getName(DsrKey v);
  126. String getName(KeyboardEventType v);
  127. String getName(MouseKeyEnum v);
  128. String getName(MouseEventType v);
  129. String getName(WindowEventType v);
  130. String& string_toStreamIndented(String& target, const DsrKey& source, const ReadableString& indentation);
  131. String& string_toStreamIndented(String& target, const KeyboardEventType& source, const ReadableString& indentation);
  132. String& string_toStreamIndented(String& target, const MouseKeyEnum& source, const ReadableString& indentation);
  133. String& string_toStreamIndented(String& target, const MouseEventType& source, const ReadableString& indentation);
  134. String& string_toStreamIndented(String& target, const WindowEventType& source, const ReadableString& indentation);
  135. String& string_toStreamIndented(String& target, const KeyboardEvent& source, const ReadableString& indentation);
  136. String& string_toStreamIndented(String& target, const MouseEvent& source, const ReadableString& indentation);
  137. String& string_toStreamIndented(String& target, const WindowEvent& source, const ReadableString& indentation);
  138. }
  139. #endif