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.

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