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.

246 lines
4.8KB

  1. #pragma once
  2. #include <vector>
  3. #include "widgets/Widget.hpp"
  4. namespace rack {
  5. struct EventWidget;
  6. namespace event {
  7. struct Event {
  8. /** Set this to the Widget that consumes (responds to) the event.
  9. This stops propagation of the event if applicable.
  10. */
  11. Widget *target = NULL;
  12. virtual ~Event() {}
  13. /** Triggers the event on an EventWidget.
  14. Calls the appropriate `EventWidget::on()` method.
  15. */
  16. virtual void trigger(EventWidget *w) = 0;
  17. };
  18. struct Position {
  19. /** The pixel coordinate where the event occurred, relative to the Widget it is called on. */
  20. math::Vec pos;
  21. };
  22. struct Key {
  23. /** GLFW_KEY_* */
  24. int key;
  25. /** GLFW_KEY_*. You should usually use `key` instead. */
  26. int scancode;
  27. /** GLFW_RELEASE, GLFW_PRESS, or GLFW_REPEAT */
  28. int action;
  29. /** GLFW_MOD_* */
  30. int mods;
  31. };
  32. struct Text {
  33. /** Unicode code point of the character */
  34. int codepoint;
  35. };
  36. #define EVENT_TRIGGER_DECLARATION() void trigger(EventWidget *w) override
  37. #define EVENT_TRIGGER_DEFINITION(_event) inline void _event::trigger(EventWidget *w) { w->on(*this); }
  38. /** Occurs every frame when the mouse is hovering over a Widget.
  39. Recurses until consumed.
  40. If target is set, other events may occur on that Widget.
  41. */
  42. struct Hover : Event, Position {
  43. /** Change in mouse position since the last frame. Can be zero. */
  44. math::Vec mouseDelta;
  45. EVENT_TRIGGER_DECLARATION();
  46. };
  47. /** Occurs each mouse button press or release.
  48. Recurses until consumed.
  49. If target is set, other events may occur on that Widget.
  50. */
  51. struct Button : Event, Position {
  52. /** GLFW_MOUSE_BUTTON_LEFT, GLFW_MOUSE_BUTTON_RIGHT, GLFW_MOUSE_BUTTON_MIDDLE, etc. */
  53. int button;
  54. /** GLFW_PRESS or GLFW_RELEASE */
  55. int action;
  56. /** GLFW_MOD_* */
  57. int mods;
  58. EVENT_TRIGGER_DECLARATION();
  59. };
  60. /** Occurs when a key is pressed while the mouse is hovering a Widget.
  61. Recurses until consumed.
  62. */
  63. struct HoverKey : Event, Position, Key {
  64. EVENT_TRIGGER_DECLARATION();
  65. };
  66. /** Occurs when a character is typed while the mouse is hovering a Widget.
  67. Recurses until consumed.
  68. */
  69. struct HoverText : Event, Position, Text {
  70. EVENT_TRIGGER_DECLARATION();
  71. };
  72. /** Occurs when the mouse scroll wheel is moved while the mouse is hovering a Widget.
  73. Recurses until consumed.
  74. */
  75. struct HoverScroll : Event, Position {
  76. /** Change of scroll wheel position. */
  77. math::Vec scrollDelta;
  78. EVENT_TRIGGER_DECLARATION();
  79. };
  80. /** Occurs when a Widget begins consuming the Hover event.
  81. */
  82. struct Enter : Event {
  83. EVENT_TRIGGER_DECLARATION();
  84. };
  85. /** Occurs when a different Widget is entered.
  86. */
  87. struct Leave : Event {
  88. EVENT_TRIGGER_DECLARATION();
  89. };
  90. /** Occurs when a Widget begins consuming the Button press event.
  91. */
  92. struct Select : Event {
  93. EVENT_TRIGGER_DECLARATION();
  94. };
  95. /** Occurs when a different Widget is selected.
  96. */
  97. struct Deselect : Event {
  98. EVENT_TRIGGER_DECLARATION();
  99. };
  100. /** Occurs when a key is pressed while a Widget is selected.
  101. */
  102. struct SelectKey : Event, Key {
  103. EVENT_TRIGGER_DECLARATION();
  104. };
  105. /** Occurs when text is typed while a Widget is selected.
  106. */
  107. struct SelectText : Event, Text {
  108. EVENT_TRIGGER_DECLARATION();
  109. };
  110. /** Occurs when a Widget begins being dragged.
  111. Must consume to allow the drag to occur.
  112. */
  113. struct DragStart : Event {
  114. int button;
  115. EVENT_TRIGGER_DECLARATION();
  116. };
  117. /** Occurs when a Widget stops being dragged by releasing the mouse button.
  118. */
  119. struct DragEnd : Event {
  120. int button;
  121. EVENT_TRIGGER_DECLARATION();
  122. };
  123. /** Occurs when a dragged Widget is moved.
  124. Called once per frame, even when mouseDelta is zero.
  125. */
  126. struct DragMove : Event {
  127. int button;
  128. math::Vec mouseDelta;
  129. EVENT_TRIGGER_DECLARATION();
  130. };
  131. /** Occurs when the mouse enters a Widget while dragging.
  132. */
  133. struct DragEnter : Event {
  134. Widget *origin = NULL;
  135. int button;
  136. EVENT_TRIGGER_DECLARATION();
  137. };
  138. /** Occurs when the mouse leaves a Widget while dragging.
  139. */
  140. struct DragLeave : Event {
  141. Widget *origin = NULL;
  142. int button;
  143. EVENT_TRIGGER_DECLARATION();
  144. };
  145. /** Occurs when the mouse button is released over a Widget while dragging.
  146. */
  147. struct DragDrop : Event {
  148. Widget *origin = NULL;
  149. int button;
  150. EVENT_TRIGGER_DECLARATION();
  151. };
  152. /** Occurs when a selection of files from the operating system are dropped onto a Widget.
  153. */
  154. struct PathDrop : Event, Position {
  155. /** List of file paths in the dropped selection */
  156. std::vector<std::string> paths;
  157. EVENT_TRIGGER_DECLARATION();
  158. };
  159. /** Occurs when an certain action is triggered on a Widget.
  160. */
  161. struct Action : Event {
  162. EVENT_TRIGGER_DECLARATION();
  163. };
  164. /** Occurs when the value of a Widget changes.
  165. */
  166. struct Change : Event {
  167. EVENT_TRIGGER_DECLARATION();
  168. };
  169. /** Occurs when the zoom level of a Widget is changed.
  170. Recurses.
  171. */
  172. struct Zoom : Event {
  173. EVENT_TRIGGER_DECLARATION();
  174. };
  175. } // namespace event
  176. extern Widget *gHoveredWidget;
  177. extern Widget *gSelectedWidget;
  178. extern Widget *gDraggedWidget;
  179. extern Widget *gDragHoveredWidget;
  180. } // namespace rack