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.

134 lines
3.3KB

  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. color = nvgRGB(0xff, 0xd7, 0x14);
  31. bgColor = nvgRGBAf(0, 0, 0, 0);
  32. textOffset = math::Vec(10, 18);
  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. if (font && font->handle >= 0) {
  44. nvgFillColor(args.vg, color);
  45. nvgFontFaceId(args.vg, font->handle);
  46. nvgTextLetterSpacing(args.vg, 0.0);
  47. nvgFontSize(args.vg, 12);
  48. nvgText(args.vg, textOffset.x, textOffset.y, text.c_str(), NULL);
  49. }
  50. nvgResetScissor(args.vg);
  51. }
  52. void LedDisplayChoice::onButton(const ButtonEvent& e) {
  53. OpaqueWidget::onButton(e);
  54. if (e.action == GLFW_PRESS && (e.button == GLFW_MOUSE_BUTTON_LEFT || e.button == GLFW_MOUSE_BUTTON_RIGHT)) {
  55. ActionEvent eAction;
  56. onAction(eAction);
  57. e.consume(this);
  58. }
  59. }
  60. LedDisplayTextField::LedDisplayTextField() {
  61. fontPath = asset::system("res/fonts/ShareTechMono-Regular.ttf");
  62. color = nvgRGB(0xff, 0xd7, 0x14);
  63. textOffset = math::Vec(5, 5);
  64. }
  65. void LedDisplayTextField::draw(const DrawArgs& args) {
  66. nvgScissor(args.vg, RECT_ARGS(args.clipBox));
  67. // Background
  68. nvgBeginPath(args.vg);
  69. nvgRoundedRect(args.vg, 0, 0, box.size.x, box.size.y, 5.0);
  70. nvgFillColor(args.vg, nvgRGB(0x00, 0x00, 0x00));
  71. nvgFill(args.vg);
  72. // Text
  73. std::shared_ptr<Font> font = APP->window->loadFont(fontPath);
  74. if (font && font->handle >= 0) {
  75. bndSetFont(font->handle);
  76. NVGcolor highlightColor = color;
  77. highlightColor.a = 0.5;
  78. int begin = std::min(cursor, selection);
  79. int end = (this == APP->event->selectedWidget) ? std::max(cursor, selection) : -1;
  80. bndIconLabelCaret(args.vg,
  81. textOffset.x, textOffset.y,
  82. box.size.x - 2 * textOffset.x, box.size.y - 2 * textOffset.y,
  83. -1, color, 12, text.c_str(), highlightColor, begin, end);
  84. bndSetFont(APP->window->uiFont->handle);
  85. }
  86. nvgResetScissor(args.vg);
  87. }
  88. int LedDisplayTextField::getTextPosition(math::Vec mousePos) {
  89. std::shared_ptr<Font> font = APP->window->loadFont(fontPath);
  90. if (!font || !font->handle)
  91. return 0;
  92. bndSetFont(font->handle);
  93. int textPos = bndIconLabelTextPosition(APP->window->vg,
  94. textOffset.x, textOffset.y,
  95. box.size.x - 2 * textOffset.x, box.size.y - 2 * textOffset.y,
  96. -1, 12, text.c_str(), mousePos.x, mousePos.y);
  97. bndSetFont(APP->window->uiFont->handle);
  98. return textPos;
  99. }
  100. } // namespace app
  101. } // namespace rack