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.

225 lines
6.5KB

  1. #pragma once
  2. #include <list>
  3. #include <common.hpp>
  4. #include <math.hpp>
  5. #include <window.hpp>
  6. #include <color.hpp>
  7. #include <event.hpp>
  8. namespace rack {
  9. /** General UI widgets
  10. */
  11. namespace widget {
  12. /** A node in the 2D [scene graph](https://en.wikipedia.org/wiki/Scene_graph).
  13. The bounding box of a Widget is a rectangle specified by `box` relative to their parent.
  14. The appearance is defined by overriding `draw()`, and the behavior is defined by overriding `step()` and `on*()` event handlers.
  15. */
  16. struct Widget {
  17. /** Stores position and size */
  18. math::Rect box = math::Rect(math::Vec(), math::Vec(INFINITY, INFINITY));
  19. /** Automatically set when Widget is added as a child to another Widget */
  20. Widget* parent = NULL;
  21. std::list<Widget*> children;
  22. /** Disables rendering but allow stepping */
  23. bool visible = true;
  24. /** If set to true, parent will delete Widget in the next step() */
  25. bool requestedDelete = false;
  26. virtual ~Widget();
  27. math::Rect getBox();
  28. void setBox(math::Rect box);
  29. math::Vec getPosition();
  30. void setPosition(math::Vec pos);
  31. math::Vec getSize();
  32. void setSize(math::Vec size);
  33. bool isVisible();
  34. void setVisible(bool visible);
  35. void show() {
  36. setVisible(true);
  37. }
  38. void hide() {
  39. setVisible(false);
  40. }
  41. void requestDelete();
  42. /** Returns the smallest rectangle containing this widget's children (visible and invisible) in its local coordinates.
  43. Returns `Rect(Vec(inf, inf), Vec(-inf, -inf))` if there are no children.
  44. */
  45. virtual math::Rect getChildrenBoundingBox();
  46. /** Returns `v` (given in local coordinates) transformed into the coordinate system of `relative`.
  47. */
  48. virtual math::Vec getRelativeOffset(math::Vec v, Widget* relative);
  49. /** Returns `v` transformed into world/root/global/absolute coordinates.
  50. */
  51. math::Vec getAbsoluteOffset(math::Vec v) {
  52. return getRelativeOffset(v, NULL);
  53. }
  54. /** Returns the zoom level in the coordinate system of `relative`.
  55. Only `ZoomWidget` should override this to return value other than 1.
  56. */
  57. virtual float getRelativeZoom(Widget* relative);
  58. float getAbsoluteZoom() {
  59. return getRelativeZoom(NULL);
  60. }
  61. /** Returns a subset of the given Rect bounded by the box of this widget and all ancestors.
  62. */
  63. virtual math::Rect getViewport(math::Rect r);
  64. template <class T>
  65. T* getAncestorOfType() {
  66. if (!parent)
  67. return NULL;
  68. T* p = dynamic_cast<T*>(parent);
  69. if (p)
  70. return p;
  71. return parent->getAncestorOfType<T>();
  72. }
  73. template <class T>
  74. T* getFirstDescendantOfType() {
  75. for (Widget* child : children) {
  76. T* c = dynamic_cast<T*>(child);
  77. if (c)
  78. return c;
  79. c = child->getFirstDescendantOfType<T>();
  80. if (c)
  81. return c;
  82. }
  83. return NULL;
  84. }
  85. /** Adds widget to list of children.
  86. Gives ownership of widget to this widget instance.
  87. */
  88. void addChild(Widget* child);
  89. void addChildBottom(Widget* child);
  90. /** Removes widget from list of children if it exists.
  91. Does not delete widget but transfers ownership to caller
  92. */
  93. void removeChild(Widget* child);
  94. /** Removes and deletes all children */
  95. void clearChildren();
  96. /** Advances the module by one frame */
  97. virtual void step();
  98. struct DrawArgs {
  99. NVGcontext* vg;
  100. math::Rect clipBox;
  101. NVGLUframebuffer* fb = NULL;
  102. };
  103. /** Draws the widget to the NanoVG context */
  104. virtual void draw(const DrawArgs& args);
  105. /** Override draw(const DrawArgs &args) instead */
  106. DEPRECATED virtual void draw(NVGcontext* vg) {}
  107. // Events
  108. /** Recurses an event to all visible Widgets */
  109. template <typename TMethod, class TEvent>
  110. void recurseEvent(TMethod f, const TEvent& e) {
  111. for (auto it = children.rbegin(); it != children.rend(); it++) {
  112. // Stop propagation if requested
  113. if (!e.isPropagating())
  114. break;
  115. Widget* child = *it;
  116. // Filter child by visibility
  117. if (!child->visible)
  118. continue;
  119. // Clone event for (currently) no reason
  120. TEvent e2 = e;
  121. // Call child event handler
  122. (child->*f)(e2);
  123. }
  124. }
  125. /** Recurses an event to all visible Widgets until it is consumed. */
  126. template <typename TMethod, class TEvent>
  127. void recursePositionEvent(TMethod f, const TEvent& e) {
  128. for (auto it = children.rbegin(); it != children.rend(); it++) {
  129. // Stop propagation if requested
  130. if (!e.isPropagating())
  131. break;
  132. Widget* child = *it;
  133. // Filter child by visibility and position
  134. if (!child->visible)
  135. continue;
  136. if (!child->box.isContaining(e.pos))
  137. continue;
  138. // Clone event and adjust its position
  139. TEvent e2 = e;
  140. e2.pos = e.pos.minus(child->box.pos);
  141. // Call child event handler
  142. (child->*f)(e2);
  143. }
  144. }
  145. /** Override these event callbacks to respond to events.
  146. See event.hpp for a description of each event.
  147. */
  148. virtual void onHover(const event::Hover& e) {
  149. recursePositionEvent(&Widget::onHover, e);
  150. }
  151. virtual void onButton(const event::Button& e) {
  152. recursePositionEvent(&Widget::onButton, e);
  153. }
  154. virtual void onDoubleClick(const event::DoubleClick& e) {}
  155. virtual void onHoverKey(const event::HoverKey& e) {
  156. recursePositionEvent(&Widget::onHoverKey, e);
  157. }
  158. virtual void onHoverText(const event::HoverText& e) {
  159. recursePositionEvent(&Widget::onHoverText, e);
  160. }
  161. virtual void onHoverScroll(const event::HoverScroll& e) {
  162. recursePositionEvent(&Widget::onHoverScroll, e);
  163. }
  164. virtual void onEnter(const event::Enter& e) {}
  165. virtual void onLeave(const event::Leave& e) {}
  166. virtual void onSelect(const event::Select& e) {}
  167. virtual void onDeselect(const event::Deselect& e) {}
  168. virtual void onSelectKey(const event::SelectKey& e) {}
  169. virtual void onSelectText(const event::SelectText& e) {}
  170. virtual void onDragStart(const event::DragStart& e) {}
  171. virtual void onDragEnd(const event::DragEnd& e) {}
  172. virtual void onDragMove(const event::DragMove& e) {}
  173. virtual void onDragHover(const event::DragHover& e) {
  174. recursePositionEvent(&Widget::onDragHover, e);
  175. }
  176. virtual void onDragEnter(const event::DragEnter& e) {}
  177. virtual void onDragLeave(const event::DragLeave& e) {}
  178. virtual void onDragDrop(const event::DragDrop& e) {}
  179. virtual void onPathDrop(const event::PathDrop& e) {
  180. recursePositionEvent(&Widget::onPathDrop, e);
  181. }
  182. virtual void onAction(const event::Action& e) {}
  183. virtual void onChange(const event::Change& e) {}
  184. virtual void onDirty(const event::Dirty& e) {
  185. recurseEvent(&Widget::onDirty, e);
  186. }
  187. virtual void onReposition(const event::Reposition& e) {}
  188. virtual void onResize(const event::Resize& e) {}
  189. virtual void onAdd(const event::Add& e) {}
  190. virtual void onRemove(const event::Remove& e) {}
  191. virtual void onShow(const event::Show& e) {
  192. recurseEvent(&Widget::onShow, e);
  193. }
  194. virtual void onHide(const event::Hide& e) {
  195. recurseEvent(&Widget::onHide, e);
  196. }
  197. };
  198. } // namespace widget
  199. } // namespace rack