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.

82 lines
1.6KB

  1. #pragma once
  2. #include "widgets/OpaqueWidget.hpp"
  3. #include "ui/common.hpp"
  4. #include "ui/MenuEntry.hpp"
  5. namespace rack {
  6. struct Menu : OpaqueWidget {
  7. Menu *parentMenu = NULL;
  8. Menu *childMenu = NULL;
  9. /** The entry which created the child menu */
  10. MenuEntry *activeEntry = NULL;
  11. Menu() {
  12. box.size = Vec(0, 0);
  13. }
  14. ~Menu() {
  15. setChildMenu(NULL);
  16. }
  17. /** Deprecated. Just use addChild(child) instead */
  18. DEPRECATED void pushChild(Widget *child) {
  19. addChild(child);
  20. }
  21. void setChildMenu(Menu *menu) {
  22. if (childMenu) {
  23. if (childMenu->parent)
  24. childMenu->parent->removeChild(childMenu);
  25. delete childMenu;
  26. childMenu = NULL;
  27. }
  28. if (menu) {
  29. childMenu = menu;
  30. assert(parent);
  31. parent->addChild(childMenu);
  32. }
  33. }
  34. void step() override {
  35. Widget::step();
  36. // Set positions of children
  37. box.size = Vec(0, 0);
  38. for (Widget *child : children) {
  39. if (!child->visible)
  40. continue;
  41. // Increment height, set position of child
  42. child->box.pos = Vec(0, box.size.y);
  43. box.size.y += child->box.size.y;
  44. // Increase width based on maximum width of child
  45. if (child->box.size.x > box.size.x) {
  46. box.size.x = child->box.size.x;
  47. }
  48. }
  49. // Resize widths of children
  50. for (Widget *child : children) {
  51. child->box.size.x = box.size.x;
  52. }
  53. }
  54. void draw(NVGcontext *vg) override {
  55. bndMenuBackground(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE);
  56. Widget::draw(vg);
  57. }
  58. void onHoverScroll(event::HoverScroll &e) override {
  59. if (!parent)
  60. return;
  61. if (!parent->box.contains(box))
  62. box.pos.y += e.scrollDelta.y;
  63. // e.consumed = true;
  64. }
  65. };
  66. } // namespace rack