Browse Source

Rearrange ModuleWidget methods.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
ac29571db3
2 changed files with 107 additions and 99 deletions
  1. +20
    -12
      include/app/ModuleWidget.hpp
  2. +87
    -87
      src/app/ModuleWidget.cpp

+ 20
- 12
include/app/ModuleWidget.hpp View File

@@ -27,14 +27,12 @@ struct ModuleWidget : widget::OpaqueWidget {
} }
~ModuleWidget(); ~ModuleWidget();


void draw(const DrawArgs& args) override;
void drawShadow(const DrawArgs& args);

void onButton(const ButtonEvent& e) override;
void onHoverKey(const HoverKeyEvent& e) override;
void onDragStart(const DragStartEvent& e) override;
void onDragEnd(const DragEndEvent& e) override;
void onDragMove(const DragMoveEvent& e) override;
plugin::Model* getModel() {
return model;
}
engine::Module* getModule() {
return module;
}


/** Associates this ModuleWidget with the Module. /** Associates this ModuleWidget with the Module.
Transfers ownership. Transfers ownership.
@@ -59,6 +57,20 @@ struct ModuleWidget : widget::OpaqueWidget {
PortWidget* getInput(int portId); PortWidget* getInput(int portId);
PortWidget* getOutput(int portId); PortWidget* getOutput(int portId);


void draw(const DrawArgs& args) override;
void drawShadow(const DrawArgs& args);

/** Override to add context menu entries to your subclass.
It is recommended to add a blank ui::MenuEntry first for spacing.
*/
virtual void appendContextMenu(ui::Menu* menu) {}

void onButton(const ButtonEvent& e) override;
void onHoverKey(const HoverKeyEvent& e) override;
void onDragStart(const DragStartEvent& e) override;
void onDragEnd(const DragEndEvent& e) override;
void onDragMove(const DragMoveEvent& e) override;

json_t* toJson(); json_t* toJson();
void fromJson(json_t* rootJ); void fromJson(json_t* rootJ);
void copyClipboard(); void copyClipboard();
@@ -94,10 +106,6 @@ struct ModuleWidget : widget::OpaqueWidget {
/** Deletes `this` */ /** Deletes `this` */
void removeAction(); void removeAction();
void createContextMenu(); void createContextMenu();
/** Override to add context menu entries to your subclass.
It is recommended to add a blank ui::MenuEntry first for spacing.
*/
virtual void appendContextMenu(ui::Menu* menu) {}


INTERNAL math::Vec& dragOffset(); INTERNAL math::Vec& dragOffset();
INTERNAL bool& dragEnabled(); INTERNAL bool& dragEnabled();


+ 87
- 87
src/app/ModuleWidget.cpp View File

@@ -407,6 +407,93 @@ ModuleWidget::~ModuleWidget() {
delete internal; delete internal;
} }


void ModuleWidget::setModule(engine::Module* module) {
if (this->module) {
APP->engine->removeModule(this->module);
delete this->module;
this->module = NULL;
}
this->module = module;
}

void ModuleWidget::setPanel(widget::Widget* panel) {
// Remove existing panel
if (internal->panel) {
removeChild(internal->panel);
delete internal->panel;
internal->panel = NULL;
}

if (panel) {
addChildBottom(panel);
internal->panel = panel;
box.size.x = std::round(panel->box.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH;
}
}

void ModuleWidget::setPanel(std::shared_ptr<Svg> svg) {
// Create SvgPanel
SvgPanel* panel = new SvgPanel;
panel->setBackground(svg);
setPanel(panel);
}

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

void ModuleWidget::addInput(PortWidget* input) {
// Check that the port is an input
assert(input->type == engine::Port::INPUT);
// Check that the port doesn't have a duplicate ID
PortWidget* input2 = getInput(input->portId);
assert(!input2);
// Add port
addChild(input);
}

void ModuleWidget::addOutput(PortWidget* output) {
// Check that the port is an output
assert(output->type == engine::Port::OUTPUT);
// Check that the port doesn't have a duplicate ID
PortWidget* output2 = getOutput(output->portId);
assert(!output2);
// Add port
addChild(output);
}

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;
}

ParamWidget* ModuleWidget::getParam(int paramId) {
return getFirstDescendantOfTypeWithCondition<ParamWidget>(this, [&](ParamWidget* pw) -> bool {
return pw->paramId == paramId;
});
}

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

void ModuleWidget::draw(const DrawArgs& args) { void ModuleWidget::draw(const DrawArgs& args) {
nvgScissor(args.vg, RECT_ARGS(args.clipBox)); nvgScissor(args.vg, RECT_ARGS(args.clipBox));


@@ -614,93 +701,6 @@ void ModuleWidget::onDragMove(const DragMoveEvent& e) {
} }
} }


void ModuleWidget::setModule(engine::Module* module) {
if (this->module) {
APP->engine->removeModule(this->module);
delete this->module;
this->module = NULL;
}
this->module = module;
}

void ModuleWidget::setPanel(widget::Widget* panel) {
// Remove existing panel
if (internal->panel) {
removeChild(internal->panel);
delete internal->panel;
internal->panel = NULL;
}

if (panel) {
addChildBottom(panel);
internal->panel = panel;
box.size.x = std::round(panel->box.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH;
}
}

void ModuleWidget::setPanel(std::shared_ptr<Svg> svg) {
// Create SvgPanel
SvgPanel* panel = new SvgPanel;
panel->setBackground(svg);
setPanel(panel);
}

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

void ModuleWidget::addInput(PortWidget* input) {
// Check that the port is an input
assert(input->type == engine::Port::INPUT);
// Check that the port doesn't have a duplicate ID
PortWidget* input2 = getInput(input->portId);
assert(!input2);
// Add port
addChild(input);
}

void ModuleWidget::addOutput(PortWidget* output) {
// Check that the port is an output
assert(output->type == engine::Port::OUTPUT);
// Check that the port doesn't have a duplicate ID
PortWidget* output2 = getOutput(output->portId);
assert(!output2);
// Add port
addChild(output);
}

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;
}

ParamWidget* ModuleWidget::getParam(int paramId) {
return getFirstDescendantOfTypeWithCondition<ParamWidget>(this, [&](ParamWidget* pw) -> bool {
return pw->paramId == paramId;
});
}

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() {
json_t* moduleJ = APP->engine->moduleToJson(module); json_t* moduleJ = APP->engine->moduleToJson(module);
return moduleJ; return moduleJ;


Loading…
Cancel
Save