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.

83 lines
2.0KB

  1. #pragma once
  2. #include <list>
  3. #include "common.hpp"
  4. #include "math.hpp"
  5. #include "window.hpp"
  6. #include "color.hpp"
  7. namespace rack {
  8. namespace event {
  9. struct Event;
  10. } // namespace event
  11. /** A node in the 2D scene graph */
  12. struct Widget {
  13. /** Stores position and size */
  14. math::Rect box = math::Rect(math::Vec(), math::Vec(INFINITY, INFINITY));
  15. Widget *parent = NULL;
  16. std::list<Widget*> children;
  17. /** Disable rendering but continue stepping */
  18. bool visible = true;
  19. /** If set to true, parent will delete Widget in the next step() */
  20. bool requestedDelete = false;
  21. virtual ~Widget();
  22. virtual math::Rect getChildrenBoundingBox();
  23. /** Returns `v` transformed into the coordinate system of `relative` */
  24. virtual math::Vec getRelativeOffset(math::Vec v, Widget *relative);
  25. /** Returns `v` transformed into world coordinates */
  26. math::Vec getAbsoluteOffset(math::Vec v) {
  27. return getRelativeOffset(v, NULL);
  28. }
  29. /** Returns a subset of the given math::Rect bounded by the box of this widget and all ancestors */
  30. virtual math::Rect getViewport(math::Rect r);
  31. template <class T>
  32. T *getAncestorOfType() {
  33. if (!parent) return NULL;
  34. T *p = dynamic_cast<T*>(parent);
  35. if (p) return p;
  36. return parent->getAncestorOfType<T>();
  37. }
  38. template <class T>
  39. T *getFirstDescendantOfType() {
  40. for (Widget *child : children) {
  41. T *c = dynamic_cast<T*>(child);
  42. if (c) return c;
  43. c = child->getFirstDescendantOfType<T>();
  44. if (c) return c;
  45. }
  46. return NULL;
  47. }
  48. /** Adds widget to list of children.
  49. Gives ownership of widget to this widget instance.
  50. */
  51. void addChild(Widget *widget);
  52. /** Removes widget from list of children if it exists.
  53. Does not delete widget but transfers ownership to caller
  54. */
  55. void removeChild(Widget *widget);
  56. /** Removes and deletes all children */
  57. void clearChildren();
  58. /** Advances the module by one frame */
  59. virtual void step();
  60. /** Draws to NanoVG context */
  61. virtual void draw(NVGcontext *vg);
  62. /** Trigger an event on this Widget. */
  63. virtual void handleEvent(event::Event &e) {}
  64. };
  65. } // namespace rack