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.

143 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 *widget);
  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 *widget);
  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. template <typename TMethod, class TEvent>
  66. void recurseEvent(TMethod f, const TEvent &e) {
  67. for (auto it = children.rbegin(); it != children.rend(); it++) {
  68. Widget *child = *it;
  69. // Filter child by visibility
  70. if (!child->visible)
  71. continue;
  72. // Call child event handler
  73. (child->*f)(e);
  74. // Stop iterating if consumed
  75. if (e.getConsumed())
  76. break;
  77. }
  78. }
  79. template <typename TMethod, class TEvent>
  80. void recursePositionEvent(TMethod f, const TEvent &e) {
  81. for (auto it = children.rbegin(); it != children.rend(); it++) {
  82. Widget *child = *it;
  83. // Filter child by visibility and position
  84. if (!child->visible)
  85. continue;
  86. if (!child->box.contains(e.pos))
  87. continue;
  88. // Clone event and adjust its position
  89. TEvent e2 = e;
  90. e2.pos = e.pos.minus(child->box.pos);
  91. // Call child event handler
  92. (child->*f)(e2);
  93. // Stop iterating if consumed
  94. if (e.getConsumed())
  95. break;
  96. }
  97. }
  98. /** Override these event callbacks to respond to events.
  99. See events.hpp for a description of each event.
  100. */
  101. virtual void onHover(const event::Hover &e) {recursePositionEvent(&Widget::onHover, e);}
  102. virtual void onButton(const event::Button &e) {recursePositionEvent(&Widget::onButton, e);}
  103. virtual void onHoverKey(const event::HoverKey &e) {recursePositionEvent(&Widget::onHoverKey, e);}
  104. virtual void onHoverText(const event::HoverText &e) {recursePositionEvent(&Widget::onHoverText, e);}
  105. virtual void onHoverScroll(const event::HoverScroll &e) {recursePositionEvent(&Widget::onHoverScroll, e);}
  106. virtual void onEnter(const event::Enter &e) {}
  107. virtual void onLeave(const event::Leave &e) {}
  108. virtual void onSelect(const event::Select &e) {}
  109. virtual void onDeselect(const event::Deselect &e) {}
  110. virtual void onSelectKey(const event::SelectKey &e) {}
  111. virtual void onSelectText(const event::SelectText &e) {}
  112. virtual void onDragStart(const event::DragStart &e) {}
  113. virtual void onDragEnd(const event::DragEnd &e) {}
  114. virtual void onDragMove(const event::DragMove &e) {}
  115. virtual void onDragHover(const event::DragHover &e) {recursePositionEvent(&Widget::onDragHover, e);}
  116. virtual void onDragEnter(const event::DragEnter &e) {}
  117. virtual void onDragLeave(const event::DragLeave &e) {}
  118. virtual void onDragDrop(const event::DragDrop &e) {}
  119. virtual void onPathDrop(const event::PathDrop &e) {recursePositionEvent(&Widget::onPathDrop, e);}
  120. virtual void onAction(const event::Action &e) {}
  121. virtual void onChange(const event::Change &e) {}
  122. virtual void onZoom(const event::Zoom &e) {recurseEvent(&Widget::onZoom, e);}
  123. };
  124. } // namespace rack