From 2cd29d62f065e8af52c4a205e01577d299a532b3 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 29 Apr 2019 18:10:53 -0400 Subject: [PATCH] Draw plugs under wires for CableWidgets in CableContainer. --- include/app/RackWidget.hpp | 1 + include/engine/Port.hpp | 9 ++------- include/math.hpp | 3 +++ src/app/CableWidget.cpp | 12 ++++++++---- src/app/RackWidget.cpp | 4 ++-- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/include/app/RackWidget.hpp b/include/app/RackWidget.hpp index 8743c69f..7bbac58a 100644 --- a/include/app/RackWidget.hpp +++ b/include/app/RackWidget.hpp @@ -24,6 +24,7 @@ struct RackWidget : widget::OpaqueWidget { math::Vec mousePos; ParamWidget *touchedParam = NULL; std::map moduleDragPositions; + int nextCableColorId = 0; RackWidget(); ~RackWidget(); diff --git a/include/engine/Port.hpp b/include/engine/Port.hpp index 72a73bbb..ae32689a 100644 --- a/include/engine/Port.hpp +++ b/include/engine/Port.hpp @@ -22,7 +22,7 @@ struct alignas(32) Port { Unstable API. Use set/getChannels() instead. May be 0 to PORT_MAX_CHANNELS. */ - uint8_t channels = 0; + uint8_t channels = 1; /** Unstable API. Use isConnected() instead. */ bool active = false; /** For rendering plug lights on cables. @@ -98,12 +98,7 @@ struct alignas(32) Port { }; -struct Output : Port { - Output() { - channels = 1; - } -}; - +struct Output : Port {}; struct Input : Port {}; diff --git a/include/math.hpp b/include/math.hpp index fee748f9..1e368209 100644 --- a/include/math.hpp +++ b/include/math.hpp @@ -205,6 +205,9 @@ struct Vec { float norm() const { return std::hypot(x, y); } + Vec normalize() const { + return div(norm()); + } float square() const { return x * x + y * y; } diff --git a/src/app/CableWidget.cpp b/src/app/CableWidget.cpp index 30dc788c..e20694c9 100644 --- a/src/app/CableWidget.cpp +++ b/src/app/CableWidget.cpp @@ -1,5 +1,6 @@ #include "app/CableWidget.hpp" #include "app/Scene.hpp" +#include "app/RackWidget.hpp" #include "window.hpp" #include "app.hpp" #include "patch.hpp" @@ -46,6 +47,10 @@ static void drawCable(NVGcontext *vg, math::Vec pos1, math::Vec pos2, NVGcolor c slump.y = (1.0 - tension) * (150.0 + 1.0*dist); math::Vec pos3 = pos1.plus(pos2).div(2).plus(slump); + // Adjust pos1 and pos2 to not draw over the plug + pos1 = pos1.plus(pos3.minus(pos1).normalize().mult(9)); + pos2 = pos2.plus(pos3.minus(pos2).normalize().mult(9)); + nvgLineJoin(vg, NVG_ROUND); // Shadow @@ -81,12 +86,12 @@ static const NVGcolor cableColors[] = { nvgRGB(0x0c, 0x8e, 0x15), // green nvgRGB(0x09, 0x86, 0xad), // blue }; -static int lastCableColorId = -1; CableWidget::CableWidget() { - lastCableColorId = (lastCableColorId + 1) % LENGTHOF(cableColors); - color = cableColors[lastCableColorId]; + int id = APP->scene->rack->nextCableColorId++; + APP->scene->rack->nextCableColorId %= LENGTHOF(cableColors); + color = cableColors[id]; cable = new engine::Cable; } @@ -244,7 +249,6 @@ void CableWidget::draw(const DrawArgs &args) { } void CableWidget::drawPlugs(const DrawArgs &args) { - // TODO Figure out a way to draw plugs first and cables last, and cut the plug portion of the cable off. math::Vec outputPos = getOutputPos(); math::Vec inputPos = getInputPos(); diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index fe96f3f2..05405bce 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -63,14 +63,14 @@ struct ModuleContainer : widget::Widget { struct CableContainer : widget::TransparentWidget { void draw(const DrawArgs &args) override { - Widget::draw(args); - // Draw cable plugs for (widget::Widget *w : children) { CableWidget *cw = dynamic_cast(w); assert(cw); cw->drawPlugs(args); } + + Widget::draw(args); } };