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.

164 lines
5.4KB

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