InputEvent.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. enum DsrKey {
  43. DsrKey_LeftArrow, DsrKey_RightArrow, DsrKey_UpArrow, DsrKey_DownArrow, DsrKey_PageUp, DsrKey_PageDown,
  44. DsrKey_Control, DsrKey_Shift, DsrKey_Alt, DsrKey_Escape, DsrKey_Pause, DsrKey_Space, DsrKey_Tab,
  45. DsrKey_Return, DsrKey_BackSpace, DsrKey_Delete, DsrKey_Insert, DsrKey_Home, DsrKey_End,
  46. DsrKey_0, DsrKey_1, DsrKey_2, DsrKey_3, DsrKey_4, DsrKey_5, DsrKey_6, DsrKey_7, DsrKey_8, DsrKey_9,
  47. DsrKey_F1, DsrKey_F2, DsrKey_F3, DsrKey_F4, DsrKey_F5, DsrKey_F6, DsrKey_F7, DsrKey_F8, DsrKey_F9, DsrKey_F10, DsrKey_F11, DsrKey_F12,
  48. 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,
  49. 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,
  50. // TODO: Add any missing essential keys.
  51. DsrKey_Unhandled
  52. };
  53. class KeyboardEvent : public InputEvent {
  54. public:
  55. // What the user did to the key.
  56. KeyboardEventType keyboardEventType;
  57. // The raw unicode value without any encoding.
  58. DsrChar character;
  59. // Minimal set of keys for portability.
  60. DsrKey dsrKey;
  61. KeyboardEvent(KeyboardEventType keyboardEventType, DsrChar character, DsrKey dsrKey)
  62. : keyboardEventType(keyboardEventType), character(character), dsrKey(dsrKey) {}
  63. };
  64. enum class MouseKeyEnum { NoKey, Left, Right, Middle, ScrollUp, ScrollDown };
  65. enum class MouseEventType { MouseDown, MouseUp, MouseMove, Scroll };
  66. class MouseEvent : public InputEvent {
  67. public:
  68. MouseEventType mouseEventType;
  69. MouseKeyEnum key;
  70. IVector2D position; // Pixel coordinates relative to upper left corner of parent container
  71. MouseEvent(MouseEventType mouseEventType, MouseKeyEnum key, IVector2D position)
  72. : mouseEventType(mouseEventType), key(key), position(position) {}
  73. };
  74. inline MouseEvent operator+(const MouseEvent &old, const IVector2D &offset) {
  75. MouseEvent result = old;
  76. result.position = result.position + offset;
  77. return result;
  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, int scale) {
  85. MouseEvent result = old;
  86. result.position = result.position * scale;
  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. enum class WindowEventType { Close, Redraw };
  95. class WindowEvent : public InputEvent {
  96. public:
  97. WindowEventType windowEventType;
  98. int width, height;
  99. WindowEvent(WindowEventType windowEventType, int width, int height)
  100. : windowEventType(windowEventType), width(width), height(height) {}
  101. };
  102. // A macro for declaring a virtual callback from the base method.
  103. // Use the getter for registering methods so that they can be forwarded to a wrapper without inheritance.
  104. // Use the actual variable beginning with `callback_` when calling the method from inside.
  105. #define DECLARE_CALLBACK(NAME, LAMBDA) \
  106. decltype(LAMBDA) callback_##NAME = LAMBDA; \
  107. decltype(LAMBDA)& NAME() { return callback_##NAME; }
  108. // The callback types.
  109. using EmptyCallback = std::function<void()>;
  110. using IndexCallback = std::function<void(int index)>;
  111. using SizeCallback = std::function<void(int width, int height)>;
  112. using KeyboardCallback = std::function<void(const KeyboardEvent& event)>;
  113. using MouseCallback = std::function<void(const MouseEvent& event)>;
  114. // The default functions to call until a callback has been selected.
  115. static EmptyCallback emptyCallback = []() {};
  116. static IndexCallback indexCallback = [](int64_t index) {};
  117. static SizeCallback sizeCallback = [](int width, int height) {};
  118. static KeyboardCallback keyboardCallback = [](const KeyboardEvent& event) {};
  119. static MouseCallback mouseCallback = [](const MouseEvent& event) {};
  120. // Conversion to text for easy debugging.
  121. String getName(DsrKey v);
  122. String getName(KeyboardEventType v);
  123. String getName(MouseKeyEnum v);
  124. String getName(MouseEventType v);
  125. String getName(WindowEventType v);
  126. String& string_toStreamIndented(String& target, const DsrKey& source, const ReadableString& indentation);
  127. String& string_toStreamIndented(String& target, const KeyboardEventType& source, const ReadableString& indentation);
  128. String& string_toStreamIndented(String& target, const MouseKeyEnum& source, const ReadableString& indentation);
  129. String& string_toStreamIndented(String& target, const MouseEventType& source, const ReadableString& indentation);
  130. String& string_toStreamIndented(String& target, const WindowEventType& source, const ReadableString& indentation);
  131. String& string_toStreamIndented(String& target, const KeyboardEvent& source, const ReadableString& indentation);
  132. String& string_toStreamIndented(String& target, const MouseEvent& source, const ReadableString& indentation);
  133. String& string_toStreamIndented(String& target, const WindowEvent& source, const ReadableString& indentation);
  134. }
  135. #endif