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.

80 lines
1.5KB

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