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.

153 lines
3.4KB

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