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.

Widget.hpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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)
  44. return NULL;
  45. T* p = dynamic_cast<T*>(parent);
  46. if (p)
  47. return p;
  48. return parent->getAncestorOfType<T>();
  49. }
  50. template <class T>
  51. T* getFirstDescendantOfType() {
  52. for (Widget* child : children) {
  53. T* c = dynamic_cast<T*>(child);
  54. if (c)
  55. return c;
  56. c = child->getFirstDescendantOfType<T>();
  57. if (c)
  58. return c;
  59. }
  60. return NULL;
  61. }
  62. /** Adds widget to list of children.
  63. Gives ownership of widget to this widget instance.
  64. */
  65. void addChild(Widget* child);
  66. void addChildBottom(Widget* child);
  67. /** Removes widget from list of children if it exists.
  68. Does not delete widget but transfers ownership to caller
  69. */
  70. void removeChild(Widget* child);
  71. /** Removes and deletes all children */
  72. void clearChildren();
  73. /** Advances the module by one frame */
  74. virtual void step();
  75. struct DrawArgs {
  76. NVGcontext* vg;
  77. math::Rect clipBox;
  78. NVGLUframebuffer* fb = NULL;
  79. };
  80. /** Draws the widget to the NanoVG context */
  81. virtual void draw(const DrawArgs& args);
  82. /** Override draw(const DrawArgs &args) instead */
  83. DEPRECATED virtual void draw(NVGcontext* vg) {}
  84. // Events
  85. /** Recurses an event to all visible Widgets */
  86. template <typename TMethod, class TEvent>
  87. void recurseEvent(TMethod f, const TEvent& e) {
  88. for (auto it = children.rbegin(); it != children.rend(); it++) {
  89. // Stop propagation if requested
  90. if (!e.isPropagating())
  91. break;
  92. Widget* child = *it;
  93. // Filter child by visibility
  94. if (!child->visible)
  95. continue;
  96. // Clone event for (currently) no reason
  97. TEvent e2 = e;
  98. // Call child event handler
  99. (child->*f)(e2);
  100. }
  101. }
  102. /** Recurses an event to all visible Widgets until it is consumed. */
  103. template <typename TMethod, class TEvent>
  104. void recursePositionEvent(TMethod f, const TEvent& e) {
  105. for (auto it = children.rbegin(); it != children.rend(); it++) {
  106. // Stop propagation if requested
  107. if (!e.isPropagating())
  108. break;
  109. Widget* child = *it;
  110. // Filter child by visibility and position
  111. if (!child->visible)
  112. continue;
  113. if (!child->box.isContaining(e.pos))
  114. continue;
  115. // Clone event and adjust its position
  116. TEvent e2 = e;
  117. e2.pos = e.pos.minus(child->box.pos);
  118. // Call child event handler
  119. (child->*f)(e2);
  120. }
  121. }
  122. /** Override these event callbacks to respond to events.
  123. See event.hpp for a description of each event.
  124. */
  125. virtual void onHover(const event::Hover& e) {
  126. recursePositionEvent(&Widget::onHover, e);
  127. }
  128. virtual void onButton(const event::Button& e) {
  129. recursePositionEvent(&Widget::onButton, e);
  130. }
  131. virtual void onDoubleClick(const event::DoubleClick& e) {}
  132. virtual void onHoverKey(const event::HoverKey& e) {
  133. recursePositionEvent(&Widget::onHoverKey, e);
  134. }
  135. virtual void onHoverText(const event::HoverText& e) {
  136. recursePositionEvent(&Widget::onHoverText, e);
  137. }
  138. virtual void onHoverScroll(const event::HoverScroll& e) {
  139. recursePositionEvent(&Widget::onHoverScroll, e);
  140. }
  141. virtual void onEnter(const event::Enter& e) {}
  142. virtual void onLeave(const event::Leave& e) {}
  143. virtual void onSelect(const event::Select& e) {}
  144. virtual void onDeselect(const event::Deselect& e) {}
  145. virtual void onSelectKey(const event::SelectKey& e) {}
  146. virtual void onSelectText(const event::SelectText& e) {}
  147. virtual void onDragStart(const event::DragStart& e) {}
  148. virtual void onDragEnd(const event::DragEnd& e) {}
  149. virtual void onDragMove(const event::DragMove& e) {}
  150. virtual void onDragHover(const event::DragHover& e) {
  151. recursePositionEvent(&Widget::onDragHover, e);
  152. }
  153. virtual void onDragEnter(const event::DragEnter& e) {}
  154. virtual void onDragLeave(const event::DragLeave& e) {}
  155. virtual void onDragDrop(const event::DragDrop& e) {}
  156. virtual void onPathDrop(const event::PathDrop& e) {
  157. recursePositionEvent(&Widget::onPathDrop, e);
  158. }
  159. virtual void onAction(const event::Action& e) {}
  160. virtual void onChange(const event::Change& e) {}
  161. virtual void onZoom(const event::Zoom& e) {
  162. recurseEvent(&Widget::onZoom, e);
  163. }
  164. virtual void onReposition(const event::Reposition& e) {}
  165. virtual void onResize(const event::Resize& e) {}
  166. virtual void onAdd(const event::Add& e) {}
  167. virtual void onRemove(const event::Remove& e) {}
  168. virtual void onShow(const event::Show& e) {
  169. recurseEvent(&Widget::onShow, e);
  170. }
  171. virtual void onHide(const event::Hide& e) {
  172. recurseEvent(&Widget::onHide, e);
  173. }
  174. };
  175. } // namespace widget
  176. } // namespace rack