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.

227 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. virtual math::Rect getVisibleChildrenBoundingBox();
  48. /** Returns `v` (given in local coordinates) transformed into the coordinate system of `relative`.
  49. */
  50. virtual math::Vec getRelativeOffset(math::Vec v, Widget* relative);
  51. /** Returns `v` transformed into world/root/global/absolute coordinates.
  52. */
  53. math::Vec getAbsoluteOffset(math::Vec v) {
  54. return getRelativeOffset(v, NULL);
  55. }
  56. /** Returns the zoom level in the coordinate system of `relative`.
  57. Only `ZoomWidget` should override this to return value other than 1.
  58. */
  59. virtual float getRelativeZoom(Widget* relative);
  60. float getAbsoluteZoom() {
  61. return getRelativeZoom(NULL);
  62. }
  63. /** Returns a subset of the given Rect bounded by the box of this widget and all ancestors.
  64. */
  65. virtual math::Rect getViewport(math::Rect r);
  66. template <class T>
  67. T* getAncestorOfType() {
  68. if (!parent)
  69. return NULL;
  70. T* p = dynamic_cast<T*>(parent);
  71. if (p)
  72. return p;
  73. return parent->getAncestorOfType<T>();
  74. }
  75. template <class T>
  76. T* getFirstDescendantOfType() {
  77. for (Widget* child : children) {
  78. T* c = dynamic_cast<T*>(child);
  79. if (c)
  80. return c;
  81. c = child->getFirstDescendantOfType<T>();
  82. if (c)
  83. return c;
  84. }
  85. return NULL;
  86. }
  87. /** Adds widget to list of children.
  88. Gives ownership of widget to this widget instance.
  89. */
  90. void addChild(Widget* child);
  91. void addChildBottom(Widget* child);
  92. /** Removes widget from list of children if it exists.
  93. Does not delete widget but transfers ownership to caller
  94. */
  95. void removeChild(Widget* child);
  96. /** Removes and deletes all children */
  97. void clearChildren();
  98. /** Advances the module by one frame */
  99. virtual void step();
  100. struct DrawArgs {
  101. NVGcontext* vg;
  102. math::Rect clipBox;
  103. NVGLUframebuffer* fb = NULL;
  104. };
  105. /** Draws the widget to the NanoVG context */
  106. virtual void draw(const DrawArgs& args);
  107. /** Override draw(const DrawArgs &args) instead */
  108. DEPRECATED virtual void draw(NVGcontext* vg) {}
  109. // Events
  110. /** Recurses an event to all visible Widgets */
  111. template <typename TMethod, class TEvent>
  112. void recurseEvent(TMethod f, const TEvent& e) {
  113. for (auto it = children.rbegin(); it != children.rend(); it++) {
  114. // Stop propagation if requested
  115. if (!e.isPropagating())
  116. break;
  117. Widget* child = *it;
  118. // Filter child by visibility
  119. if (!child->visible)
  120. continue;
  121. // Clone event for (currently) no reason
  122. TEvent e2 = e;
  123. // Call child event handler
  124. (child->*f)(e2);
  125. }
  126. }
  127. /** Recurses an event to all visible Widgets until it is consumed. */
  128. template <typename TMethod, class TEvent>
  129. void recursePositionEvent(TMethod f, const TEvent& e) {
  130. for (auto it = children.rbegin(); it != children.rend(); it++) {
  131. // Stop propagation if requested
  132. if (!e.isPropagating())
  133. break;
  134. Widget* child = *it;
  135. // Filter child by visibility and position
  136. if (!child->visible)
  137. continue;
  138. if (!child->box.isContaining(e.pos))
  139. continue;
  140. // Clone event and adjust its position
  141. TEvent e2 = e;
  142. e2.pos = e.pos.minus(child->box.pos);
  143. // Call child event handler
  144. (child->*f)(e2);
  145. }
  146. }
  147. /** Override these event callbacks to respond to events.
  148. See event.hpp for a description of each event.
  149. */
  150. virtual void onHover(const event::Hover& e) {
  151. recursePositionEvent(&Widget::onHover, e);
  152. }
  153. virtual void onButton(const event::Button& e) {
  154. recursePositionEvent(&Widget::onButton, e);
  155. }
  156. virtual void onDoubleClick(const event::DoubleClick& e) {}
  157. virtual void onHoverKey(const event::HoverKey& e) {
  158. recursePositionEvent(&Widget::onHoverKey, e);
  159. }
  160. virtual void onHoverText(const event::HoverText& e) {
  161. recursePositionEvent(&Widget::onHoverText, e);
  162. }
  163. virtual void onHoverScroll(const event::HoverScroll& e) {
  164. recursePositionEvent(&Widget::onHoverScroll, e);
  165. }
  166. virtual void onEnter(const event::Enter& e) {}
  167. virtual void onLeave(const event::Leave& e) {}
  168. virtual void onSelect(const event::Select& e) {}
  169. virtual void onDeselect(const event::Deselect& e) {}
  170. virtual void onSelectKey(const event::SelectKey& e) {}
  171. virtual void onSelectText(const event::SelectText& e) {}
  172. virtual void onDragStart(const event::DragStart& e) {}
  173. virtual void onDragEnd(const event::DragEnd& e) {}
  174. virtual void onDragMove(const event::DragMove& e) {}
  175. virtual void onDragHover(const event::DragHover& e) {
  176. recursePositionEvent(&Widget::onDragHover, e);
  177. }
  178. virtual void onDragEnter(const event::DragEnter& e) {}
  179. virtual void onDragLeave(const event::DragLeave& e) {}
  180. virtual void onDragDrop(const event::DragDrop& e) {}
  181. virtual void onPathDrop(const event::PathDrop& e) {
  182. recursePositionEvent(&Widget::onPathDrop, e);
  183. }
  184. virtual void onAction(const event::Action& e) {}
  185. virtual void onChange(const event::Change& e) {}
  186. virtual void onDirty(const event::Dirty& e) {
  187. recurseEvent(&Widget::onDirty, e);
  188. }
  189. virtual void onReposition(const event::Reposition& e) {}
  190. virtual void onResize(const event::Resize& e) {}
  191. virtual void onAdd(const event::Add& e) {}
  192. virtual void onRemove(const event::Remove& e) {}
  193. virtual void onShow(const event::Show& e) {
  194. recurseEvent(&Widget::onShow, e);
  195. }
  196. virtual void onHide(const event::Hide& e) {
  197. recurseEvent(&Widget::onHide, e);
  198. }
  199. };
  200. } // namespace widget
  201. } // namespace rack