InputEvent.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. inline String getName(DsrKey v);
  54. String& string_toStreamIndented(String& target, const DsrKey& source, const ReadableString& indentation);
  55. class KeyboardEvent : public InputEvent {
  56. public:
  57. // What the user did to the key.
  58. KeyboardEventType keyboardEventType;
  59. // The raw unicode value without any encoding.
  60. DsrChar character;
  61. // Minimal set of keys for portability.
  62. DsrKey dsrKey;
  63. KeyboardEvent(KeyboardEventType keyboardEventType, DsrChar character, DsrKey dsrKey)
  64. : keyboardEventType(keyboardEventType), character(character), dsrKey(dsrKey) {}
  65. };
  66. enum class MouseKeyEnum { NoKey, Left, Right, Middle, ScrollUp, ScrollDown };
  67. enum class MouseEventType { MouseDown, MouseUp, MouseMove, Scroll };
  68. class MouseEvent : public InputEvent {
  69. public:
  70. MouseEventType mouseEventType;
  71. MouseKeyEnum key;
  72. IVector2D position; // Pixel coordinates relative to upper left corner of parent container
  73. MouseEvent(MouseEventType mouseEventType, MouseKeyEnum key, IVector2D position)
  74. : mouseEventType(mouseEventType), key(key), position(position) {}
  75. };
  76. inline MouseEvent operator+(const MouseEvent &old, const IVector2D &offset) {
  77. MouseEvent result = old;
  78. result.position = result.position + offset;
  79. return result;
  80. }
  81. inline MouseEvent operator-(const MouseEvent &old, const IVector2D &offset) {
  82. MouseEvent result = old;
  83. result.position = result.position - offset;
  84. return result;
  85. }
  86. inline MouseEvent operator*(const MouseEvent &old, int scale) {
  87. MouseEvent result = old;
  88. result.position = result.position * scale;
  89. return result;
  90. }
  91. inline MouseEvent operator/(const MouseEvent &old, int scale) {
  92. MouseEvent result = old;
  93. result.position = result.position / scale;
  94. return result;
  95. }
  96. enum class WindowEventType { Close, Redraw };
  97. class WindowEvent : public InputEvent {
  98. public:
  99. WindowEventType windowEventType;
  100. int width, height;
  101. WindowEvent(WindowEventType windowEventType, int width, int height)
  102. : windowEventType(windowEventType), width(width), height(height) {}
  103. };
  104. // A macro for declaring a virtual callback from the base method.
  105. // Use the getter for registering methods so that they can be forwarded to a wrapper without inheritance.
  106. // Use the actual variable beginning with `callback_` when calling the method from inside.
  107. #define DECLARE_CALLBACK(NAME, LAMBDA) \
  108. decltype(LAMBDA) callback_##NAME = LAMBDA; \
  109. decltype(LAMBDA)& NAME() { return callback_##NAME; }
  110. // The callback types.
  111. using EmptyCallback = std::function<void()>;
  112. using IndexCallback = std::function<void(int index)>;
  113. using SizeCallback = std::function<void(int width, int height)>;
  114. using KeyboardCallback = std::function<void(const KeyboardEvent& event)>;
  115. using MouseCallback = std::function<void(const MouseEvent& event)>;
  116. // The default functions to call until a callback has been selected.
  117. static EmptyCallback emptyCallback = []() {};
  118. static IndexCallback indexCallback = [](int64_t index) {};
  119. static SizeCallback sizeCallback = [](int width, int height) {};
  120. static KeyboardCallback keyboardCallback = [](const KeyboardEvent& event) {};
  121. static MouseCallback mouseCallback = [](const MouseEvent& event) {};
  122. }
  123. #endif