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.

165 lines
3.8KB

  1. #include "scene.hpp"
  2. #include "engine.hpp"
  3. namespace rack {
  4. static void drawPlug(NVGcontext *vg, Vec pos, NVGcolor color) {
  5. NVGcolor colorOutline = nvgLerpRGBA(color, nvgRGBf(0.0, 0.0, 0.0), 0.5);
  6. // Plug solid
  7. nvgBeginPath(vg);
  8. nvgCircle(vg, pos.x, pos.y, 9.5);
  9. nvgFillColor(vg, color);
  10. nvgFill(vg);
  11. // Border
  12. nvgStrokeWidth(vg, 1.0);
  13. nvgStrokeColor(vg, colorOutline);
  14. nvgStroke(vg);
  15. // Hole
  16. nvgBeginPath(vg);
  17. nvgCircle(vg, pos.x, pos.y, 5.5);
  18. nvgFillColor(vg, nvgRGBf(0.0, 0.0, 0.0));
  19. nvgFill(vg);
  20. }
  21. static void drawWire(NVGcontext *vg, Vec pos1, Vec pos2, NVGcolor color, float tension, float opacity) {
  22. NVGcolor colorShadow = nvgRGBAf(0, 0, 0, 0.08);
  23. NVGcolor colorOutline = nvgLerpRGBA(color, nvgRGBf(0.0, 0.0, 0.0), 0.5);
  24. // Wire
  25. if (opacity > 0.0) {
  26. nvgSave(vg);
  27. nvgGlobalAlpha(vg, opacity);
  28. float dist = pos1.minus(pos2).norm();
  29. Vec slump;
  30. slump.y = (1.0 - tension) * (150.0 + 1.0*dist);
  31. Vec pos3 = pos1.plus(pos2).div(2).plus(slump);
  32. nvgLineJoin(vg, NVG_ROUND);
  33. // Shadow
  34. Vec pos4 = pos3.plus(slump.mult(0.08));
  35. nvgBeginPath(vg);
  36. nvgMoveTo(vg, pos1.x, pos1.y);
  37. nvgQuadTo(vg, pos4.x, pos4.y, pos2.x, pos2.y);
  38. nvgStrokeColor(vg, colorShadow);
  39. nvgStrokeWidth(vg, 5);
  40. nvgStroke(vg);
  41. // Wire outline
  42. nvgBeginPath(vg);
  43. nvgMoveTo(vg, pos1.x, pos1.y);
  44. nvgQuadTo(vg, pos3.x, pos3.y, pos2.x, pos2.y);
  45. nvgStrokeColor(vg, colorOutline);
  46. nvgStrokeWidth(vg, 5);
  47. nvgStroke(vg);
  48. // Wire solid
  49. nvgStrokeColor(vg, color);
  50. nvgStrokeWidth(vg, 3);
  51. nvgStroke(vg);
  52. nvgRestore(vg);
  53. }
  54. }
  55. static NVGcolor wireColors[8] = {
  56. nvgRGB(0xc9, 0xb7, 0x0e), // yellow
  57. nvgRGB(0xc9, 0x18, 0x47), // red
  58. nvgRGB(0x0c, 0x8e, 0x15), // green
  59. nvgRGB(0x09, 0x86, 0xad), // blue
  60. nvgRGB(0x44, 0x44, 0x44), // black
  61. nvgRGB(0x66, 0x66, 0x66), // gray
  62. nvgRGB(0x88, 0x88, 0x88), // light gray
  63. nvgRGB(0xaa, 0xaa, 0xaa), // white
  64. };
  65. static int lastWireColorId = -1;
  66. WireWidget::WireWidget() {
  67. int wireColorId;
  68. do {
  69. wireColorId = randomu32() % 8;
  70. } while (wireColorId == lastWireColorId);
  71. lastWireColorId = wireColorId;
  72. color = wireColors[wireColorId];
  73. }
  74. WireWidget::~WireWidget() {
  75. if (outputPort) {
  76. outputPort->connectedWire = NULL;
  77. outputPort = NULL;
  78. }
  79. if (inputPort) {
  80. inputPort->connectedWire = NULL;
  81. inputPort = NULL;
  82. }
  83. updateWire();
  84. }
  85. void WireWidget::updateWire() {
  86. if (wire) {
  87. engineRemoveWire(wire);
  88. delete wire;
  89. wire = NULL;
  90. }
  91. if (inputPort && outputPort) {
  92. wire = new Wire();
  93. wire->outputModule = outputPort->module;
  94. wire->outputId = outputPort->outputId;
  95. wire->inputModule = inputPort->module;
  96. wire->inputId = inputPort->inputId;
  97. engineAddWire(wire);
  98. }
  99. }
  100. void WireWidget::draw(NVGcontext *vg) {
  101. Vec absolutePos = getAbsolutePos().minus(box.pos);
  102. float opacity = dynamic_cast<RackScene*>(gScene)->toolbar->wireOpacitySlider->value / 100.0;
  103. float tension = dynamic_cast<RackScene*>(gScene)->toolbar->wireTensionSlider->value;
  104. // Display the actively dragged wire as opaque
  105. if (gRackWidget->activeWire == this)
  106. opacity = 1.0;
  107. // Compute location of outputPos and inputPos
  108. Vec outputPos;
  109. if (outputPort) {
  110. outputPos = Rect(outputPort->getAbsolutePos(), outputPort->box.size).getCenter();
  111. }
  112. else if (hoveredOutputPort) {
  113. outputPos = Rect(hoveredOutputPort->getAbsolutePos(), hoveredOutputPort->box.size).getCenter();
  114. }
  115. else {
  116. outputPos = gMousePos;
  117. }
  118. Vec inputPos;
  119. if (inputPort) {
  120. inputPos = Rect(inputPort->getAbsolutePos(), inputPort->box.size).getCenter();
  121. }
  122. else if (hoveredInputPort) {
  123. inputPos = Rect(hoveredInputPort->getAbsolutePos(), hoveredInputPort->box.size).getCenter();
  124. }
  125. else {
  126. inputPos = gMousePos;
  127. }
  128. outputPos = outputPos.minus(absolutePos);
  129. inputPos = inputPos.minus(absolutePos);
  130. drawWire(vg, outputPos, inputPos, color, tension, opacity);
  131. drawPlug(vg, outputPos, color);
  132. drawPlug(vg, inputPos, color);
  133. }
  134. } // namespace rack