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.

136 lines
4.2KB

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