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.

158 lines
5.1KB

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