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