From 950d73444e4738641af83c5c4e491300e9b8f2d9 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 4 Mar 2022 13:27:32 -0500 Subject: [PATCH] Add menu item to port menu to set color of cables. --- include/history.hpp | 13 ++++++++++ src/app/PortWidget.cpp | 58 ++++++++++++++++++++++++++++++++---------- src/history.cpp | 22 ++++++++++++++++ 3 files changed, 80 insertions(+), 13 deletions(-) diff --git a/include/history.hpp b/include/history.hpp index 2763079a..a227fe3d 100644 --- a/include/history.hpp +++ b/include/history.hpp @@ -157,6 +157,19 @@ struct CableRemove : InverseAction { }; +struct CableColorChange : Action { + int64_t cableId; + NVGcolor newColor; + NVGcolor oldColor; + void setCable(app::CableWidget* cw); + void undo() override; + void redo() override; + CableColorChange() { + name = "change cable color"; + } +}; + + struct State { struct Internal; Internal* internal; diff --git a/src/app/PortWidget.cpp b/src/app/PortWidget.cpp index 468a6532..091c288a 100644 --- a/src/app/PortWidget.cpp +++ b/src/app/PortWidget.cpp @@ -118,6 +118,22 @@ struct PortCloneCableItem : ui::MenuItem { }; +struct CableColorItem : ColorMenuItem { + CableWidget* cw; + + void onAction(const ActionEvent& e) override { + // history::CableColorChange + history::CableColorChange* h = new history::CableColorChange; + h->setCable(cw); + h->newColor = color; + h->oldColor = cw->color; + APP->history->push(h); + + cw->color = color; + } +}; + + struct PortCableItem : ColorMenuItem { PortWidget* pw; CableWidget* cw; @@ -136,6 +152,21 @@ struct PortCableItem : ColorMenuItem { doAction(); } } + + ui::Menu* createChildMenu() override { + ui::Menu* menu = new ui::Menu; + + for (NVGcolor color : settings::cableColors) { + // Include extra leading spaces for the color circle + CableColorItem* item = createMenuItem(" Set color"); + item->disabled = color::isEqual(color, cw->color); + item->cw = cw; + item->color = color; + menu->addChild(item); + } + + return menu; + } }; @@ -261,19 +292,20 @@ void PortWidget::createContextMenu() { if (!cws.empty()) { menu->addChild(new ui::MenuSeparator); - } - - // Cable items - for (auto it = cws.rbegin(); it != cws.rend(); it++) { - CableWidget* cw = *it; - PortWidget* pw = (type == engine::Port::INPUT) ? cw->outputPort : cw->inputPort; - engine::PortInfo* portInfo = pw->getPortInfo(); - - PortCableItem* item = createMenuItem(" " + portInfo->module->model->name + ": " + portInfo->getName()); - item->color = cw->color; - item->pw = this; - item->cw = cw; - menu->addChild(item); + menu->addChild(createMenuLabel("Click+drag to grab cable")); + + // Cable items + for (auto it = cws.rbegin(); it != cws.rend(); it++) { + CableWidget* cw = *it; + PortWidget* pw = (type == engine::Port::INPUT) ? cw->outputPort : cw->inputPort; + engine::PortInfo* portInfo = pw->getPortInfo(); + + PortCableItem* item = createMenuItem(" " + portInfo->module->model->name + ": " + portInfo->getName(), RIGHT_ARROW); + item->color = cw->color; + item->pw = this; + item->cw = cw; + menu->addChild(item); + } } } diff --git a/src/history.cpp b/src/history.cpp index 9a2ebc02..b7455f25 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -192,6 +192,28 @@ void CableAdd::redo() { } +void CableColorChange::setCable(app::CableWidget* cw) { + assert(cw); + assert(cw->cable); + assert(cw->cable->id >= 0); + cableId = cw->cable->id; +} + +void CableColorChange::undo() { + app::CableWidget* cw = APP->scene->rack->getCable(cableId); + if (!cw) + return; + cw->color = oldColor; +} + +void CableColorChange::redo() { + app::CableWidget* cw = APP->scene->rack->getCable(cableId); + if (!cw) + return; + cw->color = newColor; +} + + State::State() { clear(); }