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.

305 lines
6.4KB

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