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.

279 lines
6.0KB

  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 when an certain action is triggered on a Widget.
  167. */
  168. struct Action : Event {
  169. };
  170. /** Occurs when the value of a Widget changes.
  171. */
  172. struct Change : Event {
  173. };
  174. /** Occurs when the zoom level of a Widget is changed.
  175. Recurses until consumed.
  176. */
  177. struct Zoom : Event {
  178. };
  179. struct State {
  180. widget::Widget *rootWidget = NULL;
  181. /** State widgets
  182. Don't set these directly unless you know what you're doing. Use the set*() methods instead.
  183. */
  184. widget::Widget *hoveredWidget = NULL;
  185. widget::Widget *draggedWidget = NULL;
  186. widget::Widget *dragHoveredWidget = NULL;
  187. widget::Widget *selectedWidget = NULL;
  188. /** For middle-click dragging */
  189. widget::Widget *scrollWidget = NULL;
  190. /** For double-clicking */
  191. double lastClickTime = -INFINITY;
  192. widget::Widget *lastClickedWidget = NULL;
  193. void setHovered(widget::Widget *w);
  194. void setDragged(widget::Widget *w);
  195. void setDragHovered(widget::Widget *w);
  196. void setSelected(widget::Widget *w);
  197. /** Prepares a widget for deletion */
  198. void finalizeWidget(widget::Widget *w);
  199. void handleButton(math::Vec pos, int button, int action, int mods);
  200. void handleHover(math::Vec pos, math::Vec mouseDelta);
  201. void handleLeave();
  202. void handleScroll(math::Vec pos, math::Vec scrollDelta);
  203. void handleText(math::Vec pos, int codepoint);
  204. void handleKey(math::Vec pos, int key, int scancode, int action, int mods);
  205. void handleDrop(math::Vec pos, const std::vector<std::string> &paths);
  206. void handleZoom();
  207. };
  208. } // namespace event
  209. } // namespace rack