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.

275 lines
6.0KB

  1. #include "LRComponents.hpp"
  2. /**
  3. * @brief Constructor of LCD Widget
  4. */
  5. LCDWidget::LCDWidget(NVGcolor fg, unsigned char length) {
  6. /** load LCD ttf font */
  7. gLCDFont_DIG7 = Font::load(assetPlugin(plugin, LCD_FONT_DIG7));
  8. auto r = (unsigned char) (fg.r * 0xFF);
  9. auto g = (unsigned char) (fg.g * 0xFF);
  10. auto b = (unsigned char) (fg.b * 0xFF);
  11. LCDWidget::length = length;
  12. LCDWidget::fg = fg;
  13. LCDWidget::bg = nvgRGBA(r - 0x40, g - 0x40, b - 0x40, 0x40);
  14. }
  15. LRModuleWidget::LRModuleWidget(Module *module) : ModuleWidget(module) {
  16. }
  17. /**
  18. * @brief Draw method of custom LCD widget
  19. * @param vg
  20. */
  21. void LCDWidget::draw(NVGcontext *vg) {
  22. nvgFontSize(vg, LCD_FONTSIZE);
  23. nvgFontFaceId(vg, gLCDFont_DIG7->handle);
  24. nvgTextLetterSpacing(vg, LCD_LETTER_SPACING);
  25. nvgFillColor(vg, bg);
  26. std::string s1;
  27. std::string s2;
  28. for (int i = 0; i < LCDWidget::length; ++i) {
  29. s1.append("8");
  30. s2.append(":");
  31. }
  32. nvgTextBox(vg, 0, 0, 220, s1.c_str(), nullptr);
  33. nvgTextBox(vg, 0, 0, 220, s2.c_str(), nullptr);
  34. nvgFillColor(vg, fg);
  35. nvgTextBox(vg, 0, 0, 220, text.c_str(), nullptr);
  36. }
  37. void LRRedLight::draw(NVGcontext *vg) {
  38. //LightWidget::draw(vg);
  39. float radius = box.size.x / 1.5f;
  40. float oradius = radius + 10.0f;
  41. /* color.r = clampf(color.r, 0.0f, 1.0f);
  42. color.g = clampf(color.g, 0.0f, 1.0f);
  43. color.b = clampf(color.b, 0.0f, 1.0f);
  44. color.a = clampf(color.a, 0.0f, 1.0f);*/
  45. // Solid
  46. nvgBeginPath(vg);
  47. nvgCircle(vg, radius, radius, radius);
  48. nvgFillColor(vg, bgColor);
  49. nvgFill(vg);
  50. // Border
  51. nvgStrokeWidth(vg, 1.0f);
  52. NVGcolor borderColor = bgColor;
  53. borderColor.a *= 0.5f;
  54. nvgStrokeColor(vg, borderColor);
  55. nvgStroke(vg);
  56. // Inner glow
  57. nvgGlobalCompositeOperation(vg, NVG_LIGHTER);
  58. nvgFillColor(vg, color);
  59. nvgFill(vg);
  60. // Outer glow
  61. nvgBeginPath(vg);
  62. nvgRect(vg, radius - oradius, radius - oradius, 2 * oradius, 2 * oradius);
  63. NVGpaint paint;
  64. NVGcolor icol = color;
  65. icol.a *= 0.40f;
  66. NVGcolor ocol = color;
  67. ocol.a = 0.00f;
  68. paint = nvgRadialGradient(vg, radius, radius, radius, oradius, icol, ocol);
  69. nvgFillPaint(vg, paint);
  70. nvgFill(vg);
  71. }
  72. /**
  73. * @brief Constructor
  74. */
  75. LRRedLight::LRRedLight() {
  76. addBaseColor(COLOR_RED);
  77. }
  78. /**
  79. * @brief Draw routine for cv indicator
  80. * @param vg
  81. */
  82. void Indicator::draw(NVGcontext *vg) {
  83. NVGcolor current = normalColor;
  84. if (active) {
  85. /** underrun */
  86. if (cv < 0.f - OVERFLOW_THRESHOLD) {
  87. cv = 0.f - OVERFLOW_THRESHOLD;
  88. current = overflowColor;
  89. }
  90. /** overrun */
  91. if (cv > 1.f + OVERFLOW_THRESHOLD) {
  92. cv = 1.f + OVERFLOW_THRESHOLD;
  93. current = overflowColor;
  94. }
  95. float a = -angle + cv * angle2;
  96. float d = distance - 4.f;
  97. Vec p1, p2, p3;
  98. /** compute correct point of indicator on circle */
  99. p1.x = middle.x - sin(-a * (float) M_PI) * distance;
  100. p1.y = middle.y - cos(-a * (float) M_PI) * distance;
  101. p2.x = middle.x - sin(-(a + 0.1f) * (float) M_PI) * d;
  102. p2.y = middle.y - cos(-(a + 0.1f) * (float) M_PI) * d;
  103. p3.x = middle.x - sin(-(a - 0.1f) * (float) M_PI) * d;
  104. p3.y = middle.y - cos(-(a - 0.1f) * (float) M_PI) * d;
  105. nvgBeginPath(vg);
  106. nvgMoveTo(vg, p1.x, p1.y);
  107. nvgLineTo(vg, p2.x, p2.y);
  108. nvgLineTo(vg, p3.x, p3.y);
  109. nvgLineTo(vg, p1.x, p1.y);
  110. nvgClosePath(vg);
  111. nvgFillColor(vg, current);
  112. nvgFill(vg);
  113. }
  114. }
  115. /**
  116. * @brief Draw shadow for circular knobs
  117. * @param vg NVGcontext
  118. * @param strength Alpha value of outside gradient
  119. * @param size Outer size
  120. * @param shift XY Offset shift from middle
  121. */
  122. void LRShadow::drawShadow(NVGcontext *vg, float strength, float size) {
  123. // add shadow
  124. nvgBeginPath(vg);
  125. nvgRect(vg, -20, -20, box.size.x + 40, box.size.y + 40);
  126. NVGcolor icol = nvgRGBAf(0.0f, 0.0f, 0.0f, strength);
  127. NVGcolor ocol = nvgRGBAf(0.0f, 0.0f, 0.0f, 0.f);;
  128. NVGpaint paint = nvgRadialGradient(vg, box.size.x / 2 + shadowPos.x, box.size.y / 2 + shadowPos.y,
  129. box.size.x * 0.3f, box.size.x * size, icol, ocol);
  130. nvgFillPaint(vg, paint);
  131. nvgFill(vg);
  132. }
  133. /**
  134. * @brief Hook into widget draw routine to simulate shadow
  135. * @param vg
  136. */
  137. void LRShadow::draw(NVGcontext *vg) {
  138. drawShadow(vg, strength, size);
  139. }
  140. /**
  141. * @brief Setter for box dimensions
  142. * @param box
  143. */
  144. void LRShadow::setBox(const Rect &box) {
  145. LRShadow::box = box;
  146. }
  147. /**
  148. * @brief Setter for outer radius size
  149. * @param size
  150. */
  151. void LRShadow::setSize(float size) {
  152. LRShadow::size = size;
  153. }
  154. /**
  155. * @brief Setter for draw strength of shadow
  156. * @param strength
  157. */
  158. void LRShadow::setStrength(float strength) {
  159. LRShadow::strength = strength;
  160. }
  161. /**
  162. * @brief Extention for panel background
  163. * @param vg
  164. */
  165. void LRPanel::draw(NVGcontext *vg) {
  166. FramebufferWidget::draw(vg);
  167. nvgBeginPath(vg);
  168. nvgRect(vg, -MARGIN, -MARGIN, box.size.x + MARGIN * 2, box.size.y + MARGIN * 2);
  169. NVGpaint paint = nvgLinearGradient(vg, offset.x, offset.y, box.size.x, box.size.y, inner, outer);
  170. nvgFillPaint(vg, paint);
  171. nvgFill(vg);
  172. }
  173. void LRPanel::setInner(const NVGcolor &inner) {
  174. LRPanel::inner = inner;
  175. }
  176. void LRPanel::setOuter(const NVGcolor &outer) {
  177. LRPanel::outer = outer;
  178. }
  179. LRPanel::LRPanel() {}
  180. SVGRotator::SVGRotator() : FramebufferWidget() {
  181. tw = new TransformWidget();
  182. addChild(tw);
  183. sw = new SVGWidget();
  184. tw->addChild(sw);
  185. }
  186. /**
  187. * @brief Set SVG image to rotator
  188. * @param svg
  189. */
  190. void SVGRotator::setSVG(std::shared_ptr<SVG> svg) {
  191. sw->setSVG(svg);
  192. tw->box.size = sw->box.size;
  193. box.size = sw->box.size;
  194. }
  195. /**
  196. * @brief Rotate one step
  197. */
  198. void SVGRotator::step() {
  199. tw->identity();
  200. angle = fmodf(angle + inc, 2 * M_PI);;
  201. Vec center = sw->box.getCenter();
  202. tw->translate(center);
  203. tw->rotate(angle);
  204. tw->translate(center.neg());
  205. dirty = true;
  206. FramebufferWidget::step();
  207. }