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.

95 lines
2.5KB

  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. if (active)
  15. state = BND_ACTIVE;
  16. // Main text and background
  17. if (!disabled)
  18. bndMenuItem(args.vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str());
  19. else
  20. bndMenuLabel(args.vg, 0.0, 0.0, box.size.x, box.size.y, -1, text.c_str());
  21. // Right text
  22. float x = box.size.x - bndLabelWidth(args.vg, -1, rightText.c_str());
  23. NVGcolor rightColor = (state == BND_DEFAULT && !disabled) ? bndGetTheme()->menuTheme.textColor : bndGetTheme()->menuTheme.textSelectedColor;
  24. bndIconLabelValue(args.vg, x, 0.0, box.size.x, box.size.y, -1, rightColor, BND_LEFT, BND_LABEL_FONT_SIZE, rightText.c_str(), NULL);
  25. }
  26. void MenuItem::step() {
  27. // Add 10 more pixels because measurements on high-DPI screens are sometimes too small for some reason
  28. const float rightPadding = 10.0;
  29. // HACK use APP->window->vg from the window.
  30. // 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.
  31. box.size.x = bndLabelWidth(APP->window->vg, -1, text.c_str()) + bndLabelWidth(APP->window->vg, -1, rightText.c_str()) + rightPadding;
  32. Widget::step();
  33. }
  34. void MenuItem::onEnter(const EnterEvent& e) {
  35. Menu* parentMenu = dynamic_cast<Menu*>(parent);
  36. if (!parentMenu)
  37. return;
  38. parentMenu->activeEntry = NULL;
  39. // Try to create child menu
  40. Menu* childMenu = createChildMenu();
  41. if (childMenu) {
  42. parentMenu->activeEntry = this;
  43. childMenu->box.pos = parent->box.pos.plus(box.getTopRight());
  44. }
  45. parentMenu->setChildMenu(childMenu);
  46. }
  47. void MenuItem::onDragDrop(const DragDropEvent& e) {
  48. if (e.origin != this)
  49. return;
  50. doAction();
  51. }
  52. void MenuItem::doAction() {
  53. if (disabled)
  54. return;
  55. widget::EventContext cAction;
  56. ActionEvent eAction;
  57. eAction.context = &cAction;
  58. // Consume event by default, but allow action to un-consume it to prevent the menu from being removed.
  59. eAction.consume(this);
  60. onAction(eAction);
  61. if (!cAction.consumed)
  62. return;
  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