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.

69 lines
1.3KB

  1. #include <ui/Menu.hpp>
  2. namespace rack {
  3. namespace ui {
  4. Menu::Menu() {
  5. box.size = math::Vec(0, 0);
  6. }
  7. Menu::~Menu() {
  8. setChildMenu(NULL);
  9. }
  10. void Menu::setChildMenu(Menu* menu) {
  11. if (childMenu) {
  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::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::Widget* child : children) {
  39. child->box.size.x = box.size.x;
  40. }
  41. // Fit inside parent
  42. assert(parent);
  43. box = box.nudge(parent->box.zeroPos());
  44. }
  45. void Menu::draw(const DrawArgs& args) {
  46. bndMenuBackground(args.vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE);
  47. Widget::draw(args);
  48. }
  49. void Menu::onHoverScroll(const event::HoverScroll& e) {
  50. if (parent && !parent->box.isContaining(box))
  51. box.pos.y += e.scrollDelta.y;
  52. }
  53. } // namespace ui
  54. } // namespace rack