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 6.0KB

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