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.

317 lines
6.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. /** A per-event state shared and writable by all widgets that recursively handle an event. */
  9. struct EventContext {
  10. /** The Widget that consumes the event.
  11. This stops propagation of the event if applicable.
  12. */
  13. Widget *consumed = NULL;
  14. };
  15. /** Base class for all events. */
  16. struct Event {
  17. EventContext *context = NULL;
  18. void consume(Widget *w) const {
  19. if (context)
  20. context->consumed = w;
  21. }
  22. Widget *getConsumed() const {
  23. return context ? context->consumed : NULL;
  24. }
  25. };
  26. /** An Event prototype with a vector position. */
  27. struct PositionEvent {
  28. /** The pixel coordinate where the event occurred, relative to the Widget it is called on. */
  29. math::Vec pos;
  30. };
  31. /** An Event prototype with a GLFW key. */
  32. struct KeyEvent {
  33. /** GLFW_KEY_* */
  34. int key;
  35. /** GLFW_KEY_*. You should usually use `key` instead. */
  36. int scancode;
  37. /** GLFW_RELEASE, GLFW_PRESS, or GLFW_REPEAT */
  38. int action;
  39. /** GLFW_MOD_* */
  40. int mods;
  41. };
  42. /** An Event prototype with a Unicode character. */
  43. struct TextEvent {
  44. /** Unicode code point of the character */
  45. int codepoint;
  46. };
  47. // Concrete events
  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 HoverEvent : Event, PositionEvent {
  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 ButtonEvent : Event, PositionEvent {
  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 DoubleClickEvent : 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 HoverKeyEvent : Event, PositionEvent, KeyEvent {
  76. };
  77. /** Occurs when a character is typed while the mouse is hovering a Widget.
  78. Recurses until consumed.
  79. */
  80. struct HoverTextEvent : Event, PositionEvent, TextEvent {
  81. };
  82. /** Occurs when the mouse scroll wheel is moved while the mouse is hovering a Widget.
  83. Recurses until consumed.
  84. */
  85. struct HoverScrollEvent : Event, PositionEvent {
  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 EnterEvent : Event {
  93. };
  94. /** Occurs when a different Widget is entered.
  95. */
  96. struct LeaveEvent : 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 SelectEvent : Event {
  102. };
  103. /** Occurs when a different Widget is selected.
  104. */
  105. struct DeselectEvent : 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 SelectKeyEvent : Event, KeyEvent {
  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 SelectTextEvent : Event, TextEvent {
  116. };
  117. /** Occurs when a Widget begins being dragged.
  118. Must consume to set the widget as dragged.
  119. */
  120. struct DragStartEvent : Event {
  121. };
  122. /** Occurs when a Widget stops being dragged by releasing the mouse button.
  123. */
  124. struct DragEndEvent : Event {
  125. };
  126. /** Occurs every frame on the dragged Widget.
  127. */
  128. struct DragMoveEvent : 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 DragHoverEvent : Event, PositionEvent {
  136. /** The dragged widget */
  137. 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 DragEnterEvent : Event {
  145. /** The dragged widget */
  146. Widget *origin = NULL;
  147. };
  148. /** Occurs when the mouse leaves a Widget while dragging.
  149. */
  150. struct DragLeaveEvent : Event {
  151. /** The dragged widget */
  152. Widget *origin = NULL;
  153. };
  154. /** Occurs when the mouse button is released over a Widget while dragging.
  155. */
  156. struct DragDropEvent : Event {
  157. /** The dragged widget */
  158. 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 PathDropEvent : Event, PositionEvent {
  164. PathDropEvent(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 ActionEvent : 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 ChangeEvent : Event {
  177. };
  178. /** Occurs after the zoom level of a Widget is changed.
  179. Recurses until consumed.
  180. */
  181. struct ZoomEvent : Event {
  182. };
  183. /** Occurs after a Widget's position is set by Widget::setPos().
  184. */
  185. struct RepositionEvent : Event {
  186. };
  187. /** Occurs after a Widget's size is set by Widget::setSize().
  188. */
  189. struct ResizeEvent : Event {
  190. };
  191. /** Occurs after a Widget is added to a parent.
  192. */
  193. struct AddEvent : Event {
  194. };
  195. /** Occurs before a Widget is removed from its parent.
  196. */
  197. struct RemoveEvent : Event {
  198. };
  199. /** Occurs after a Widget is shown with Widget::show().
  200. */
  201. struct ShowEvent : Event {
  202. };
  203. /** Occurs after a Widget is hidden with Widget::hide().
  204. */
  205. struct HideEvent : Event {
  206. };
  207. struct EventState {
  208. Widget *rootWidget = NULL;
  209. /** State widgets
  210. Don't set these directly unless you know what you're doing. Use the set*() methods instead.
  211. */
  212. Widget *hoveredWidget = NULL;
  213. Widget *draggedWidget = NULL;
  214. Widget *dragHoveredWidget = NULL;
  215. Widget *selectedWidget = NULL;
  216. /** For double-clicking */
  217. double lastClickTime = -INFINITY;
  218. Widget *lastClickedWidget = NULL;
  219. void setHovered(Widget *w);
  220. void setDragged(Widget *w);
  221. void setDragHovered(Widget *w);
  222. void setSelected(Widget *w);
  223. /** Prepares a widget for deletion */
  224. void finalizeWidget(Widget *w);
  225. void handleButton(math::Vec pos, int button, int action, int mods);
  226. void handleHover(math::Vec pos, math::Vec mouseDelta);
  227. void handleLeave();
  228. void handleScroll(math::Vec pos, math::Vec scrollDelta);
  229. void handleText(math::Vec pos, int codepoint);
  230. void handleKey(math::Vec pos, int key, int scancode, int action, int mods);
  231. void handleDrop(math::Vec pos, const std::vector<std::string> &paths);
  232. void handleZoom();
  233. };
  234. } // namespace widget
  235. } // namespace rack