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.

151 lines
4.9KB

  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. namespace widget {
  10. struct DrawContext {
  11. NVGcontext *vg;
  12. math::Rect clipBox = math::Rect(math::Vec(), math::Vec(INFINITY, INFINITY));
  13. };
  14. /** A node in the 2D scene graph
  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 *child);
  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 *child);
  61. /** Removes and deletes all children */
  62. void clearChildren();
  63. /** Advances the module by one frame */
  64. virtual void step();
  65. /** Draws the widget to the NanoVG context */
  66. virtual void draw(const DrawContext &ctx);
  67. /** Override `draw(const DrawContext &ctx)` instead */
  68. DEPRECATED virtual void draw(NVGcontext *vg) {}
  69. // Events
  70. /** Recurses an event to all visible Widgets */
  71. template <typename TMethod, class TEvent>
  72. void recurseEvent(TMethod f, const TEvent &e) {
  73. for (auto it = children.rbegin(); it != children.rend(); it++) {
  74. Widget *child = *it;
  75. // Filter child by visibility
  76. if (!child->visible)
  77. continue;
  78. // Call child event handler
  79. (child->*f)(e);
  80. }
  81. }
  82. /** Recurses an event to all visible Widgets until it is consumed. */
  83. template <typename TMethod, class TEvent>
  84. void recursePositionEvent(TMethod f, const TEvent &e) {
  85. for (auto it = children.rbegin(); it != children.rend(); it++) {
  86. Widget *child = *it;
  87. // Filter child by visibility and position
  88. if (!child->visible)
  89. continue;
  90. if (!child->box.isContaining(e.pos))
  91. continue;
  92. // Clone event and adjust its position
  93. TEvent e2 = e;
  94. e2.pos = e.pos.minus(child->box.pos);
  95. // Call child event handler
  96. (child->*f)(e2);
  97. // Stop iterating if consumed
  98. if (e.getConsumed())
  99. break;
  100. }
  101. }
  102. /** Override these event callbacks to respond to events.
  103. See events.hpp for a description of each event.
  104. */
  105. virtual void onHover(const event::Hover &e) {recursePositionEvent(&Widget::onHover, e);}
  106. virtual void onButton(const event::Button &e) {recursePositionEvent(&Widget::onButton, e);}
  107. virtual void onDoubleClick(const event::DoubleClick &e) {}
  108. virtual void onHoverKey(const event::HoverKey &e) {recursePositionEvent(&Widget::onHoverKey, e);}
  109. virtual void onHoverText(const event::HoverText &e) {recursePositionEvent(&Widget::onHoverText, e);}
  110. virtual void onHoverScroll(const event::HoverScroll &e) {recursePositionEvent(&Widget::onHoverScroll, e);}
  111. virtual void onEnter(const event::Enter &e) {}
  112. virtual void onLeave(const event::Leave &e) {}
  113. virtual void onSelect(const event::Select &e) {}
  114. virtual void onDeselect(const event::Deselect &e) {}
  115. virtual void onSelectKey(const event::SelectKey &e) {}
  116. virtual void onSelectText(const event::SelectText &e) {}
  117. virtual void onDragStart(const event::DragStart &e) {}
  118. virtual void onDragEnd(const event::DragEnd &e) {}
  119. virtual void onDragMove(const event::DragMove &e) {}
  120. virtual void onDragHover(const event::DragHover &e) {recursePositionEvent(&Widget::onDragHover, e);}
  121. virtual void onDragEnter(const event::DragEnter &e) {}
  122. virtual void onDragLeave(const event::DragLeave &e) {}
  123. virtual void onDragDrop(const event::DragDrop &e) {}
  124. virtual void onPathDrop(const event::PathDrop &e) {recursePositionEvent(&Widget::onPathDrop, e);}
  125. virtual void onAction(const event::Action &e) {}
  126. virtual void onChange(const event::Change &e) {}
  127. virtual void onZoom(const event::Zoom &e) {recurseEvent(&Widget::onZoom, e);}
  128. };
  129. } // namespace widget
  130. } // namespace rack