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.

197 lines
4.8KB

  1. #include "app.hpp"
  2. #include "engine.hpp"
  3. #include "components.hpp"
  4. namespace rack {
  5. static void drawPlug(NVGcontext *vg, Vec pos, NVGcolor color) {
  6. NVGcolor colorOutline = nvgLerpRGBA(color, nvgRGBf(0.0, 0.0, 0.0), 0.5);
  7. // Plug solid
  8. nvgBeginPath(vg);
  9. nvgCircle(vg, pos.x, pos.y, 9.5);
  10. nvgFillColor(vg, color);
  11. nvgFill(vg);
  12. // Border
  13. nvgStrokeWidth(vg, 1.0);
  14. nvgStrokeColor(vg, colorOutline);
  15. nvgStroke(vg);
  16. // Hole
  17. nvgBeginPath(vg);
  18. nvgCircle(vg, pos.x, pos.y, 5.5);
  19. nvgFillColor(vg, nvgRGBf(0.0, 0.0, 0.0));
  20. nvgFill(vg);
  21. }
  22. static void drawWire(NVGcontext *vg, Vec pos1, Vec pos2, NVGcolor color, float tension, float opacity) {
  23. NVGcolor colorShadow = nvgRGBAf(0, 0, 0, 0.08);
  24. NVGcolor colorOutline = nvgLerpRGBA(color, nvgRGBf(0.0, 0.0, 0.0), 0.5);
  25. // Wire
  26. if (opacity > 0.0) {
  27. nvgSave(vg);
  28. nvgGlobalAlpha(vg, powf(opacity, 1.5));
  29. float dist = pos1.minus(pos2).norm();
  30. Vec slump;
  31. slump.y = (1.0 - tension) * (150.0 + 1.0*dist);
  32. Vec pos3 = pos1.plus(pos2).div(2).plus(slump);
  33. nvgLineJoin(vg, NVG_ROUND);
  34. // Shadow
  35. Vec pos4 = pos3.plus(slump.mult(0.08));
  36. nvgBeginPath(vg);
  37. nvgMoveTo(vg, pos1.x, pos1.y);
  38. nvgQuadTo(vg, pos4.x, pos4.y, pos2.x, pos2.y);
  39. nvgStrokeColor(vg, colorShadow);
  40. nvgStrokeWidth(vg, 5);
  41. nvgStroke(vg);
  42. // Wire outline
  43. nvgBeginPath(vg);
  44. nvgMoveTo(vg, pos1.x, pos1.y);
  45. nvgQuadTo(vg, pos3.x, pos3.y, pos2.x, pos2.y);
  46. nvgStrokeColor(vg, colorOutline);
  47. nvgStrokeWidth(vg, 5);
  48. nvgStroke(vg);
  49. // Wire solid
  50. nvgStrokeColor(vg, color);
  51. nvgStrokeWidth(vg, 3);
  52. nvgStroke(vg);
  53. nvgRestore(vg);
  54. }
  55. }
  56. static const NVGcolor wireColors[6] = {
  57. nvgRGB(0xc9, 0xb7, 0x0e), // yellow
  58. nvgRGB(0xc9, 0x18, 0x47), // red
  59. nvgRGB(0x0c, 0x8e, 0x15), // green
  60. nvgRGB(0x09, 0x86, 0xad), // blue
  61. nvgRGB(0x44, 0x44, 0x44), // black
  62. // nvgRGB(0x66, 0x66, 0x66), // gray
  63. // nvgRGB(0x88, 0x88, 0x88), // light gray
  64. nvgRGB(0xaa, 0xaa, 0xaa), // white
  65. };
  66. static int lastWireColorId = -1;
  67. WireWidget::WireWidget() {
  68. lastWireColorId = (lastWireColorId + 1) % 6;
  69. color = wireColors[lastWireColorId];
  70. PolarityLight *inputPolarityLight = new MediumLight<PolarityLight>();
  71. PolarityLight *outputPolarityLight = new MediumLight<PolarityLight>();
  72. outputPolarityLight->posColor = inputPolarityLight->posColor = COLOR_GREEN;
  73. outputPolarityLight->negColor = inputPolarityLight->negColor = COLOR_RED;
  74. inputLight = inputPolarityLight;
  75. outputLight = outputPolarityLight;
  76. addChild(inputLight);
  77. addChild(outputLight);
  78. }
  79. WireWidget::~WireWidget() {
  80. outputPort = NULL;
  81. inputPort = NULL;
  82. updateWire();
  83. }
  84. void WireWidget::updateWire() {
  85. if (inputPort && outputPort) {
  86. // Check correct types
  87. assert(inputPort->type == Port::INPUT);
  88. assert(outputPort->type == Port::OUTPUT);
  89. if (!wire) {
  90. wire = new Wire();
  91. wire->outputModule = outputPort->module;
  92. wire->outputId = outputPort->portId;
  93. wire->inputModule = inputPort->module;
  94. wire->inputId = inputPort->portId;
  95. engineAddWire(wire);
  96. }
  97. }
  98. else {
  99. if (wire) {
  100. engineRemoveWire(wire);
  101. delete wire;
  102. wire = NULL;
  103. }
  104. }
  105. }
  106. Vec WireWidget::getOutputPos() {
  107. Vec pos;
  108. if (outputPort) {
  109. pos = Rect(outputPort->getAbsolutePos(), outputPort->box.size).getCenter();
  110. }
  111. else if (hoveredOutputPort) {
  112. pos = Rect(hoveredOutputPort->getAbsolutePos(), hoveredOutputPort->box.size).getCenter();
  113. }
  114. else {
  115. pos = gMousePos;
  116. }
  117. return pos.minus(getAbsolutePos().minus(box.pos));
  118. }
  119. Vec WireWidget::getInputPos() {
  120. Vec pos;
  121. if (inputPort) {
  122. pos = Rect(inputPort->getAbsolutePos(), inputPort->box.size).getCenter();
  123. }
  124. else if (hoveredInputPort) {
  125. pos = Rect(hoveredInputPort->getAbsolutePos(), hoveredInputPort->box.size).getCenter();
  126. }
  127. else {
  128. pos = gMousePos;
  129. }
  130. return pos.minus(getAbsolutePos().minus(box.pos));
  131. }
  132. void WireWidget::draw(NVGcontext *vg) {
  133. float opacity = dynamic_cast<RackScene*>(gScene)->toolbar->wireOpacitySlider->value / 100.0;
  134. float tension = dynamic_cast<RackScene*>(gScene)->toolbar->wireTensionSlider->value;
  135. // Draw as opaque if an "incomplete" wire
  136. if (!(inputPort && outputPort))
  137. opacity = 1.0;
  138. drawWire(vg, getOutputPos(), getInputPos(), color, tension, opacity);
  139. }
  140. void WireWidget::drawPlugs(NVGcontext *vg) {
  141. // TODO Figure out a way to draw plugs first and wires last, and cut the plug portion of the wire off.
  142. Vec outputPos = getOutputPos();
  143. Vec inputPos = getInputPos();
  144. drawPlug(vg, outputPos, color);
  145. drawPlug(vg, getInputPos(), color);
  146. // Draw plug light
  147. /*
  148. if (wire) {
  149. Output &output = wire->outputModule->outputs[wire->outputId];
  150. float value = output.value / 10.0;
  151. outputLight->box.pos = outputPos.minus(Vec(6, 6));
  152. inputLight->box.pos = inputPos.minus(Vec(6, 6));
  153. outputLight->setValue(value);
  154. inputLight->setValue(value);
  155. }
  156. else {
  157. outputLight->setValue(0.0);
  158. inputLight->setValue(0.0);
  159. }
  160. Widget::draw(vg);
  161. */
  162. }
  163. } // namespace rack