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.

MenuItem.hpp 2.2KB

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