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.

226 lines
6.6KB

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