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.

450 lines
12KB

  1. #pragma once
  2. #include <vector>
  3. #include <set>
  4. #include <common.hpp>
  5. #include <math.hpp>
  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)
  47. return;
  48. context->propagating = false;
  49. }
  50. bool isPropagating() const {
  51. if (!context)
  52. return true;
  53. return context->propagating;
  54. }
  55. /** Tells the event handler that a particular Widget consumed the event.
  56. You usually want to stop propagation as well, so call consume() instead.
  57. */
  58. void setTarget(widget::Widget* w) const {
  59. if (!context)
  60. return;
  61. context->target = w;
  62. }
  63. widget::Widget* getTarget() const {
  64. if (!context)
  65. return NULL;
  66. return context->target;
  67. }
  68. /** Sets the target Widget and stops propagating.
  69. A NULL Widget may be passed to consume but not set a target.
  70. */
  71. void consume(widget::Widget* w) const {
  72. if (!context)
  73. return;
  74. context->propagating = false;
  75. context->consumed = true;
  76. context->target = w;
  77. }
  78. void unconsume() const {
  79. if (!context)
  80. return;
  81. context->consumed = false;
  82. }
  83. bool isConsumed() const {
  84. if (!context)
  85. return false;
  86. return context->consumed;
  87. }
  88. };
  89. /** An event prototype with a vector position. */
  90. struct PositionBase {
  91. /** The pixel coordinate where the event occurred, relative to the Widget it is called on. */
  92. math::Vec pos;
  93. };
  94. #define RACK_HELD 3
  95. /** An event prototype with a GLFW key. */
  96. struct KeyBase {
  97. /** The key corresponding to what it would be called in its position on a QWERTY US keyboard.
  98. For example, the WASD directional keys used for first-person shooters will always be reported as "WASD", regardless if they say "ZQSD" on an AZERTY keyboard.
  99. You should usually not use these for printable characters such as "Ctrl+V" key commands. Instead, use `keyName`.
  100. You *should* use these for non-printable keys, such as Escape, arrow keys, Home, F1-12, etc.
  101. You should also use this for Enter, Tab, and Space. Although they are printable keys, they do not appear in `keyName`.
  102. See GLFW_KEY_* for the list of possible values.
  103. */
  104. int key;
  105. /** Platform-dependent "software" key code.
  106. This variable is only included for completion. There should be no reason for you to use this.
  107. You should instead use `key` (for non-printable characters) or `keyName` (for printable characters).
  108. Values are platform independent and can change between different keyboards or keyboard layouts on the same OS.
  109. */
  110. int scancode;
  111. /** String containing the lowercase key name, if it produces a printable character.
  112. This is the only variable that correctly represents the label printed on any keyboard layout, whether it's QWERTY, AZERTY, QWERTZ, Dvorak, etc.
  113. For example, if the user presses the key labeled "q" regardless of the key position, `keyName` will be "q".
  114. For non-printable characters this is an empty string.
  115. Enter, Tab, and Space do not give a `keyName`. Use `key` instead.
  116. Shift has no effect on the key name. Shift+1 results in "1", Shift+q results in "q", etc.
  117. */
  118. std::string keyName;
  119. /** The type of event occurring with the key.
  120. Possible values are GLFW_RELEASE, GLFW_PRESS, GLFW_REPEAT, or RACK_HELD.
  121. RACK_HELD is sent every frame while the key is held.
  122. */
  123. int action;
  124. /** Bitwise OR of key modifiers, such as Ctrl or Shift.
  125. Use (mods & RACK_MOD_MASK) == RACK_MOD_CTRL to check for Ctrl on Linux and Windows but Cmd on Mac.
  126. See GLFW_MOD_* for the list of possible values.
  127. */
  128. int mods;
  129. };
  130. /** An event prototype with a Unicode character. */
  131. struct TextBase {
  132. /** Unicode code point of the character */
  133. int codepoint;
  134. };
  135. /** Occurs every frame when the mouse is hovering over a Widget.
  136. Recurses.
  137. Consume this event to allow Enter and Leave to occur.
  138. */
  139. struct Hover : Base, PositionBase {
  140. /** Change in mouse position since the last frame. Can be zero. */
  141. math::Vec mouseDelta;
  142. };
  143. /** Occurs each mouse button press or release.
  144. Recurses.
  145. Consume this event to allow DoubleClick, Select, Deselect, SelectKey, SelectText, DragStart, DragEnd, DragMove, and DragDrop to occur.
  146. */
  147. struct Button : Base, PositionBase {
  148. /** GLFW_MOUSE_BUTTON_LEFT, GLFW_MOUSE_BUTTON_RIGHT, GLFW_MOUSE_BUTTON_MIDDLE, etc. */
  149. int button;
  150. /** GLFW_PRESS or GLFW_RELEASE */
  151. int action;
  152. /** GLFW_MOD_* */
  153. int mods;
  154. };
  155. /** Occurs when the left mouse button is pressed a second time on the same Widget within a time duration.
  156. Must consume the Button event (on left button press) to receive this event.
  157. */
  158. struct DoubleClick : Base {
  159. };
  160. /** Occurs when a key is pressed, released, or repeated while the mouse is hovering a Widget.
  161. Recurses.
  162. */
  163. struct HoverKey : Base, PositionBase, KeyBase {
  164. };
  165. /** Occurs when a character is typed while the mouse is hovering a Widget.
  166. Recurses.
  167. */
  168. struct HoverText : Base, PositionBase, TextBase {
  169. };
  170. /** Occurs when the mouse scroll wheel is moved while the mouse is hovering a Widget.
  171. Recurses.
  172. */
  173. struct HoverScroll : Base, PositionBase {
  174. /** Change of scroll wheel position. */
  175. math::Vec scrollDelta;
  176. };
  177. /** Occurs when a Widget begins consuming the Hover event.
  178. Must consume the Hover event to receive this event.
  179. The target sets `hoveredWidget`, which allows Leave to occur.
  180. */
  181. struct Enter : Base {
  182. };
  183. /** Occurs when a different Widget is entered.
  184. Must consume the Hover event (when a Widget is entered) to receive this event.
  185. */
  186. struct Leave : Base {
  187. };
  188. /** Occurs when a Widget begins consuming the Button press event for the left mouse button.
  189. Must consume the Button event (on left button press) to receive this event.
  190. The target sets `selectedWidget`, which allows SelectText and SelectKey to occur.
  191. */
  192. struct Select : Base {
  193. };
  194. /** Occurs when a different Widget is selected.
  195. Must consume the Button event (on left button press, when the Widget is selected) to receive this event.
  196. */
  197. struct Deselect : Base {
  198. };
  199. /** Occurs when a key is pressed, released, or repeated while a Widget is selected.
  200. Must consume to prevent HoverKey from being triggered.
  201. */
  202. struct SelectKey : Base, KeyBase {
  203. };
  204. /** Occurs when text is typed while a Widget is selected.
  205. Must consume to prevent HoverKey from being triggered.
  206. */
  207. struct SelectText : Base, TextBase {
  208. };
  209. struct DragBase : Base {
  210. /** The mouse button held while dragging. */
  211. int button;
  212. };
  213. /** Occurs when a Widget begins being dragged.
  214. Must consume the Button event (on press) to receive this event.
  215. The target sets `draggedWidget`, which allows DragEnd, DragMove, DragHover, DragEnter, and DragDrop to occur.
  216. */
  217. struct DragStart : DragBase {
  218. };
  219. /** Occurs when a Widget stops being dragged by releasing the mouse button.
  220. Must consume the Button event (on press, when the Widget drag begins) to receive this event.
  221. */
  222. struct DragEnd : DragBase {
  223. };
  224. /** Occurs every frame on the dragged Widget.
  225. Must consume the Button event (on press, when the Widget drag begins) to receive this event.
  226. */
  227. struct DragMove : DragBase {
  228. /** Change in mouse position since the last frame. Can be zero. */
  229. math::Vec mouseDelta;
  230. };
  231. /** Occurs every frame when the mouse is hovering over a Widget while another Widget (possibly the same one) is being dragged.
  232. Recurses.
  233. Consume this event to allow DragEnter and DragLeave to occur.
  234. */
  235. struct DragHover : DragBase, PositionBase {
  236. /** The dragged widget */
  237. widget::Widget* origin = NULL;
  238. /** Change in mouse position since the last frame. Can be zero. */
  239. math::Vec mouseDelta;
  240. };
  241. /** Occurs when the mouse enters a Widget while dragging.
  242. Must consume the DragHover event to receive this event.
  243. The target sets `draggedWidget`, which allows DragLeave to occur.
  244. */
  245. struct DragEnter : DragBase {
  246. /** The dragged widget */
  247. widget::Widget* origin = NULL;
  248. };
  249. /** Occurs when the mouse leaves a Widget while dragging.
  250. Must consume the DragHover event (when the Widget is entered) to receive this event.
  251. */
  252. struct DragLeave : DragBase {
  253. /** The dragged widget */
  254. widget::Widget* origin = NULL;
  255. };
  256. /** Occurs when the mouse button is released over a Widget while dragging.
  257. Must consume the Button event (on release) to receive this event.
  258. */
  259. struct DragDrop : DragBase {
  260. /** The dragged widget */
  261. widget::Widget* origin = NULL;
  262. };
  263. /** Occurs when a selection of files from the operating system is dropped onto a Widget.
  264. Recurses.
  265. */
  266. struct PathDrop : Base, PositionBase {
  267. PathDrop(const std::vector<std::string>& paths) : paths(paths) {}
  268. /** List of file paths in the dropped selection */
  269. const std::vector<std::string>& paths;
  270. };
  271. /** Occurs after a certain action is triggered on a Widget.
  272. The concept of an "action" is defined by the type of Widget.
  273. */
  274. struct Action : Base {
  275. };
  276. /** Occurs after the value of a Widget changes.
  277. The concept of a "value" is defined by the type of Widget.
  278. */
  279. struct Change : Base {
  280. };
  281. /** Occurs when the pixel buffer of this module must be refreshed.
  282. Recurses.
  283. */
  284. struct Dirty : Base {
  285. };
  286. /** Occurs after a Widget's position is set by Widget::setPosition().
  287. */
  288. struct Reposition : Base {
  289. };
  290. /** Occurs after a Widget's size is set by Widget::setSize().
  291. */
  292. struct Resize : Base {
  293. };
  294. /** Occurs after a Widget is added to a parent.
  295. */
  296. struct Add : Base {
  297. };
  298. /** Occurs before a Widget is removed from its parent.
  299. */
  300. struct Remove : Base {
  301. };
  302. /** Occurs after a Widget is shown with Widget::show().
  303. Recurses.
  304. */
  305. struct Show : Base {
  306. };
  307. /** Occurs after a Widget is hidden with Widget::hide().
  308. Recurses.
  309. */
  310. struct Hide : Base {
  311. };
  312. struct State {
  313. widget::Widget* rootWidget = NULL;
  314. /** State widgets
  315. Don't set these directly unless you know what you're doing. Use the set*() methods instead.
  316. */
  317. widget::Widget* hoveredWidget = NULL;
  318. widget::Widget* draggedWidget = NULL;
  319. int dragButton = 0;
  320. widget::Widget* dragHoveredWidget = NULL;
  321. widget::Widget* selectedWidget = NULL;
  322. /** For double-clicking */
  323. double lastClickTime = -INFINITY;
  324. widget::Widget* lastClickedWidget = NULL;
  325. std::set<int> heldKeys;
  326. widget::Widget* getRootWidget() {
  327. return rootWidget;
  328. }
  329. widget::Widget* getHoveredWidget() {
  330. return hoveredWidget;
  331. }
  332. widget::Widget* getDraggedWidget() {
  333. return draggedWidget;
  334. }
  335. widget::Widget* getDragHoveredWidget() {
  336. return dragHoveredWidget;
  337. }
  338. widget::Widget* getSelectedWidget() {
  339. return selectedWidget;
  340. }
  341. void setHovered(widget::Widget* w);
  342. void setDragged(widget::Widget* w, int button);
  343. void setDragHovered(widget::Widget* w);
  344. void setSelected(widget::Widget* w);
  345. /** Prepares a widget for deletion */
  346. void finalizeWidget(widget::Widget* w);
  347. bool handleButton(math::Vec pos, int button, int action, int mods);
  348. bool handleHover(math::Vec pos, math::Vec mouseDelta);
  349. bool handleLeave();
  350. bool handleScroll(math::Vec pos, math::Vec scrollDelta);
  351. bool handleText(math::Vec pos, int codepoint);
  352. bool handleKey(math::Vec pos, int key, int scancode, int action, int mods);
  353. bool handleDrop(math::Vec pos, const std::vector<std::string>& paths);
  354. bool handleDirty();
  355. };
  356. } // namespace event
  357. } // namespace rack