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.

175 lines
5.7KB

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