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.

262 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. 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, released, or repeated 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, released, or repeated 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. */
  107. struct DragStart : Event {
  108. };
  109. /** Occurs when a Widget stops being dragged by releasing the mouse button.
  110. */
  111. struct DragEnd : Event {
  112. };
  113. /** Occurs every frame on the dragged Widget.
  114. */
  115. struct DragMove : Event {
  116. /** Change in mouse position since the last frame. Can be zero. */
  117. math::Vec mouseDelta;
  118. };
  119. /** Occurs every frame when the mouse is hovering over a Widget while another Widget (possibly the same one) is being dragged.
  120. Recurses until consumed.
  121. */
  122. struct DragHover : Event, Position {
  123. /** The dragged widget */
  124. Widget *origin = NULL;
  125. /** Change in mouse position since the last frame. Can be zero. */
  126. math::Vec mouseDelta;
  127. };
  128. /** Occurs when the mouse enters a Widget while dragging.
  129. */
  130. struct DragEnter : Event {
  131. /** The dragged widget */
  132. Widget *origin = NULL;
  133. };
  134. /** Occurs when the mouse leaves a Widget while dragging.
  135. */
  136. struct DragLeave : Event {
  137. /** The dragged widget */
  138. Widget *origin = NULL;
  139. };
  140. /** Occurs when the mouse button is released over a Widget while dragging.
  141. */
  142. struct DragDrop : Event {
  143. /** The dragged widget */
  144. Widget *origin = NULL;
  145. };
  146. /** Occurs when a selection of files from the operating system is dropped onto a Widget.
  147. Recurses until consumed.
  148. */
  149. struct PathDrop : Event, Position {
  150. PathDrop(const std::vector<std::string> &paths) : paths(paths) {}
  151. /** List of file paths in the dropped selection */
  152. const std::vector<std::string> &paths;
  153. };
  154. /** Occurs when an certain action is triggered on a Widget.
  155. */
  156. struct Action : Event {
  157. };
  158. /** Occurs when the value of a Widget changes.
  159. */
  160. struct Change : Event {
  161. };
  162. /** Occurs when the zoom level of a Widget is changed.
  163. Recurses until consumed.
  164. */
  165. struct Zoom : Event {
  166. };
  167. struct State {
  168. Widget *rootWidget = NULL;
  169. /** State widgets
  170. Don't set these directly unless you know what you're doing. Use the set*() methods instead.
  171. */
  172. Widget *hoveredWidget = NULL;
  173. Widget *draggedWidget = NULL;
  174. Widget *dragHoveredWidget = NULL;
  175. Widget *selectedWidget = NULL;
  176. /** For middle-click dragging */
  177. Widget *scrollWidget = NULL;
  178. void setHovered(Widget *w);
  179. void setDragged(Widget *w);
  180. void setDragHovered(Widget *w);
  181. void setSelected(Widget *w);
  182. /** Prepares a widget for deletion */
  183. void finalizeWidget(Widget *w);
  184. void handleButton(math::Vec pos, int button, int action, int mods);
  185. void handleHover(math::Vec pos, math::Vec mouseDelta);
  186. void handleLeave();
  187. void handleScroll(math::Vec pos, math::Vec scrollDelta);
  188. void handleText(math::Vec pos, int codepoint);
  189. void handleKey(math::Vec pos, int key, int scancode, int action, int mods);
  190. void handleDrop(math::Vec pos, const std::vector<std::string> &paths);
  191. void handleZoom();
  192. };
  193. } // namespace event
  194. } // namespace rack