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.

232 lines
4.6KB

  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. EVENT_TRIGGER_DECLARATION();
  115. };
  116. /** Occurs when a Widget stops being dragged by releasing the mouse button.
  117. */
  118. struct DragEnd : Event {
  119. EVENT_TRIGGER_DECLARATION();
  120. };
  121. /** Occurs when a dragged Widget is moved.
  122. Called once per frame, even when mouseDelta is zero.
  123. */
  124. struct DragMove : Event {
  125. math::Vec mouseDelta;
  126. EVENT_TRIGGER_DECLARATION();
  127. };
  128. /** Occurs when the mouse enters a Widget while dragging.
  129. */
  130. struct DragEnter : Event {
  131. Widget *origin = NULL;
  132. EVENT_TRIGGER_DECLARATION();
  133. };
  134. /** Occurs when the mouse leaves a Widget while dragging.
  135. */
  136. struct DragLeave : Event {
  137. Widget *origin = NULL;
  138. EVENT_TRIGGER_DECLARATION();
  139. };
  140. /** Occurs when the mouse button is released over a Widget while dragging.
  141. */
  142. struct DragDrop : Event {
  143. Widget *origin = NULL;
  144. EVENT_TRIGGER_DECLARATION();
  145. };
  146. /** Occurs when a selection of files from the operating system are dropped onto a Widget.
  147. */
  148. struct PathDrop : Event, Position {
  149. /** List of file paths in the dropped selection */
  150. std::vector<std::string> paths;
  151. EVENT_TRIGGER_DECLARATION();
  152. };
  153. /** Occurs when an certain action is triggered on a Widget.
  154. */
  155. struct Action : Event {
  156. EVENT_TRIGGER_DECLARATION();
  157. };
  158. /** Occurs when the value of a Widget changes.
  159. */
  160. struct Change : Event {
  161. EVENT_TRIGGER_DECLARATION();
  162. };
  163. /** Occurs when the zoom level of a Widget is changed.
  164. Recurses.
  165. */
  166. struct Zoom : Event {
  167. EVENT_TRIGGER_DECLARATION();
  168. };
  169. } // namespace event
  170. } // namespace rack