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.

353 lines
7.9KB

  1. #pragma once
  2. #include "common.hpp"
  3. #include "math.hpp"
  4. #include <vector>
  5. namespace rack {
  6. namespace widget {
  7. struct Widget;
  8. }
  9. namespace event {
  10. /** A per-event state shared and writable by all widgets that recursively handle an event. */
  11. struct Context {
  12. /** Whether the event should continue recursing to children Widgets. */
  13. bool propagating = true;
  14. /** Whether the event has been consumed by an event handler and no more handlers should consume the event. */
  15. bool consumed = false;
  16. /** The widget that responded to the event. */
  17. widget::Widget *target = NULL;
  18. };
  19. /** Base class for all events. */
  20. struct Base {
  21. Context *context = NULL;
  22. /** Prevents the event from being handled by more Widgets.
  23. */
  24. void stopPropagating() const {
  25. if (!context) return;
  26. context->propagating = false;
  27. }
  28. bool isPropagating() const {
  29. if (!context) return true;
  30. return context->propagating;
  31. }
  32. /** Tells the event handler that a particular Widget consumed the event.
  33. You usually want to stop propagation as well, so call consume() instead.
  34. */
  35. void setTarget(widget::Widget *w) const {
  36. if (!context) return;
  37. context->target = w;
  38. }
  39. widget::Widget *getTarget() const {
  40. if (!context) return NULL;
  41. return context->target;
  42. }
  43. /** Sets the target Widget and stops propagating.
  44. A NULL Widget may be passed to consume but not set a target.
  45. */
  46. void consume(widget::Widget *w) const {
  47. if (!context) return;
  48. context->propagating = false;
  49. context->consumed = true;
  50. context->target = w;
  51. }
  52. bool isConsumed() const {
  53. if (!context) return false;
  54. return context->consumed;
  55. }
  56. };
  57. /** An event prototype with a vector position. */
  58. struct PositionBase {
  59. /** The pixel coordinate where the event occurred, relative to the Widget it is called on. */
  60. math::Vec pos;
  61. };
  62. /** An event prototype with a GLFW key. */
  63. struct KeyBase {
  64. /** GLFW_KEY_* */
  65. int key;
  66. /** GLFW_KEY_*. You should usually use `key` instead. */
  67. int scancode;
  68. /** GLFW_RELEASE, GLFW_PRESS, or GLFW_REPEAT */
  69. int action;
  70. /** GLFW_MOD_* */
  71. int mods;
  72. };
  73. /** An event prototype with a Unicode character. */
  74. struct TextBase {
  75. /** Unicode code point of the character */
  76. int codepoint;
  77. };
  78. /** Occurs every frame when the mouse is hovering over a Widget.
  79. Recurses until consumed.
  80. If `target` is set, other events may occur on that Widget.
  81. */
  82. struct Hover : Base, PositionBase {
  83. /** Change in mouse position since the last frame. Can be zero. */
  84. math::Vec mouseDelta;
  85. };
  86. /** Occurs each mouse button press or release.
  87. Recurses until consumed.
  88. If `target` is set, other events may occur on that Widget.
  89. */
  90. struct Button : Base, PositionBase {
  91. /** GLFW_MOUSE_BUTTON_LEFT, GLFW_MOUSE_BUTTON_RIGHT, GLFW_MOUSE_BUTTON_MIDDLE, etc. */
  92. int button;
  93. /** GLFW_PRESS or GLFW_RELEASE */
  94. int action;
  95. /** GLFW_MOD_* */
  96. int mods;
  97. };
  98. /** Occurs when the left mouse button is pressed the second time within a time and position window.
  99. */
  100. struct DoubleClick : Base {
  101. };
  102. /** Occurs when a key is pressed, released, or repeated while the mouse is hovering a Widget.
  103. Recurses until consumed.
  104. */
  105. struct HoverKey : Base, PositionBase, KeyBase {
  106. };
  107. /** Occurs when a character is typed while the mouse is hovering a Widget.
  108. Recurses until consumed.
  109. */
  110. struct HoverText : Base, PositionBase, TextBase {
  111. };
  112. /** Occurs when the mouse scroll wheel is moved while the mouse is hovering a Widget.
  113. Recurses until consumed.
  114. */
  115. struct HoverScroll : Base, PositionBase {
  116. /** Change of scroll wheel position. */
  117. math::Vec scrollDelta;
  118. };
  119. /** Occurs when a Widget begins consuming the Hover event.
  120. Must consume to set the widget as hovered.
  121. */
  122. struct Enter : Base {
  123. };
  124. /** Occurs when a different Widget is entered.
  125. */
  126. struct Leave : Base {
  127. };
  128. /** Occurs when a Widget begins consuming the Button press event for the left mouse button.
  129. Must consume to set the widget as selected.
  130. */
  131. struct Select : Base {
  132. };
  133. /** Occurs when a different Widget is selected.
  134. */
  135. struct Deselect : Base {
  136. };
  137. /** Occurs when a key is pressed, released, or repeated while a Widget is selected.
  138. If consumed, a HoverKey event will not be triggered.
  139. */
  140. struct SelectKey : Base, KeyBase {
  141. };
  142. /** Occurs when text is typed while a Widget is selected.
  143. If consumed, a HoverText event will not be triggered.
  144. */
  145. struct SelectText : Base, TextBase {
  146. };
  147. struct DragBase : Base {
  148. /** The mouse button held during the drag. */
  149. int button;
  150. };
  151. /** Occurs when a Widget begins being dragged.
  152. Must consume to set the widget as dragged.
  153. */
  154. struct DragStart : DragBase {
  155. };
  156. /** Occurs when a Widget stops being dragged by releasing the mouse button.
  157. */
  158. struct DragEnd : DragBase {
  159. };
  160. /** Occurs every frame on the dragged Widget.
  161. */
  162. struct DragMove : DragBase {
  163. /** Change in mouse position since the last frame. Can be zero. */
  164. math::Vec mouseDelta;
  165. };
  166. /** Occurs every frame when the mouse is hovering over a Widget while another Widget (possibly the same one) is being dragged.
  167. Recurses until consumed.
  168. */
  169. struct DragHover : DragBase, PositionBase {
  170. /** The dragged widget */
  171. widget::Widget *origin = NULL;
  172. /** Change in mouse position since the last frame. Can be zero. */
  173. math::Vec mouseDelta;
  174. };
  175. /** Occurs when the mouse enters a Widget while dragging.
  176. Must consume to set the widget as drag-hovered.
  177. */
  178. struct DragEnter : DragBase {
  179. /** The dragged widget */
  180. widget::Widget *origin = NULL;
  181. };
  182. /** Occurs when the mouse leaves a Widget while dragging.
  183. */
  184. struct DragLeave : DragBase {
  185. /** The dragged widget */
  186. widget::Widget *origin = NULL;
  187. };
  188. /** Occurs when the mouse button is released over a Widget while dragging.
  189. */
  190. struct DragDrop : DragBase {
  191. /** The dragged widget */
  192. widget::Widget *origin = NULL;
  193. };
  194. /** Occurs when a selection of files from the operating system is dropped onto a Widget.
  195. Recurses until consumed.
  196. */
  197. struct PathDrop : Base, PositionBase {
  198. PathDrop(const std::vector<std::string> &paths) : paths(paths) {}
  199. /** List of file paths in the dropped selection */
  200. const std::vector<std::string> &paths;
  201. };
  202. /** Occurs after a certain action is triggered on a Widget.
  203. The concept of an "action" is dependent on the type of Widget.
  204. */
  205. struct Action : Base {
  206. };
  207. /** Occurs after the value of a Widget changes.
  208. The concept of a "value" is dependent on the type of Widget.
  209. */
  210. struct Change : Base {
  211. };
  212. /** Occurs after the zoom level of a Widget is changed.
  213. Recurses until consumed.
  214. */
  215. struct Zoom : Base {
  216. };
  217. /** Occurs after a Widget's position is set by Widget::setPos().
  218. */
  219. struct Reposition : Base {
  220. };
  221. /** Occurs after a Widget's size is set by Widget::setSize().
  222. */
  223. struct Resize : Base {
  224. };
  225. /** Occurs after a Widget is added to a parent.
  226. */
  227. struct Add : Base {
  228. };
  229. /** Occurs before a Widget is removed from its parent.
  230. */
  231. struct Remove : Base {
  232. };
  233. /** Occurs after a Widget is shown with Widget::show().
  234. */
  235. struct Show : Base {
  236. };
  237. /** Occurs after a Widget is hidden with Widget::hide().
  238. */
  239. struct Hide : Base {
  240. };
  241. struct State {
  242. widget::Widget *rootWidget = NULL;
  243. /** State widgets
  244. Don't set these directly unless you know what you're doing. Use the set*() methods instead.
  245. */
  246. widget::Widget *hoveredWidget = NULL;
  247. widget::Widget *draggedWidget = NULL;
  248. int dragButton = 0;
  249. widget::Widget *dragHoveredWidget = NULL;
  250. widget::Widget *selectedWidget = NULL;
  251. /** For double-clicking */
  252. double lastClickTime = -INFINITY;
  253. widget::Widget *lastClickedWidget = NULL;
  254. void setHovered(widget::Widget *w);
  255. void setDragged(widget::Widget *w, int button);
  256. void setDragHovered(widget::Widget *w);
  257. void setSelected(widget::Widget *w);
  258. /** Prepares a widget for deletion */
  259. void finalizeWidget(widget::Widget *w);
  260. void handleButton(math::Vec pos, int button, int action, int mods);
  261. void handleHover(math::Vec pos, math::Vec mouseDelta);
  262. void handleLeave();
  263. void handleScroll(math::Vec pos, math::Vec scrollDelta);
  264. void handleText(math::Vec pos, int codepoint);
  265. void handleKey(math::Vec pos, int key, int scancode, int action, int mods);
  266. void handleDrop(math::Vec pos, const std::vector<std::string> &paths);
  267. void handleZoom();
  268. };
  269. } // namespace event
  270. } // namespace rack