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.

87 lines
2.1KB

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