diff --git a/include/app/ModuleWidget.hpp b/include/app/ModuleWidget.hpp index eafb10fb..2a6f9fb8 100644 --- a/include/app/ModuleWidget.hpp +++ b/include/app/ModuleWidget.hpp @@ -36,6 +36,7 @@ struct ModuleWidget : OpaqueWidget { virtual json_t *toJson(); virtual void fromJson(json_t *rootJ); + void copyClipboard(); void pasteClipboard(); void save(std::string filename); diff --git a/include/engine.hpp b/include/engine.hpp index d4f33adb..b0bbd7df 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -115,9 +115,12 @@ struct Module { randomize(); } + json_t *toJson(); + void fromJson(json_t *rootJ); + /** Override these to store extra internal data in the "data" property of the module's JSON object */ - virtual json_t *toJson() { return NULL; } - virtual void fromJson(json_t *root) {} + virtual json_t *dataToJson() { return NULL; } + virtual void dataFromJson(json_t *root) {} /** Deprecated */ virtual void reset() {} diff --git a/src/Core/AudioInterface.cpp b/src/Core/AudioInterface.cpp index 6639d31c..fde51b70 100644 --- a/src/Core/AudioInterface.cpp +++ b/src/Core/AudioInterface.cpp @@ -125,13 +125,13 @@ struct AudioInterface : Module { void step() override; - json_t *toJson() override { + json_t *dataToJson() override { json_t *rootJ = json_object(); json_object_set_new(rootJ, "audio", audioIO.toJson()); return rootJ; } - void fromJson(json_t *rootJ) override { + void dataFromJson(json_t *rootJ) override { json_t *audioJ = json_object_get(rootJ, "audio"); audioIO.fromJson(audioJ); } diff --git a/src/Core/MIDICCToCVInterface.cpp b/src/Core/MIDICCToCVInterface.cpp index d649faab..9603519d 100644 --- a/src/Core/MIDICCToCVInterface.cpp +++ b/src/Core/MIDICCToCVInterface.cpp @@ -74,7 +74,7 @@ struct MIDICCToCVInterface : Module { } } - json_t *toJson() override { + json_t *dataToJson() override { json_t *rootJ = json_object(); json_t *ccsJ = json_array(); @@ -88,7 +88,7 @@ struct MIDICCToCVInterface : Module { return rootJ; } - void fromJson(json_t *rootJ) override { + void dataFromJson(json_t *rootJ) override { json_t *ccsJ = json_object_get(rootJ, "ccs"); if (ccsJ) { for (int i = 0; i < 16; i++) { diff --git a/src/Core/MIDIToCVInterface.cpp b/src/Core/MIDIToCVInterface.cpp index 66e80b70..3a6b7d50 100644 --- a/src/Core/MIDIToCVInterface.cpp +++ b/src/Core/MIDIToCVInterface.cpp @@ -61,7 +61,7 @@ struct MIDIToCVInterface : Module { onReset(); } - json_t *toJson() override { + json_t *dataToJson() override { json_t *rootJ = json_object(); json_t *divisionsJ = json_array(); @@ -75,7 +75,7 @@ struct MIDIToCVInterface : Module { return rootJ; } - void fromJson(json_t *rootJ) override { + void dataFromJson(json_t *rootJ) override { json_t *divisionsJ = json_object_get(rootJ, "divisions"); if (divisionsJ) { for (int i = 0; i < 2; i++) { diff --git a/src/Core/MIDITriggerToCVInterface.cpp b/src/Core/MIDITriggerToCVInterface.cpp index 80d3e29f..04fc8b6f 100644 --- a/src/Core/MIDITriggerToCVInterface.cpp +++ b/src/Core/MIDITriggerToCVInterface.cpp @@ -107,7 +107,7 @@ struct MIDITriggerToCVInterface : Module { } } - json_t *toJson() override { + json_t *dataToJson() override { json_t *rootJ = json_object(); json_t *notesJ = json_array(); @@ -122,7 +122,7 @@ struct MIDITriggerToCVInterface : Module { return rootJ; } - void fromJson(json_t *rootJ) override { + void dataFromJson(json_t *rootJ) override { json_t *notesJ = json_object_get(rootJ, "notes"); if (notesJ) { for (int i = 0; i < 16; i++) { diff --git a/src/Core/QuadMIDIToCVInterface.cpp b/src/Core/QuadMIDIToCVInterface.cpp index fdf3d983..5e7c1395 100644 --- a/src/Core/QuadMIDIToCVInterface.cpp +++ b/src/Core/QuadMIDIToCVInterface.cpp @@ -55,14 +55,14 @@ struct QuadMIDIToCVInterface : Module { onReset(); } - json_t *toJson() override { + json_t *dataToJson() override { json_t *rootJ = json_object(); json_object_set_new(rootJ, "midi", midiInput.toJson()); json_object_set_new(rootJ, "polyMode", json_integer(polyMode)); return rootJ; } - void fromJson(json_t *rootJ) override { + void dataFromJson(json_t *rootJ) override { json_t *midiJ = json_object_get(rootJ, "midi"); if (midiJ) midiInput.fromJson(midiJ); diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 2cf8e073..611a6ca4 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -66,23 +66,14 @@ json_t *ModuleWidget::toJson() { json_object_set_new(rootJ, "version", json_string(model->plugin->version.c_str())); // model json_object_set_new(rootJ, "model", json_string(model->slug.c_str())); - // params - if (module) { - json_t *paramsJ = json_array(); - for (Param ¶m : module->params) { - json_t *paramJ = param.toJson(); - json_array_append_new(paramsJ, paramJ); - } - json_object_set_new(rootJ, "params", paramsJ); - } - // data + + // Other properties if (module) { - json_t *dataJ = module->toJson(); - if (dataJ) { - json_object_set_new(rootJ, "data", dataJ); - } + json_t *moduleJ = module->toJson(); + // Merge with rootJ + json_object_update(rootJ, moduleJ); + json_decref(moduleJ); } - return rootJ; } @@ -117,27 +108,8 @@ void ModuleWidget::fromJson(json_t *rootJ) { } } - // params - json_t *paramsJ = json_object_get(rootJ, "params"); - size_t i; - json_t *paramJ; - json_array_foreach(paramsJ, i, paramJ) { - uint32_t paramId = i; - // Get paramId - json_t *paramIdJ = json_object_get(paramJ, "paramId"); - if (paramIdJ) { - // Legacy v0.6.0 to params.size()) { - module->params[paramId].fromJson(paramJ); - } - } - - // data - json_t *dataJ = json_object_get(rootJ, "data"); - if (dataJ && module) { - module->fromJson(dataJ); + if (module) { + module->fromJson(rootJ); } } diff --git a/src/engine.cpp b/src/engine.cpp index a8b9377e..9ab5ddb8 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -105,6 +105,53 @@ void Light::setBrightnessSmooth(float brightness, float frames) { } +json_t *Module::toJson() { + json_t *rootJ = json_object(); + + // params + json_t *paramsJ = json_array(); + for (Param ¶m : params) { + json_t *paramJ = param.toJson(); + json_array_append_new(paramsJ, paramJ); + } + json_object_set_new(rootJ, "params", paramsJ); + + // data + json_t *dataJ = dataToJson(); + if (dataJ) { + json_object_set_new(rootJ, "data", dataJ); + } + + return rootJ; +} + +void Module::fromJson(json_t *rootJ) { + // params + json_t *paramsJ = json_object_get(rootJ, "params"); + size_t i; + json_t *paramJ; + json_array_foreach(paramsJ, i, paramJ) { + uint32_t paramId = i; + // Get paramId + json_t *paramIdJ = json_object_get(paramJ, "paramId"); + if (paramIdJ) { + // Legacy v0.6.0 to outputs[outputId].value; inputModule->inputs[inputId].value = value;