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.

142 lines
4.6KB

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