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.

139 lines
3.5KB

  1. #include <app/LedDisplay.hpp>
  2. #include <asset.hpp>
  3. #include <Window.hpp>
  4. #include <context.hpp>
  5. namespace rack {
  6. namespace app {
  7. void LedDisplay::draw(const DrawArgs& args) {
  8. nvgBeginPath(args.vg);
  9. nvgRoundedRect(args.vg, 0, 0, box.size.x, box.size.y, 5.0);
  10. nvgFillColor(args.vg, nvgRGB(0x00, 0x00, 0x00));
  11. nvgFill(args.vg);
  12. nvgScissor(args.vg, RECT_ARGS(args.clipBox));
  13. Widget::draw(args);
  14. nvgResetScissor(args.vg);
  15. }
  16. LedDisplaySeparator::LedDisplaySeparator() {
  17. box.size = math::Vec();
  18. }
  19. void LedDisplaySeparator::draw(const DrawArgs& args) {
  20. nvgBeginPath(args.vg);
  21. nvgMoveTo(args.vg, 0, 0);
  22. nvgLineTo(args.vg, box.size.x, box.size.y);
  23. nvgStrokeWidth(args.vg, 1.0);
  24. nvgStrokeColor(args.vg, nvgRGB(0x33, 0x33, 0x33));
  25. nvgStroke(args.vg);
  26. }
  27. LedDisplayChoice::LedDisplayChoice() {
  28. box.size = mm2px(math::Vec(0, 28.0 / 3));
  29. fontPath = asset::system("res/fonts/ShareTechMono-Regular.ttf");
  30. textOffset = math::Vec(10, 18);
  31. color = nvgRGB(0xff, 0xd7, 0x14);
  32. bgColor = nvgRGBAf(0, 0, 0, 0);
  33. }
  34. void LedDisplayChoice::draw(const DrawArgs& args) {
  35. nvgScissor(args.vg, RECT_ARGS(args.clipBox));
  36. if (bgColor.a > 0.0) {
  37. nvgBeginPath(args.vg);
  38. nvgRect(args.vg, 0, 0, box.size.x, box.size.y);
  39. nvgFillColor(args.vg, bgColor);
  40. nvgFill(args.vg);
  41. }
  42. std::shared_ptr<Font> font = APP->window->loadFont(fontPath);
  43. nvgGlobalAlpha(args.vg, 1.0);
  44. if (font && font->handle >= 0) {
  45. nvgFillColor(args.vg, color);
  46. nvgFontFaceId(args.vg, font->handle);
  47. nvgTextLetterSpacing(args.vg, 0.0);
  48. nvgFontSize(args.vg, 12);
  49. nvgText(args.vg, textOffset.x, textOffset.y, text.c_str(), NULL);
  50. }
  51. nvgResetScissor(args.vg);
  52. }
  53. void LedDisplayChoice::onButton(const ButtonEvent& e) {
  54. OpaqueWidget::onButton(e);
  55. if (e.action == GLFW_PRESS && (e.button == GLFW_MOUSE_BUTTON_LEFT || e.button == GLFW_MOUSE_BUTTON_RIGHT)) {
  56. ActionEvent eAction;
  57. onAction(eAction);
  58. e.consume(this);
  59. }
  60. }
  61. LedDisplayTextField::LedDisplayTextField() {
  62. fontPath = asset::system("res/fonts/ShareTechMono-Regular.ttf");
  63. textOffset = math::Vec(5, 5);
  64. color = nvgRGB(0xff, 0xd7, 0x14);
  65. bgColor = nvgRGB(0x00, 0x00, 0x00);
  66. }
  67. void LedDisplayTextField::draw(const DrawArgs& args) {
  68. nvgScissor(args.vg, RECT_ARGS(args.clipBox));
  69. // Background
  70. if (bgColor.a > 0.0) {
  71. nvgBeginPath(args.vg);
  72. nvgRoundedRect(args.vg, 0, 0, box.size.x, box.size.y, 5.0);
  73. nvgFillColor(args.vg, nvgRGB(0x00, 0x00, 0x00));
  74. nvgFill(args.vg);
  75. }
  76. // Text
  77. std::shared_ptr<Font> font = APP->window->loadFont(fontPath);
  78. nvgGlobalAlpha(args.vg, 1.0);
  79. if (font && font->handle >= 0) {
  80. bndSetFont(font->handle);
  81. NVGcolor highlightColor = color;
  82. highlightColor.a = 0.5;
  83. int begin = std::min(cursor, selection);
  84. int end = (this == APP->event->selectedWidget) ? std::max(cursor, selection) : -1;
  85. bndIconLabelCaret(args.vg,
  86. textOffset.x, textOffset.y,
  87. box.size.x - 2 * textOffset.x, box.size.y - 2 * textOffset.y,
  88. -1, color, 12, text.c_str(), highlightColor, begin, end);
  89. bndSetFont(APP->window->uiFont->handle);
  90. }
  91. nvgResetScissor(args.vg);
  92. }
  93. int LedDisplayTextField::getTextPosition(math::Vec mousePos) {
  94. std::shared_ptr<Font> font = APP->window->loadFont(fontPath);
  95. if (!font || !font->handle)
  96. return 0;
  97. bndSetFont(font->handle);
  98. int textPos = bndIconLabelTextPosition(APP->window->vg,
  99. textOffset.x, textOffset.y,
  100. box.size.x - 2 * textOffset.x, box.size.y - 2 * textOffset.y,
  101. -1, 12, text.c_str(), mousePos.x, mousePos.y);
  102. bndSetFont(APP->window->uiFont->handle);
  103. return textPos;
  104. }
  105. } // namespace app
  106. } // namespace rack