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.

96 lines
2.6KB

  1. #include "ui/MenuItem.hpp"
  2. #include "ui/MenuOverlay.hpp"
  3. namespace rack {
  4. namespace ui {
  5. #define BND_LABEL_FONT_SIZE 13
  6. void MenuItem::draw(const DrawArgs &args) {
  7. BNDwidgetState state = BND_DEFAULT;
  8. if (APP->event->hoveredWidget == this)
  9. state = BND_HOVER;
  10. // Set active state if this MenuItem
  11. Menu *parentMenu = dynamic_cast<Menu*>(parent);
  12. if (parentMenu && parentMenu->activeEntry == this)
  13. state = BND_ACTIVE;
  14. // Main text and background
  15. if (!disabled)
  16. bndMenuItem(args.vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str());
  17. else
  18. bndMenuLabel(args.vg, 0.0, 0.0, box.size.x, box.size.y, -1, text.c_str());
  19. // Right text
  20. float x = box.size.x - bndLabelWidth(args.vg, -1, rightText.c_str());
  21. NVGcolor rightColor = (state == BND_DEFAULT && !disabled) ? bndGetTheme()->menuTheme.textColor : bndGetTheme()->menuTheme.textSelectedColor;
  22. bndIconLabelValue(args.vg, x, 0.0, box.size.x, box.size.y, -1, rightColor, BND_LEFT, BND_LABEL_FONT_SIZE, rightText.c_str(), NULL);
  23. }
  24. void MenuItem::step() {
  25. // Add 10 more pixels because measurements on high-DPI screens are sometimes too small for some reason
  26. const float rightPadding = 10.0;
  27. // HACK use APP->window->vg from the window.
  28. // 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.
  29. box.size.x = bndLabelWidth(APP->window->vg, -1, text.c_str()) + bndLabelWidth(APP->window->vg, -1, rightText.c_str()) + rightPadding;
  30. Widget::step();
  31. }
  32. void MenuItem::onEnter(const event::Enter &e) {
  33. e.consume(this);
  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::onDragStart(const event::DragStart &e) {
  47. if (e.button != GLFW_MOUSE_BUTTON_LEFT)
  48. return;
  49. e.consume(this);
  50. }
  51. void MenuItem::onDragDrop(const event::DragDrop &e) {
  52. if (e.origin != this)
  53. return;
  54. doAction();
  55. }
  56. void MenuItem::doAction() {
  57. if (disabled)
  58. return;
  59. event::Context cAction;
  60. event::Action eAction;
  61. eAction.context = &cAction;
  62. // Consume event by default, but allow action to un-consume it to prevent the menu from being removed.
  63. eAction.consume(this);
  64. onAction(eAction);
  65. if (!cAction.target)
  66. return;
  67. MenuOverlay *overlay = getAncestorOfType<MenuOverlay>();
  68. if (overlay) {
  69. overlay->requestedDelete = true;
  70. }
  71. }
  72. } // namespace ui
  73. } // namespace rack