diff --git a/include/app.hpp b/include/app.hpp index 9cea300e..ea9817d4 100644 --- a/include/app.hpp +++ b/include/app.hpp @@ -47,6 +47,8 @@ struct ModuleWidget : OpaqueWidget { virtual json_t *toJson(); virtual void fromJson(json_t *rootJ); + virtual void create(); + virtual void _delete(); /** Disconnects cables from all ports Called when the user clicks Disconnect Cables in the context menu. */ diff --git a/include/engine.hpp b/include/engine.hpp index 289b47ad..29eedfdf 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -65,11 +65,14 @@ struct Module { virtual void step() {} virtual void onSampleRateChange() {} - /** Override these to implement spacial behavior when user clicks Initialize and Randomize */ - virtual void reset() {} - virtual void randomize() {} - /** Deprecated */ - virtual void initialize() final {} + /** Called when module is created by the Add Module popup, cloning, or when loading a patch or autosave */ + virtual void onCreate() {} + /** Called when user explicitly deletes the module, not when Rack is closed or a new patch is loaded */ + virtual void onDelete() {} + /** Called when user clicks Initialize in the module context menu */ + virtual void onReset() {} + /** Called when user clicks Randomize in the module context menu */ + virtual void onRandomize() {} /** Override these to store extra internal data in the "data" property */ virtual json_t *toJson() { return NULL; } diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 7de226f6..bdd5e0d8 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -121,12 +121,24 @@ void ModuleWidget::disconnect() { } } +void ModuleWidget::create() { + if (module) { + module->onCreate(); + } +} + +void ModuleWidget::_delete() { + if (module) { + module->onDelete(); + } +} + void ModuleWidget::reset() { for (ParamWidget *param : params) { param->setValue(param->defaultValue); } if (module) { - module->reset(); + module->onReset(); } } @@ -135,7 +147,7 @@ void ModuleWidget::randomize() { param->randomize(); } if (module) { - module->randomize(); + module->onRandomize(); } } @@ -191,7 +203,7 @@ void ModuleWidget::onMouseMove(EventMouseMove &e) { gRackWidget->deleteModule(this); this->finalizeEvents(); delete this; - // Kinda sketchy because events will be passed further down the tree + e.consumed = true; return; } } diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index 2cbb7a52..a8003e93 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -306,9 +306,11 @@ void RackWidget::fromJson(json_t *rootJ) { void RackWidget::addModule(ModuleWidget *m) { moduleContainer->addChild(m); + m->create(); } void RackWidget::deleteModule(ModuleWidget *m) { + m->_delete(); moduleContainer->removeChild(m); } diff --git a/src/core/AudioInterface.cpp b/src/core/AudioInterface.cpp index 937ace0d..e120362c 100644 --- a/src/core/AudioInterface.cpp +++ b/src/core/AudioInterface.cpp @@ -128,7 +128,7 @@ struct AudioInterface : Module { openStream(); } - void reset() override { + void onReset() override { closeStream(); } }; diff --git a/src/core/MidiCCToCV.cpp b/src/core/MidiCCToCV.cpp index dcafc589..de651a1b 100644 --- a/src/core/MidiCCToCV.cpp +++ b/src/core/MidiCCToCV.cpp @@ -81,7 +81,7 @@ struct MIDICCToCVInterface : MidiIO, Module { } } - void reset() override { + void onReset() override { resetMidi(); } diff --git a/src/core/MidiToCV.cpp b/src/core/MidiToCV.cpp index 4a417ba6..783a78ce 100644 --- a/src/core/MidiToCV.cpp +++ b/src/core/MidiToCV.cpp @@ -74,7 +74,7 @@ struct MIDIToCVInterface : MidiIO, Module { baseFromJson(rootJ); } - void reset() override { + void onReset() override { resetMidi(); } diff --git a/src/core/MidiTriggerToCV.cpp b/src/core/MidiTriggerToCV.cpp index 67a4687b..376dccf3 100644 --- a/src/core/MidiTriggerToCV.cpp +++ b/src/core/MidiTriggerToCV.cpp @@ -63,7 +63,7 @@ struct MIDITriggerToCVInterface : MidiIO, Module { } } - void reset() override { + void onReset() override { resetMidi(); } }; diff --git a/src/core/QuadMidiToCV.cpp b/src/core/QuadMidiToCV.cpp index ce18b955..99009699 100644 --- a/src/core/QuadMidiToCV.cpp +++ b/src/core/QuadMidiToCV.cpp @@ -72,7 +72,7 @@ struct QuadMIDIToCVInterface : MidiIO, Module { baseFromJson(rootJ); } - void reset() override { + void onReset() override { resetMidi(); }