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.

67 lines
1.2KB

  1. #include "widgets.hpp"
  2. namespace rack {
  3. Menu::~Menu() {
  4. setChildMenu(NULL);
  5. }
  6. void Menu::setChildMenu(Menu *menu) {
  7. if (childMenu) {
  8. if (childMenu->parent)
  9. childMenu->parent->removeChild(childMenu);
  10. delete childMenu;
  11. childMenu = NULL;
  12. }
  13. if (menu) {
  14. childMenu = menu;
  15. assert(parent);
  16. parent->addChild(childMenu);
  17. }
  18. }
  19. void Menu::step() {
  20. Widget::step();
  21. // Set positions of children
  22. box.size = Vec(0, 0);
  23. for (Widget *child : children) {
  24. if (!child->visible)
  25. continue;
  26. // Increase height, set position of child
  27. child->box.pos = Vec(0, box.size.y);
  28. box.size.y += child->box.size.y;
  29. // Increase width based on maximum width of child
  30. if (child->box.size.x > box.size.x) {
  31. box.size.x = child->box.size.x;
  32. }
  33. }
  34. // Resize widths of children
  35. for (Widget *child : children) {
  36. child->box.size.x = box.size.x;
  37. }
  38. // Try to fit into the parent's box
  39. if (parent)
  40. box = box.nudge(Rect(Vec(0, 0), parent->box.size));
  41. }
  42. void Menu::draw(NVGcontext *vg) {
  43. bndMenuBackground(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE);
  44. Widget::draw(vg);
  45. }
  46. void Menu::onScroll(EventScroll &e) {
  47. if (!parent)
  48. return;
  49. if (!parent->box.contains(box))
  50. box.pos = box.pos.plus(e.scrollRel);
  51. e.consumed = true;
  52. }
  53. } // namespace rack