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