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.

303 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. 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 the left mouse button is pressed the second time within a time and position window.
  67. */
  68. struct DoubleClick : Event {
  69. };
  70. /** Occurs when a key is pressed, released, or repeated while the mouse is hovering a Widget.
  71. Recurses until consumed.
  72. */
  73. struct HoverKey : Event, Position, Key {
  74. };
  75. /** Occurs when a character is typed while the mouse is hovering a Widget.
  76. Recurses until consumed.
  77. */
  78. struct HoverText : Event, Position, Text {
  79. };
  80. /** Occurs when the mouse scroll wheel is moved while the mouse is hovering a Widget.
  81. Recurses until consumed.
  82. */
  83. struct HoverScroll : Event, Position {
  84. /** Change of scroll wheel position. */
  85. math::Vec scrollDelta;
  86. };
  87. /** Occurs when a Widget begins consuming the Hover event.
  88. Must consume to set the widget as hovered.
  89. */
  90. struct Enter : Event {
  91. };
  92. /** Occurs when a different Widget is entered.
  93. */
  94. struct Leave : Event {
  95. };
  96. /** Occurs when a Widget begins consuming the Button press event.
  97. Must consume to set the widget as selected.
  98. */
  99. struct Select : Event {
  100. };
  101. /** Occurs when a different Widget is selected.
  102. */
  103. struct Deselect : Event {
  104. };
  105. /** Occurs when a key is pressed, released, or repeated while a Widget is selected.
  106. If consumed, a HoverKey event will not be triggered.
  107. */
  108. struct SelectKey : Event, Key {
  109. };
  110. /** Occurs when text is typed while a Widget is selected.
  111. If consumed, a HoverText event will not be triggered.
  112. */
  113. struct SelectText : Event, Text {
  114. };
  115. /** Occurs when a Widget begins being dragged.
  116. Must consume to set the widget as dragged.
  117. */
  118. struct DragStart : Event {
  119. };
  120. /** Occurs when a Widget stops being dragged by releasing the mouse button.
  121. */
  122. struct DragEnd : Event {
  123. };
  124. /** Occurs every frame on the dragged Widget.
  125. */
  126. struct DragMove : Event {
  127. /** Change in mouse position since the last frame. Can be zero. */
  128. math::Vec mouseDelta;
  129. };
  130. /** Occurs every frame when the mouse is hovering over a Widget while another Widget (possibly the same one) is being dragged.
  131. Recurses until consumed.
  132. */
  133. struct DragHover : Event, Position {
  134. /** The dragged widget */
  135. widget::Widget *origin = NULL;
  136. /** Change in mouse position since the last frame. Can be zero. */
  137. math::Vec mouseDelta;
  138. };
  139. /** Occurs when the mouse enters a Widget while dragging.
  140. Must consume to set the widget as drag-hovered.
  141. */
  142. struct DragEnter : Event {
  143. /** The dragged widget */
  144. widget::Widget *origin = NULL;
  145. };
  146. /** Occurs when the mouse leaves a Widget while dragging.
  147. */
  148. struct DragLeave : Event {
  149. /** The dragged widget */
  150. widget::Widget *origin = NULL;
  151. };
  152. /** Occurs when the mouse button is released over a Widget while dragging.
  153. */
  154. struct DragDrop : Event {
  155. /** The dragged widget */
  156. widget::Widget *origin = NULL;
  157. };
  158. /** Occurs when a selection of files from the operating system is dropped onto a Widget.
  159. Recurses until consumed.
  160. */
  161. struct PathDrop : Event, Position {
  162. PathDrop(const std::vector<std::string> &paths) : paths(paths) {}
  163. /** List of file paths in the dropped selection */
  164. const std::vector<std::string> &paths;
  165. };
  166. /** Occurs after a certain action is triggered on a Widget.
  167. The concept of an "action" is dependent on the type of Widget.
  168. */
  169. struct Action : Event {
  170. };
  171. /** Occurs after the value of a Widget changes.
  172. The concept of a "value" is dependent on the type of Widget.
  173. */
  174. struct Change : Event {
  175. };
  176. /** Occurs after the zoom level of a Widget is changed.
  177. Recurses until consumed.
  178. */
  179. struct Zoom : Event {
  180. };
  181. /** Occurs after the Widget's position is set by setPos();
  182. */
  183. struct Reposition : Event {
  184. };
  185. /** Occurs after the Widget's size is set by setSize();
  186. */
  187. struct Resize : Event {
  188. };
  189. /** Occurs after the Widget is added to a parent.
  190. */
  191. struct Add : Event {
  192. };
  193. /** Occurs before the Widget is remove from its parent.
  194. */
  195. struct Remove : Event {
  196. };
  197. struct State {
  198. widget::Widget *rootWidget = NULL;
  199. /** State widgets
  200. Don't set these directly unless you know what you're doing. Use the set*() methods instead.
  201. */
  202. widget::Widget *hoveredWidget = NULL;
  203. widget::Widget *draggedWidget = NULL;
  204. widget::Widget *dragHoveredWidget = NULL;
  205. widget::Widget *selectedWidget = NULL;
  206. /** For double-clicking */
  207. double lastClickTime = -INFINITY;
  208. widget::Widget *lastClickedWidget = NULL;
  209. void setHovered(widget::Widget *w);
  210. void setDragged(widget::Widget *w);
  211. void setDragHovered(widget::Widget *w);
  212. void setSelected(widget::Widget *w);
  213. /** Prepares a widget for deletion */
  214. void finalizeWidget(widget::Widget *w);
  215. void handleButton(math::Vec pos, int button, int action, int mods);
  216. void handleHover(math::Vec pos, math::Vec mouseDelta);
  217. void handleLeave();
  218. void handleScroll(math::Vec pos, math::Vec scrollDelta);
  219. void handleText(math::Vec pos, int codepoint);
  220. void handleKey(math::Vec pos, int key, int scancode, int action, int mods);
  221. void handleDrop(math::Vec pos, const std::vector<std::string> &paths);
  222. void handleZoom();
  223. };
  224. } // namespace event
  225. } // namespace rack