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.

176 lines
5.8KB

  1. #pragma once
  2. #include <common.hpp>
  3. #include <math.hpp>
  4. #include <window.hpp>
  5. #include <color.hpp>
  6. #include <event.hpp>
  7. #include <list>
  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. void setPosition(math::Vec pos);
  28. void setSize(math::Vec size);
  29. void show();
  30. void hide();
  31. void requestDelete();
  32. virtual math::Rect getChildrenBoundingBox();
  33. /** Returns `v` transformed into the coordinate system of `relative` */
  34. virtual math::Vec getRelativeOffset(math::Vec v, Widget *relative);
  35. /** Returns `v` transformed into world coordinates */
  36. math::Vec getAbsoluteOffset(math::Vec v) {
  37. return getRelativeOffset(v, NULL);
  38. }
  39. /** Returns a subset of the given math::Rect bounded by the box of this widget and all ancestors */
  40. virtual math::Rect getViewport(math::Rect r);
  41. template <class T>
  42. T *getAncestorOfType() {
  43. if (!parent) return NULL;
  44. T *p = dynamic_cast<T*>(parent);
  45. if (p) return p;
  46. return parent->getAncestorOfType<T>();
  47. }
  48. template <class T>
  49. T *getFirstDescendantOfType() {
  50. for (Widget *child : children) {
  51. T *c = dynamic_cast<T*>(child);
  52. if (c) return c;
  53. c = child->getFirstDescendantOfType<T>();
  54. if (c) return c;
  55. }
  56. return NULL;
  57. }
  58. /** Adds widget to list of children.
  59. Gives ownership of widget to this widget instance.
  60. */
  61. void addChild(Widget *child);
  62. void addChildBottom(Widget *child);
  63. /** Removes widget from list of children if it exists.
  64. Does not delete widget but transfers ownership to caller
  65. */
  66. void removeChild(Widget *child);
  67. /** Removes and deletes all children */
  68. void clearChildren();
  69. /** Advances the module by one frame */
  70. virtual void step();
  71. struct DrawArgs {
  72. NVGcontext *vg;
  73. math::Rect clipBox;
  74. NVGLUframebuffer *fb = NULL;
  75. };
  76. /** Draws the widget to the NanoVG context */
  77. virtual void draw(const DrawArgs &args);
  78. /** Override draw(const DrawArgs &args) instead */
  79. DEPRECATED virtual void draw(NVGcontext *vg) {}
  80. // Events
  81. /** Recurses an event to all visible Widgets */
  82. template <typename TMethod, class TEvent>
  83. void recurseEvent(TMethod f, const TEvent &e) {
  84. for (auto it = children.rbegin(); it != children.rend(); it++) {
  85. // Stop propagation if requested
  86. if (!e.isPropagating())
  87. break;
  88. Widget *child = *it;
  89. // Filter child by visibility
  90. if (!child->visible)
  91. continue;
  92. // Clone event for (currently) no reason
  93. TEvent e2 = e;
  94. // Call child event handler
  95. (child->*f)(e2);
  96. }
  97. }
  98. /** Recurses an event to all visible Widgets until it is consumed. */
  99. template <typename TMethod, class TEvent>
  100. void recursePositionEvent(TMethod f, const TEvent &e) {
  101. for (auto it = children.rbegin(); it != children.rend(); it++) {
  102. // Stop propagation if requested
  103. if (!e.isPropagating())
  104. break;
  105. Widget *child = *it;
  106. // Filter child by visibility and position
  107. if (!child->visible)
  108. continue;
  109. if (!child->box.isContaining(e.pos))
  110. continue;
  111. // Clone event and adjust its position
  112. TEvent e2 = e;
  113. e2.pos = e.pos.minus(child->box.pos);
  114. // Call child event handler
  115. (child->*f)(e2);
  116. }
  117. }
  118. /** Override these event callbacks to respond to events.
  119. See event.hpp for a description of each event.
  120. */
  121. virtual void onHover(const event::Hover &e) {recursePositionEvent(&Widget::onHover, e);}
  122. virtual void onButton(const event::Button &e) {recursePositionEvent(&Widget::onButton, e);}
  123. virtual void onDoubleClick(const event::DoubleClick &e) {}
  124. virtual void onHoverKey(const event::HoverKey &e) {recursePositionEvent(&Widget::onHoverKey, e);}
  125. virtual void onHoverText(const event::HoverText &e) {recursePositionEvent(&Widget::onHoverText, e);}
  126. virtual void onHoverScroll(const event::HoverScroll &e) {recursePositionEvent(&Widget::onHoverScroll, e);}
  127. virtual void onEnter(const event::Enter &e) {}
  128. virtual void onLeave(const event::Leave &e) {}
  129. virtual void onSelect(const event::Select &e) {}
  130. virtual void onDeselect(const event::Deselect &e) {}
  131. virtual void onSelectKey(const event::SelectKey &e) {}
  132. virtual void onSelectText(const event::SelectText &e) {}
  133. virtual void onDragStart(const event::DragStart &e) {}
  134. virtual void onDragEnd(const event::DragEnd &e) {}
  135. virtual void onDragMove(const event::DragMove &e) {}
  136. virtual void onDragHover(const event::DragHover &e) {recursePositionEvent(&Widget::onDragHover, e);}
  137. virtual void onDragEnter(const event::DragEnter &e) {}
  138. virtual void onDragLeave(const event::DragLeave &e) {}
  139. virtual void onDragDrop(const event::DragDrop &e) {}
  140. virtual void onPathDrop(const event::PathDrop &e) {recursePositionEvent(&Widget::onPathDrop, e);}
  141. virtual void onAction(const event::Action &e) {}
  142. virtual void onChange(const event::Change &e) {}
  143. virtual void onZoom(const event::Zoom &e) {recurseEvent(&Widget::onZoom, e);}
  144. virtual void onReposition(const event::Reposition &e) {}
  145. virtual void onResize(const event::Resize &e) {}
  146. virtual void onAdd(const event::Add &e) {}
  147. virtual void onRemove(const event::Remove &e) {}
  148. virtual void onShow(const event::Show &e) {recurseEvent(&Widget::onShow, e);}
  149. virtual void onHide(const event::Hide &e) {recurseEvent(&Widget::onHide, e);}
  150. };
  151. } // namespace widget
  152. } // namespace rack