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.

148 lines
4.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. struct DrawContext {
  10. NVGcontext *vg;
  11. math::Rect clipBox = math::Rect(math::Vec(), math::Vec(INFINITY, INFINITY));
  12. };
  13. /** A node in the 2D scene graph
  14. */
  15. struct Widget {
  16. /** Stores position and size */
  17. math::Rect box = math::Rect(math::Vec(), math::Vec(INFINITY, INFINITY));
  18. /** Automatically set when Widget is added as a child to another Widget */
  19. Widget *parent = NULL;
  20. std::list<Widget*> children;
  21. /** Disables rendering but allow stepping */
  22. bool visible = true;
  23. /** If set to true, parent will delete Widget in the next step() */
  24. bool requestedDelete = false;
  25. virtual ~Widget();
  26. virtual math::Rect getChildrenBoundingBox();
  27. /** Returns `v` transformed into the coordinate system of `relative` */
  28. virtual math::Vec getRelativeOffset(math::Vec v, Widget *relative);
  29. /** Returns `v` transformed into world coordinates */
  30. math::Vec getAbsoluteOffset(math::Vec v) {
  31. return getRelativeOffset(v, NULL);
  32. }
  33. /** Returns a subset of the given math::Rect bounded by the box of this widget and all ancestors */
  34. virtual math::Rect getViewport(math::Rect r);
  35. template <class T>
  36. T *getAncestorOfType() {
  37. if (!parent) return NULL;
  38. T *p = dynamic_cast<T*>(parent);
  39. if (p) return p;
  40. return parent->getAncestorOfType<T>();
  41. }
  42. template <class T>
  43. T *getFirstDescendantOfType() {
  44. for (Widget *child : children) {
  45. T *c = dynamic_cast<T*>(child);
  46. if (c) return c;
  47. c = child->getFirstDescendantOfType<T>();
  48. if (c) return c;
  49. }
  50. return NULL;
  51. }
  52. /** Adds widget to list of children.
  53. Gives ownership of widget to this widget instance.
  54. */
  55. void addChild(Widget *child);
  56. /** Removes widget from list of children if it exists.
  57. Does not delete widget but transfers ownership to caller
  58. */
  59. void removeChild(Widget *child);
  60. /** Removes and deletes all children */
  61. void clearChildren();
  62. /** Advances the module by one frame */
  63. virtual void step();
  64. /** Draws the widget to the NanoVG context */
  65. virtual void draw(const DrawContext &ctx);
  66. /** Override `draw(const DrawContext &ctx)` instead */
  67. DEPRECATED virtual void draw(NVGcontext *vg) {}
  68. // Events
  69. /** Recurses an event to all visible Widgets */
  70. template <typename TMethod, class TEvent>
  71. void recurseEvent(TMethod f, const TEvent &e) {
  72. for (auto it = children.rbegin(); it != children.rend(); it++) {
  73. Widget *child = *it;
  74. // Filter child by visibility
  75. if (!child->visible)
  76. continue;
  77. // Call child event handler
  78. (child->*f)(e);
  79. }
  80. }
  81. /** Recurses an event to all visible Widgets until it is consumed. */
  82. template <typename TMethod, class TEvent>
  83. void recursePositionEvent(TMethod f, const TEvent &e) {
  84. for (auto it = children.rbegin(); it != children.rend(); it++) {
  85. Widget *child = *it;
  86. // Filter child by visibility and position
  87. if (!child->visible)
  88. continue;
  89. if (!child->box.isContaining(e.pos))
  90. continue;
  91. // Clone event and adjust its position
  92. TEvent e2 = e;
  93. e2.pos = e.pos.minus(child->box.pos);
  94. // Call child event handler
  95. (child->*f)(e2);
  96. // Stop iterating if consumed
  97. if (e.getConsumed())
  98. break;
  99. }
  100. }
  101. /** Override these event callbacks to respond to events.
  102. See events.hpp for a description of each event.
  103. */
  104. virtual void onHover(const event::Hover &e) {recursePositionEvent(&Widget::onHover, e);}
  105. virtual void onButton(const event::Button &e) {recursePositionEvent(&Widget::onButton, e);}
  106. virtual void onHoverKey(const event::HoverKey &e) {recursePositionEvent(&Widget::onHoverKey, e);}
  107. virtual void onHoverText(const event::HoverText &e) {recursePositionEvent(&Widget::onHoverText, e);}
  108. virtual void onHoverScroll(const event::HoverScroll &e) {recursePositionEvent(&Widget::onHoverScroll, e);}
  109. virtual void onEnter(const event::Enter &e) {}
  110. virtual void onLeave(const event::Leave &e) {}
  111. virtual void onSelect(const event::Select &e) {}
  112. virtual void onDeselect(const event::Deselect &e) {}
  113. virtual void onSelectKey(const event::SelectKey &e) {}
  114. virtual void onSelectText(const event::SelectText &e) {}
  115. virtual void onDragStart(const event::DragStart &e) {}
  116. virtual void onDragEnd(const event::DragEnd &e) {}
  117. virtual void onDragMove(const event::DragMove &e) {}
  118. virtual void onDragHover(const event::DragHover &e) {recursePositionEvent(&Widget::onDragHover, e);}
  119. virtual void onDragEnter(const event::DragEnter &e) {}
  120. virtual void onDragLeave(const event::DragLeave &e) {}
  121. virtual void onDragDrop(const event::DragDrop &e) {}
  122. virtual void onPathDrop(const event::PathDrop &e) {recursePositionEvent(&Widget::onPathDrop, e);}
  123. virtual void onAction(const event::Action &e) {}
  124. virtual void onChange(const event::Change &e) {}
  125. virtual void onZoom(const event::Zoom &e) {recurseEvent(&Widget::onZoom, e);}
  126. };
  127. } // namespace rack