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.

65 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. if (childMenu->parent)
  13. childMenu->parent->removeChild(childMenu);
  14. delete childMenu;
  15. childMenu = NULL;
  16. }
  17. if (menu) {
  18. childMenu = menu;
  19. assert(parent);
  20. parent->addChild(childMenu);
  21. }
  22. }
  23. void Menu::step() {
  24. Widget::step();
  25. // Set positions of children
  26. box.size = math::Vec(0, 0);
  27. for (widget::Widget *child : children) {
  28. if (!child->visible)
  29. continue;
  30. // Increment height, set position of child
  31. child->box.pos = math::Vec(0, box.size.y);
  32. box.size.y += child->box.size.y;
  33. // Increase width based on maximum width of child
  34. if (child->box.size.x > box.size.x) {
  35. box.size.x = child->box.size.x;
  36. }
  37. }
  38. // Set widths of all children to maximum width
  39. for (widget::Widget *child : children) {
  40. child->box.size.x = box.size.x;
  41. }
  42. }
  43. void Menu::draw(const DrawArgs &args) {
  44. bndMenuBackground(args.vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE);
  45. Widget::draw(args);
  46. }
  47. void Menu::onHoverScroll(const event::HoverScroll &e) {
  48. if (parent && !parent->box.isContaining(box))
  49. box.pos.y += e.scrollDelta.y;
  50. }
  51. } // namespace ui
  52. } // namespace rack