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.

91 lines
2.5KB

  1. #include <ui/MenuItem.hpp>
  2. #include <ui/MenuOverlay.hpp>
  3. namespace rack {
  4. namespace ui {
  5. void MenuItem::draw(const DrawArgs& args) {
  6. BNDwidgetState state = BND_DEFAULT;
  7. if (APP->event->hoveredWidget == this)
  8. state = BND_HOVER;
  9. // Set active state if this MenuItem
  10. Menu* parentMenu = dynamic_cast<Menu*>(parent);
  11. if (parentMenu && parentMenu->activeEntry == this)
  12. state = BND_ACTIVE;
  13. // Main text and background
  14. if (!disabled)
  15. bndMenuItem(args.vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str());
  16. else
  17. bndMenuLabel(args.vg, 0.0, 0.0, box.size.x, box.size.y, -1, text.c_str());
  18. // Right text
  19. float x = box.size.x - bndLabelWidth(args.vg, -1, rightText.c_str());
  20. NVGcolor rightColor = (state == BND_DEFAULT && !disabled) ? bndGetTheme()->menuTheme.textColor : bndGetTheme()->menuTheme.textSelectedColor;
  21. bndIconLabelValue(args.vg, x, 0.0, box.size.x, box.size.y, -1, rightColor, BND_LEFT, BND_LABEL_FONT_SIZE, rightText.c_str(), NULL);
  22. }
  23. void MenuItem::step() {
  24. // Add 10 more pixels because measurements on high-DPI screens are sometimes too small for some reason
  25. const float rightPadding = 10.0;
  26. // HACK use APP->window->vg from the window.
  27. // All this does is inspect the font, so it shouldn't modify APP->window->vg and should work when called from a widget::FramebufferWidget for example.
  28. box.size.x = bndLabelWidth(APP->window->vg, -1, text.c_str()) + rightPadding;
  29. if (!rightText.empty())
  30. box.size.x += bndLabelWidth(APP->window->vg, -1, rightText.c_str()) - 10.0;
  31. Widget::step();
  32. }
  33. void MenuItem::onEnter(const EnterEvent& e) {
  34. Menu* parentMenu = dynamic_cast<Menu*>(parent);
  35. if (!parentMenu)
  36. return;
  37. parentMenu->activeEntry = NULL;
  38. // Try to create child menu
  39. Menu* childMenu = createChildMenu();
  40. if (childMenu) {
  41. parentMenu->activeEntry = this;
  42. childMenu->box.pos = parent->box.pos.plus(box.getTopRight());
  43. }
  44. parentMenu->setChildMenu(childMenu);
  45. }
  46. void MenuItem::onDragDrop(const DragDropEvent& e) {
  47. if (e.origin == this && !disabled) {
  48. int mods = APP->window->getMods();
  49. doAction((mods & RACK_MOD_MASK) != RACK_MOD_CTRL);
  50. }
  51. }
  52. void MenuItem::doAction(bool consume) {
  53. widget::EventContext cAction;
  54. ActionEvent eAction;
  55. eAction.context = &cAction;
  56. if (consume) {
  57. eAction.consume(this);
  58. }
  59. onAction(eAction);
  60. if (!cAction.consumed)
  61. return;
  62. // Close menu
  63. MenuOverlay* overlay = getAncestorOfType<MenuOverlay>();
  64. if (overlay) {
  65. overlay->requestDelete();
  66. }
  67. }
  68. void MenuItem::onAction(const ActionEvent& e) {
  69. }
  70. } // namespace ui
  71. } // namespace rack