Browse Source

Add menu item to port menu to set color of cables.

tags/v2.1.1
Andrew Belt 2 years ago
parent
commit
950d73444e
3 changed files with 80 additions and 13 deletions
  1. +13
    -0
      include/history.hpp
  2. +45
    -13
      src/app/PortWidget.cpp
  3. +22
    -0
      src/history.cpp

+ 13
- 0
include/history.hpp View File

@@ -157,6 +157,19 @@ struct CableRemove : InverseAction<CableAdd> {
};


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;


+ 45
- 13
src/app/PortWidget.cpp View File

@@ -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<CableColorItem>(" 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<PortCableItem>(" " + 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<PortCableItem>(" " + portInfo->module->model->name + ": " + portInfo->getName(), RIGHT_ARROW);
item->color = cw->color;
item->pw = this;
item->cw = cw;
menu->addChild(item);
}
}
}



+ 22
- 0
src/history.cpp View File

@@ -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();
}


Loading…
Cancel
Save