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.

78 lines
2.2KB

  1. #pragma once
  2. #include "ui/common.hpp"
  3. #include "ui/Menu.hpp"
  4. #include "ui/MenuEntry.hpp"
  5. #include "ui/MenuOverlay.hpp"
  6. namespace rack {
  7. #define BND_LABEL_FONT_SIZE 13
  8. struct MenuItem : MenuEntry {
  9. std::string text;
  10. std::string rightText;
  11. void draw(NVGcontext *vg) override {
  12. // Get state
  13. BNDwidgetState state = (event::gContext->hoveredWidget == this) ? BND_HOVER : BND_DEFAULT;
  14. Menu *parentMenu = dynamic_cast<Menu*>(parent);
  15. if (parentMenu && parentMenu->activeEntry == this) {
  16. state = BND_ACTIVE;
  17. }
  18. bndMenuItem(vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str());
  19. float x = box.size.x - bndLabelWidth(vg, -1, rightText.c_str());
  20. NVGcolor rightColor = (state == BND_DEFAULT) ? bndGetTheme()->menuTheme.textColor : bndGetTheme()->menuTheme.textSelectedColor;
  21. bndIconLabelValue(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 step() override {
  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 gVg from the window.
  27. // All this does is inspect the font, so it shouldn't modify gVg and should work when called from a FramebufferWidget for example.
  28. box.size.x = bndLabelWidth(gVg, -1, text.c_str()) + bndLabelWidth(gVg, -1, rightText.c_str()) + rightPadding;
  29. Widget::step();
  30. }
  31. virtual Menu *createChildMenu() {return NULL;}
  32. void onEnter(event::Enter &e) override {
  33. Menu *parentMenu = dynamic_cast<Menu*>(parent);
  34. if (!parentMenu)
  35. return;
  36. parentMenu->activeEntry = NULL;
  37. // Try to create child menu
  38. Menu *childMenu = createChildMenu();
  39. if (childMenu) {
  40. parentMenu->activeEntry = this;
  41. childMenu->box.pos = parent->box.pos.plus(box.getTopRight());
  42. }
  43. parentMenu->setChildMenu(childMenu);
  44. }
  45. void onDragDrop(event::DragDrop &e) override {
  46. if (e.origin != this)
  47. return;
  48. event::Action eAction;
  49. // Consume event by default, but allow action to un-consume it to prevent the menu from being removed.
  50. eAction.target = this;
  51. onAction(eAction);
  52. if (!eAction.target)
  53. return;
  54. Widget *overlay = getAncestorOfType<MenuOverlay>();
  55. overlay->requestedDelete = true;
  56. }
  57. };
  58. } // namespace rack