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.

63 lines
1.2KB

  1. #include "ui/Menu.hpp"
  2. namespace rack {
  3. Menu::Menu() {
  4. box.size = math::Vec(0, 0);
  5. }
  6. Menu::~Menu() {
  7. setChildMenu(NULL);
  8. }
  9. void Menu::setChildMenu(Menu *menu) {
  10. if (childMenu) {
  11. if (childMenu->parent)
  12. childMenu->parent->removeChild(childMenu);
  13. delete childMenu;
  14. childMenu = NULL;
  15. }
  16. if (menu) {
  17. childMenu = menu;
  18. assert(parent);
  19. parent->addChild(childMenu);
  20. }
  21. }
  22. void Menu::step() {
  23. Widget::step();
  24. // Set positions of children
  25. box.size = math::Vec(0, 0);
  26. for (Widget *child : children) {
  27. if (!child->visible)
  28. continue;
  29. // Increment height, set position of child
  30. child->box.pos = math::Vec(0, box.size.y);
  31. box.size.y += child->box.size.y;
  32. // Increase width based on maximum width of child
  33. if (child->box.size.x > box.size.x) {
  34. box.size.x = child->box.size.x;
  35. }
  36. }
  37. // Set widths of all children to maximum width
  38. for (Widget *child : children) {
  39. child->box.size.x = box.size.x;
  40. }
  41. }
  42. void Menu::draw(const DrawContext &ctx) {
  43. bndMenuBackground(ctx.vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE);
  44. Widget::draw(ctx);
  45. }
  46. void Menu::onHoverScroll(const event::HoverScroll &e) {
  47. if (parent && !parent->box.isContaining(box))
  48. box.pos.y += e.scrollDelta.y;
  49. }
  50. } // namespace rack