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.

255 lines
5.0KB

  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. struct Text {
  40. /** Unicode code point of the character */
  41. int codepoint;
  42. };
  43. /** Occurs every frame when the mouse is hovering over a Widget.
  44. Recurses until consumed.
  45. If `target` is set, other events may occur on that Widget.
  46. */
  47. struct Hover : Event, Position {
  48. /** Change in mouse position since the last frame. Can be zero. */
  49. math::Vec mouseDelta;
  50. };
  51. /** Occurs each mouse button press or release.
  52. Recurses until consumed.
  53. If `target` is set, other events may occur on that Widget.
  54. */
  55. struct Button : Event, Position {
  56. /** GLFW_MOUSE_BUTTON_LEFT, GLFW_MOUSE_BUTTON_RIGHT, GLFW_MOUSE_BUTTON_MIDDLE, etc. */
  57. int button;
  58. /** GLFW_PRESS or GLFW_RELEASE */
  59. int action;
  60. /** GLFW_MOD_* */
  61. int mods;
  62. };
  63. /** Occurs when a key is pressed while the mouse is hovering a Widget.
  64. Recurses until consumed.
  65. */
  66. struct HoverKey : Event, Position, Key {
  67. };
  68. /** Occurs when a character is typed while the mouse is hovering a Widget.
  69. Recurses until consumed.
  70. */
  71. struct HoverText : Event, Position, Text {
  72. };
  73. /** Occurs when the mouse scroll wheel is moved while the mouse is hovering a Widget.
  74. Recurses until consumed.
  75. */
  76. struct HoverScroll : Event, Position {
  77. /** Change of scroll wheel position. */
  78. math::Vec scrollDelta;
  79. };
  80. /** Occurs when a Widget begins consuming the Hover event.
  81. */
  82. struct Enter : Event {
  83. };
  84. /** Occurs when a different Widget is entered.
  85. */
  86. struct Leave : Event {
  87. };
  88. /** Occurs when a Widget begins consuming the Button press event.
  89. */
  90. struct Select : Event {
  91. };
  92. /** Occurs when a different Widget is selected.
  93. */
  94. struct Deselect : Event {
  95. };
  96. /** Occurs when a key is pressed while a Widget is selected.
  97. If consumed, a HoverKey event will not be triggered.
  98. */
  99. struct SelectKey : Event, Key {
  100. };
  101. /** Occurs when text is typed while a Widget is selected.
  102. */
  103. struct SelectText : Event, Text {
  104. };
  105. /** Occurs when a Widget begins being dragged.
  106. Must consume to allow the drag to occur.
  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. `mouseDelta` may be zero.
  116. */
  117. struct DragMove : Event {
  118. math::Vec mouseDelta;
  119. };
  120. /** Occurs every frame when the mouse is hovering over a Widget while dragging.
  121. */
  122. struct DragHover : Event, Position {
  123. Widget *origin = NULL;
  124. /** Change in mouse position since the last frame. Can be zero. */
  125. math::Vec mouseDelta;
  126. };
  127. /** Occurs when the mouse enters a Widget while dragging.
  128. */
  129. struct DragEnter : Event {
  130. Widget *origin = NULL;
  131. };
  132. /** Occurs when the mouse leaves a Widget while dragging.
  133. */
  134. struct DragLeave : Event {
  135. Widget *origin = NULL;
  136. };
  137. /** Occurs when the mouse button is released over a Widget while dragging.
  138. */
  139. struct DragDrop : Event {
  140. Widget *origin = NULL;
  141. };
  142. /** Occurs when a selection of files from the operating system are dropped onto a Widget.
  143. */
  144. struct PathDrop : Event, Position {
  145. /** List of file paths in the dropped selection */
  146. std::vector<std::string> paths;
  147. };
  148. /** Occurs when an certain action is triggered on a Widget.
  149. */
  150. struct Action : Event {
  151. };
  152. /** Occurs when the value of a Widget changes.
  153. */
  154. struct Change : Event {
  155. };
  156. /** Occurs when the zoom level of a Widget is changed.
  157. Recurses.
  158. */
  159. struct Zoom : Event {
  160. };
  161. struct State {
  162. /** State widgets
  163. Don't set these directly unless you know what you're doing. Use the set*() methods instead.
  164. */
  165. Widget *rootWidget = NULL;
  166. Widget *hoveredWidget = NULL;
  167. Widget *draggedWidget = NULL;
  168. Widget *dragHoveredWidget = NULL;
  169. Widget *selectedWidget = NULL;
  170. /** For middle-click dragging */
  171. Widget *scrollWidget = NULL;
  172. void setHovered(Widget *w);
  173. void setDragged(Widget *w);
  174. void setDragHovered(Widget *w);
  175. void setSelected(Widget *w);
  176. /** Prepares a widget for deletion */
  177. void finalizeWidget(Widget *w);
  178. void handleButton(math::Vec pos, int button, int action, int mods);
  179. void handleHover(math::Vec pos, math::Vec mouseDelta);
  180. void handleLeave();
  181. void handleScroll(math::Vec pos, math::Vec scrollDelta);
  182. void handleText(math::Vec pos, int codepoint);
  183. void handleKey(math::Vec pos, int key, int scancode, int action, int mods);
  184. void handleDrop(math::Vec pos, std::vector<std::string> paths);
  185. void handleZoom();
  186. };
  187. } // namespace event
  188. } // namespace rack