You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
4.6KB

  1. #pragma once
  2. #include "common.hpp"
  3. #include "math.hpp"
  4. #include <vector>
  5. namespace rack {
  6. struct Widget;
  7. namespace event {
  8. /** Base event class */
  9. struct Event {
  10. /** The Widget that consumes the event.
  11. Set to `this` in your event handler method if consumed.
  12. This stops propagation of the event if applicable.
  13. */
  14. Widget *target = NULL;
  15. };
  16. struct Position {
  17. /** The pixel coordinate where the event occurred, relative to the Widget it is called on. */
  18. Vec pos;
  19. };
  20. struct Key {
  21. /** GLFW_KEY_* */
  22. int key;
  23. /** GLFW_KEY_*. You should usually use `key` instead. */
  24. int scancode;
  25. /** GLFW_RELEASE, GLFW_PRESS, or GLFW_REPEAT */
  26. int action;
  27. /** GLFW_MOD_* */
  28. int mods;
  29. };
  30. struct Text {
  31. /** Unicode code point of the character */
  32. int codepoint;
  33. };
  34. /** Occurs every frame when the mouse is hovering over a Widget.
  35. Recurses until consumed.
  36. If `target` is set, other events may occur on that Widget.
  37. */
  38. struct Hover : Event, Position {
  39. /** Change in mouse position since the last frame. Can be zero. */
  40. Vec mouseDelta;
  41. };
  42. /** Occurs each mouse button press or release.
  43. Recurses until consumed.
  44. If `target` is set, other events may occur on that Widget.
  45. */
  46. struct Button : Event, Position {
  47. /** GLFW_MOUSE_BUTTON_LEFT, GLFW_MOUSE_BUTTON_RIGHT, GLFW_MOUSE_BUTTON_MIDDLE, etc. */
  48. int button;
  49. /** GLFW_PRESS or GLFW_RELEASE */
  50. int action;
  51. /** GLFW_MOD_* */
  52. int mods;
  53. };
  54. /** Occurs when a key is pressed while the mouse is hovering a Widget.
  55. Recurses until consumed.
  56. */
  57. struct HoverKey : Event, Position, Key {
  58. };
  59. /** Occurs when a character is typed while the mouse is hovering a Widget.
  60. Recurses until consumed.
  61. */
  62. struct HoverText : Event, Position, Text {
  63. };
  64. /** Occurs when the mouse scroll wheel is moved while the mouse is hovering a Widget.
  65. Recurses until consumed.
  66. */
  67. struct HoverScroll : Event, Position {
  68. /** Change of scroll wheel position. */
  69. Vec scrollDelta;
  70. };
  71. /** Occurs when a Widget begins consuming the Hover event.
  72. */
  73. struct Enter : Event {
  74. };
  75. /** Occurs when a different Widget is entered.
  76. */
  77. struct Leave : Event {
  78. };
  79. /** Occurs when a Widget begins consuming the Button press event.
  80. */
  81. struct Select : Event {
  82. };
  83. /** Occurs when a different Widget is selected.
  84. */
  85. struct Deselect : Event {
  86. };
  87. /** Occurs when a key is pressed while a Widget is selected.
  88. If consumed, a HoverKey event will not be triggered.
  89. */
  90. struct SelectKey : Event, Key {
  91. };
  92. /** Occurs when text is typed while a Widget is selected.
  93. */
  94. struct SelectText : Event, Text {
  95. };
  96. /** Occurs when a Widget begins being dragged.
  97. Must consume to allow the drag to occur.
  98. */
  99. struct DragStart : Event {
  100. };
  101. /** Occurs when a Widget stops being dragged by releasing the mouse button.
  102. */
  103. struct DragEnd : Event {
  104. };
  105. /** Occurs every frame on the dragged Widget.
  106. `mouseDelta` may be zero.
  107. */
  108. struct DragMove : Event {
  109. Vec mouseDelta;
  110. };
  111. /** Occurs every frame when the mouse is hovering over a Widget while dragging.
  112. Must consume to allow DragEnter, DragLeave, and DragDrop to occur.
  113. */
  114. struct DragHover : Event, Position {
  115. /** Change in mouse position since the last frame. Can be zero. */
  116. Vec mouseDelta;
  117. };
  118. /** Occurs when the mouse enters a Widget while dragging.
  119. */
  120. struct DragEnter : Event {
  121. Widget *origin = NULL;
  122. };
  123. /** Occurs when the mouse leaves a Widget while dragging.
  124. */
  125. struct DragLeave : Event {
  126. Widget *origin = NULL;
  127. };
  128. /** Occurs when the mouse button is released over a Widget while dragging.
  129. */
  130. struct DragDrop : Event {
  131. Widget *origin = NULL;
  132. };
  133. /** Occurs when a selection of files from the operating system are dropped onto a Widget.
  134. */
  135. struct PathDrop : Event, Position {
  136. /** List of file paths in the dropped selection */
  137. std::vector<std::string> paths;
  138. };
  139. /** Occurs when an certain action is triggered on a Widget.
  140. */
  141. struct Action : Event {
  142. };
  143. /** Occurs when the value of a Widget changes.
  144. */
  145. struct Change : Event {
  146. };
  147. /** Occurs when the zoom level of a Widget is changed.
  148. Recurses.
  149. */
  150. struct Zoom : Event {
  151. };
  152. struct Context {
  153. Widget *rootWidget = NULL;
  154. Widget *hoveredWidget = NULL;
  155. Widget *draggedWidget = NULL;
  156. Widget *dragHoveredWidget = NULL;
  157. Widget *selectedWidget = NULL;
  158. /** For middle-click dragging */
  159. Widget *scrollWidget = NULL;
  160. void handleButton(Vec pos, int button, int action, int mods);
  161. void handleHover(Vec pos, Vec mouseDelta);
  162. void handleLeave();
  163. void handleScroll(Vec pos, Vec scrollDelta);
  164. void handleText(Vec pos, int codepoint);
  165. void handleKey(Vec pos, int key, int scancode, int action, int mods);
  166. void handleDrop(Vec pos, std::vector<std::string> paths);
  167. void handleZoom();
  168. /** Prepares a widget for deletion */
  169. void finalizeWidget(Widget *w);
  170. };
  171. } // namespace event
  172. } // namespace rack