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.

238 lines
4.7KB

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