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.

212 lines
6.0KB

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