Browse Source

Remove ModuleWidget::params, inputs, and outputs arrays. Instead, search recursively for those widgets.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
73c2ecaea2
2 changed files with 58 additions and 58 deletions
  1. +7
    -9
      include/app/ModuleWidget.hpp
  2. +51
    -49
      src/app/ModuleWidget.cpp

+ 7
- 9
include/app/ModuleWidget.hpp View File

@@ -21,12 +21,6 @@ struct ModuleWidget : widget::OpaqueWidget {
/** Owned. */ /** Owned. */
engine::Module* module = NULL; engine::Module* module = NULL;


/** Note that the indexes of these vectors do not necessarily correspond with the indexes of `Module::params` etc.
*/
std::vector<ParamWidget*> params;
std::vector<PortWidget*> inputs;
std::vector<PortWidget*> outputs;

ModuleWidget(); ModuleWidget();
DEPRECATED ModuleWidget(engine::Module* module) : ModuleWidget() { DEPRECATED ModuleWidget(engine::Module* module) : ModuleWidget() {
setModule(module); setModule(module);
@@ -53,13 +47,17 @@ struct ModuleWidget : widget::OpaqueWidget {
/** Use `setPanel(createPanel(svg))` instead. */ /** Use `setPanel(createPanel(svg))` instead. */
void setPanel(std::shared_ptr<Svg> svg); void setPanel(std::shared_ptr<Svg> svg);


/** Convenience functions for adding special widgets (calls addChild()) */
/** Convenience functions for adding special widgets.
Just calls addChild() with additional checking.
It is not required to call this method. You may instead use addChild() in a child widget for example.
*/
void addParam(ParamWidget* param); void addParam(ParamWidget* param);
void addInput(PortWidget* input); void addInput(PortWidget* input);
void addOutput(PortWidget* output); void addOutput(PortWidget* output);
/** Scans children widgets recursively for a ParamWidget with the given paramId. */
ParamWidget* getParam(int paramId); ParamWidget* getParam(int paramId);
PortWidget* getInput(int inputId);
PortWidget* getOutput(int outputId);
PortWidget* getInput(int portId);
PortWidget* getOutput(int portId);


/** Serializes/unserializes the module state */ /** Serializes/unserializes the module state */
json_t* toJson(); json_t* toJson();


+ 51
- 49
src/app/ModuleWidget.cpp View File

@@ -516,7 +516,6 @@ void ModuleWidget::setPanel(std::shared_ptr<Svg> svg) {
} }


void ModuleWidget::addParam(ParamWidget* param) { void ModuleWidget::addParam(ParamWidget* param) {
params.push_back(param);
addChild(param); addChild(param);
} }


@@ -524,11 +523,9 @@ void ModuleWidget::addInput(PortWidget* input) {
// Check that the port is an input // Check that the port is an input
assert(input->type == engine::Port::INPUT); assert(input->type == engine::Port::INPUT);
// Check that the port doesn't have a duplicate ID // Check that the port doesn't have a duplicate ID
for (PortWidget* input2 : inputs) {
assert(input->portId != input2->portId);
}
PortWidget* input2 = getInput(input->portId);
assert(!input2);
// Add port // Add port
inputs.push_back(input);
addChild(input); addChild(input);
} }


@@ -536,36 +533,42 @@ void ModuleWidget::addOutput(PortWidget* output) {
// Check that the port is an output // Check that the port is an output
assert(output->type == engine::Port::OUTPUT); assert(output->type == engine::Port::OUTPUT);
// Check that the port doesn't have a duplicate ID // Check that the port doesn't have a duplicate ID
for (PortWidget* output2 : outputs) {
assert(output->portId != output2->portId);
}
PortWidget* output2 = getOutput(output->portId);
assert(!output2);
// Add port // Add port
outputs.push_back(output);
addChild(output); addChild(output);
} }


ParamWidget* ModuleWidget::getParam(int paramId) {
for (ParamWidget* param : params) {
if (param->paramId == paramId)
return param;
template <class T, typename F>
T* getFirstDescendantOfTypeWithCondition(widget::Widget* w, F f) {
T* t = dynamic_cast<T*>(w);
if (t && f(t))
return t;

for (widget::Widget* child : w->children) {
T* foundT = getFirstDescendantOfTypeWithCondition<T>(child, f);
if (foundT)
return foundT;
} }
return NULL; return NULL;
} }


PortWidget* ModuleWidget::getInput(int inputId) {
for (PortWidget* port : inputs) {
if (port->portId == inputId)
return port;
}
return NULL;
ParamWidget* ModuleWidget::getParam(int paramId) {
return getFirstDescendantOfTypeWithCondition<ParamWidget>(this, [&](ParamWidget* pw) -> bool {
return pw->paramId == paramId;
});
} }


PortWidget* ModuleWidget::getOutput(int outputId) {
for (PortWidget* port : outputs) {
if (port->portId == outputId)
return port;
}
return NULL;
PortWidget* ModuleWidget::getInput(int portId) {
return getFirstDescendantOfTypeWithCondition<PortWidget>(this, [&](PortWidget* pw) -> bool {
return pw->type == engine::Port::INPUT && pw->portId == portId;
});
}

PortWidget* ModuleWidget::getOutput(int portId) {
return getFirstDescendantOfTypeWithCondition<PortWidget>(this, [&](PortWidget* pw) -> bool {
return pw->type == engine::Port::OUTPUT && pw->portId == portId;
});
} }


json_t* ModuleWidget::toJson() { json_t* ModuleWidget::toJson() {
@@ -759,15 +762,23 @@ void ModuleWidget::saveDialog() {
save(pathStr); save(pathStr);
} }


void ModuleWidget::disconnect() {
for (PortWidget* input : inputs) {
APP->scene->rack->clearCablesOnPort(input);
}
for (PortWidget* output : outputs) {
APP->scene->rack->clearCablesOnPort(output);
template <class T, typename F>
void doOfType(widget::Widget* w, F f) {
T* t = dynamic_cast<T*>(w);
if (t)
f(t);

for (widget::Widget* child : w->children) {
doOfType<T>(child, f);
} }
} }


void ModuleWidget::disconnect() {
doOfType<PortWidget>(this, [&](PortWidget* pw) {
APP->scene->rack->clearCablesOnPort(pw);
});
}

void ModuleWidget::resetAction() { void ModuleWidget::resetAction() {
assert(module); assert(module);


@@ -799,31 +810,20 @@ void ModuleWidget::randomizeAction() {
} }


static void disconnectActions(ModuleWidget* mw, history::ComplexAction* complexAction) { static void disconnectActions(ModuleWidget* mw, history::ComplexAction* complexAction) {
// Add CableRemove action for all cables attached to inputs
for (PortWidget* input : mw->inputs) {
for (CableWidget* cw : APP->scene->rack->getCablesOnPort(input)) {
// Add CableRemove action for all cables
doOfType<PortWidget>(mw, [&](PortWidget* pw) {
for (CableWidget* cw : APP->scene->rack->getCablesOnPort(pw)) {
if (!cw->isComplete()) if (!cw->isComplete())
continue; continue;
// Avoid creating duplicate actions for self-patched cables // Avoid creating duplicate actions for self-patched cables
if (cw->outputPort->module == mw->module)
if (pw->type == engine::Port::INPUT && cw->outputPort->module == mw->module)
continue; continue;
// history::CableRemove // history::CableRemove
history::CableRemove* h = new history::CableRemove; history::CableRemove* h = new history::CableRemove;
h->setCable(cw); h->setCable(cw);
complexAction->push(h); complexAction->push(h);
} }
}
// Add CableRemove action for all cables attached to outputs
for (PortWidget* output : mw->outputs) {
for (CableWidget* cw : APP->scene->rack->getCablesOnPort(output)) {
if (!cw->isComplete())
continue;
// history::CableRemove
history::CableRemove* h = new history::CableRemove;
h->setCable(cw);
complexAction->push(h);
}
}
});
} }


void ModuleWidget::disconnectAction() { void ModuleWidget::disconnectAction() {
@@ -860,7 +860,9 @@ void ModuleWidget::cloneAction() {
h->push(hma); h->push(hma);


// Clone cables attached to input ports // Clone cables attached to input ports
for (PortWidget* pw : inputs) {
doOfType<PortWidget>(this, [&](PortWidget* pw) {
if (pw->type != engine::Port::INPUT)
return;
std::list<CableWidget*> cables = APP->scene->rack->getCablesOnPort(pw); std::list<CableWidget*> cables = APP->scene->rack->getCablesOnPort(pw);
for (CableWidget* cw : cables) { for (CableWidget* cw : cables) {
// Create cable attached to cloned ModuleWidget's input // Create cable attached to cloned ModuleWidget's input
@@ -886,7 +888,7 @@ void ModuleWidget::cloneAction() {
hca->setCable(clonedCw); hca->setCable(clonedCw);
h->push(hca); h->push(hca);
} }
}
});


APP->history->push(h); APP->history->push(h);
} }


Loading…
Cancel
Save