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.

121 lines
2.7KB

  1. #include "Rack.hpp"
  2. namespace rack {
  3. void drawWire(NVGcontext *vg, Vec pos1, Vec pos2, float tension, NVGcolor color) {
  4. float dist = pos1.minus(pos2).norm();
  5. Vec slump;
  6. slump.y = (1.0 - tension) * (150.0 + 1.0*dist);
  7. Vec pos3 = pos1.plus(pos2).div(2).plus(slump);
  8. nvgLineJoin(vg, NVG_ROUND);
  9. // Shadow
  10. Vec pos4 = pos3.plus(slump.mult(0.08));
  11. NVGcolor colorShadow = nvgRGBAf(0, 0, 0, 0.08);
  12. nvgBeginPath(vg);
  13. nvgMoveTo(vg, pos1.x, pos1.y);
  14. nvgQuadTo(vg, pos4.x, pos4.y, pos2.x, pos2.y);
  15. nvgStrokeColor(vg, colorShadow);
  16. nvgStrokeWidth(vg, 5);
  17. nvgStroke(vg);
  18. // Wire outline
  19. NVGcolor colorOutline = nvgRGBf(0, 0, 0);
  20. nvgBeginPath(vg);
  21. nvgMoveTo(vg, pos1.x, pos1.y);
  22. nvgQuadTo(vg, pos3.x, pos3.y, pos2.x, pos2.y);
  23. nvgStrokeColor(vg, colorOutline);
  24. nvgStrokeWidth(vg, 4);
  25. nvgStroke(vg);
  26. // Wire solid
  27. nvgStrokeColor(vg, color);
  28. nvgStrokeWidth(vg, 2);
  29. nvgStroke(vg);
  30. }
  31. static NVGcolor wireColors[8] = {
  32. nvgRGB(0x50, 0x50, 0x50),
  33. nvgRGB(0xac, 0x41, 0x42),
  34. nvgRGB(0x90, 0xa9, 0x59),
  35. nvgRGB(0xf4, 0xbf, 0x75),
  36. nvgRGB(0x6a, 0x9f, 0xb5),
  37. nvgRGB(0xaa, 0x75, 0x9f),
  38. nvgRGB(0x75, 0xb5, 0xaa),
  39. nvgRGB(0xf5, 0xf5, 0xf5),
  40. };
  41. static int wireColorId = 1;
  42. WireWidget::WireWidget() {
  43. wireColorId = (wireColorId + 1) % 8;
  44. color = wireColors[wireColorId];
  45. }
  46. WireWidget::~WireWidget() {
  47. if (outputPort) {
  48. outputPort->connectedWire = NULL;
  49. outputPort = NULL;
  50. }
  51. if (inputPort) {
  52. inputPort->connectedWire = NULL;
  53. inputPort = NULL;
  54. }
  55. updateWire();
  56. }
  57. void WireWidget::updateWire() {
  58. if (wire) {
  59. rackDisconnectWire(wire);
  60. delete wire;
  61. wire = NULL;
  62. }
  63. if (inputPort && outputPort) {
  64. wire = new Wire();
  65. wire->outputModule = outputPort->module;
  66. wire->outputId = outputPort->outputId;
  67. wire->inputModule = inputPort->module;
  68. wire->inputId = inputPort->inputId;
  69. rackConnectWire(wire);
  70. }
  71. }
  72. void WireWidget::draw(NVGcontext *vg) {
  73. Vec outputPos, inputPos;
  74. Vec absolutePos = getAbsolutePos();
  75. if (outputPort) {
  76. outputPos = Rect(outputPort->getAbsolutePos(), outputPort->box.size).getCenter();
  77. }
  78. else {
  79. outputPos = gMousePos;
  80. }
  81. if (inputPort) {
  82. inputPos = Rect(inputPort->getAbsolutePos(), inputPort->box.size).getCenter();
  83. }
  84. else {
  85. inputPos = gMousePos;
  86. }
  87. outputPos = outputPos.minus(absolutePos);
  88. inputPos = inputPos.minus(absolutePos);
  89. bndNodePort(vg, outputPos.x, outputPos.y, BND_DEFAULT, color);
  90. bndNodePort(vg, inputPos.x, inputPos.y, BND_DEFAULT, color);
  91. nvgSave(vg);
  92. float wireOpacity = gScene->toolbar->wireOpacitySlider->value / 100.0;
  93. if (wireOpacity > 0.0) {
  94. nvgGlobalAlpha(vg, wireOpacity);
  95. float tension = gScene->toolbar->wireTensionSlider->value;
  96. drawWire(vg, outputPos, inputPos, tension, color);
  97. }
  98. nvgRestore(vg);
  99. }
  100. } // namespace rack