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.

270 lines
5.8KB

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