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.

263 lines
5.4KB

  1. #pragma once
  2. #include "common.hpp"
  3. #include "math.hpp"
  4. #include <vector>
  5. namespace rack {
  6. struct Widget;
  7. namespace event {
  8. struct Context {
  9. /** The Widget that consumes the event.
  10. This stops propagation of the event if applicable.
  11. */
  12. Widget *consumed = NULL;
  13. };
  14. /** Base event class */
  15. struct Event {
  16. Context *context = NULL;
  17. void consume(Widget *w) const {
  18. if (context)
  19. context->consumed = w;
  20. }
  21. Widget *getConsumed() const {
  22. return context ? context->consumed : NULL;
  23. }
  24. };
  25. struct Position {
  26. /** The pixel coordinate where the event occurred, relative to the Widget it is called on. */
  27. math::Vec pos;
  28. };
  29. struct Key {
  30. /** GLFW_KEY_* */
  31. int key;
  32. /** GLFW_KEY_*. You should usually use `key` instead. */
  33. int scancode;
  34. /** GLFW_RELEASE, GLFW_PRESS, or GLFW_REPEAT */
  35. int action;
  36. /** GLFW_MOD_* */
  37. int mods;
  38. };
  39. // Events
  40. struct Text {
  41. /** Unicode code point of the character */
  42. int codepoint;
  43. };
  44. /** Occurs every frame when the mouse is hovering over a Widget.
  45. Recurses until consumed.
  46. If `target` is set, other events may occur on that Widget.
  47. */
  48. struct Hover : Event, Position {
  49. /** Change in mouse position since the last frame. Can be zero. */
  50. math::Vec mouseDelta;
  51. };
  52. /** Occurs each mouse button press or release.
  53. Recurses until consumed.
  54. If `target` is set, other events may occur on that Widget.
  55. */
  56. struct Button : Event, Position {
  57. /** GLFW_MOUSE_BUTTON_LEFT, GLFW_MOUSE_BUTTON_RIGHT, GLFW_MOUSE_BUTTON_MIDDLE, etc. */
  58. int button;
  59. /** GLFW_PRESS or GLFW_RELEASE */
  60. int action;
  61. /** GLFW_MOD_* */
  62. int mods;
  63. };
  64. /** Occurs when a key is pressed, released, or repeated while the mouse is hovering a Widget.
  65. Recurses until consumed.
  66. */
  67. struct HoverKey : Event, Position, Key {
  68. };
  69. /** Occurs when a character is typed while the mouse is hovering a Widget.
  70. Recurses until consumed.
  71. */
  72. struct HoverText : Event, Position, Text {
  73. };
  74. /** Occurs when the mouse scroll wheel is moved while the mouse is hovering a Widget.
  75. Recurses until consumed.
  76. */
  77. struct HoverScroll : Event, Position {
  78. /** Change of scroll wheel position. */
  79. math::Vec scrollDelta;
  80. };
  81. /** Occurs when a Widget begins consuming the Hover event.
  82. */
  83. struct Enter : Event {
  84. };
  85. /** Occurs when a different Widget is entered.
  86. */
  87. struct Leave : Event {
  88. };
  89. /** Occurs when a Widget begins consuming the Button press event.
  90. */
  91. struct Select : Event {
  92. };
  93. /** Occurs when a different Widget is selected.
  94. */
  95. struct Deselect : Event {
  96. };
  97. /** Occurs when a key is pressed, released, or repeated while a Widget is selected.
  98. If consumed, a HoverKey event will not be triggered.
  99. */
  100. struct SelectKey : Event, Key {
  101. };
  102. /** Occurs when text is typed while a Widget is selected.
  103. */
  104. struct SelectText : Event, Text {
  105. };
  106. /** Occurs when a Widget begins being dragged.
  107. */
  108. struct DragStart : Event {
  109. };
  110. /** Occurs when a Widget stops being dragged by releasing the mouse button.
  111. */
  112. struct DragEnd : Event {
  113. };
  114. /** Occurs every frame on the dragged Widget.
  115. */
  116. struct DragMove : Event {
  117. /** Change in mouse position since the last frame. Can be zero. */
  118. math::Vec mouseDelta;
  119. };
  120. /** Occurs every frame when the mouse is hovering over a Widget while another Widget (possibly the same one) is being dragged.
  121. Recurses until consumed.
  122. */
  123. struct DragHover : Event, Position {
  124. /** The dragged widget */
  125. Widget *origin = NULL;
  126. /** Change in mouse position since the last frame. Can be zero. */
  127. math::Vec mouseDelta;
  128. };
  129. /** Occurs when the mouse enters a Widget while dragging.
  130. */
  131. struct DragEnter : Event {
  132. /** The dragged widget */
  133. Widget *origin = NULL;
  134. };
  135. /** Occurs when the mouse leaves a Widget while dragging.
  136. */
  137. struct DragLeave : Event {
  138. /** The dragged widget */
  139. Widget *origin = NULL;
  140. };
  141. /** Occurs when the mouse button is released over a Widget while dragging.
  142. */
  143. struct DragDrop : Event {
  144. /** The dragged widget */
  145. Widget *origin = NULL;
  146. };
  147. /** Occurs when a selection of files from the operating system is dropped onto a Widget.
  148. Recurses until consumed.
  149. */
  150. struct PathDrop : Event, Position {
  151. PathDrop(const std::vector<std::string> &paths) : paths(paths) {}
  152. /** List of file paths in the dropped selection */
  153. const std::vector<std::string> &paths;
  154. };
  155. /** Occurs when an certain action is triggered on a Widget.
  156. */
  157. struct Action : Event {
  158. };
  159. /** Occurs when the value of a Widget changes.
  160. */
  161. struct Change : Event {
  162. };
  163. /** Occurs when the zoom level of a Widget is changed.
  164. Recurses until consumed.
  165. */
  166. struct Zoom : Event {
  167. };
  168. struct State {
  169. Widget *rootWidget = NULL;
  170. /** State widgets
  171. Don't set these directly unless you know what you're doing. Use the set*() methods instead.
  172. */
  173. Widget *hoveredWidget = NULL;
  174. Widget *draggedWidget = NULL;
  175. Widget *dragHoveredWidget = NULL;
  176. Widget *selectedWidget = NULL;
  177. /** For middle-click dragging */
  178. Widget *scrollWidget = NULL;
  179. void setHovered(Widget *w);
  180. void setDragged(Widget *w);
  181. void setDragHovered(Widget *w);
  182. void setSelected(Widget *w);
  183. /** Prepares a widget for deletion */
  184. void finalizeWidget(Widget *w);
  185. void handleButton(math::Vec pos, int button, int action, int mods);
  186. void handleHover(math::Vec pos, math::Vec mouseDelta);
  187. void handleLeave();
  188. void handleScroll(math::Vec pos, math::Vec scrollDelta);
  189. void handleText(math::Vec pos, int codepoint);
  190. void handleKey(math::Vec pos, int key, int scancode, int action, int mods);
  191. void handleDrop(math::Vec pos, const std::vector<std::string> &paths);
  192. void handleZoom();
  193. };
  194. } // namespace event
  195. } // namespace rack