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.

265 lines
5.6KB

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