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.

389 lines
9.2KB

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