Browse Source

Add virtual ParamQuantity::toJson() and fromJson() so plugins can override JSON serialization behavior of params.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
cb65043538
5 changed files with 23 additions and 32 deletions
  1. +0
    -5
      include/engine/Param.hpp
  2. +5
    -0
      include/engine/ParamQuantity.hpp
  3. +4
    -4
      src/engine/Module.cpp
  4. +0
    -23
      src/engine/Param.cpp
  5. +14
    -0
      src/engine/ParamQuantity.cpp

+ 0
- 5
include/engine/Param.hpp View File

@@ -1,6 +1,4 @@
#pragma once #pragma once
#include <jansson.h>

#include <common.hpp> #include <common.hpp>
#include <math.hpp> #include <math.hpp>


@@ -20,9 +18,6 @@ struct Param {
void setValue(float value) { void setValue(float value) {
this->value = value; this->value = value;
} }

json_t* toJson();
void fromJson(json_t* rootJ);
}; };






+ 5
- 0
include/engine/ParamQuantity.hpp View File

@@ -1,6 +1,8 @@
#pragma once #pragma once
#include <vector> #include <vector>


#include <jansson.h>

#include <Quantity.hpp> #include <Quantity.hpp>
#include <engine/Param.hpp> #include <engine/Param.hpp>


@@ -80,6 +82,9 @@ struct ParamQuantity : Quantity {
void randomize() override; void randomize() override;


virtual std::string getDescription(); virtual std::string getDescription();

virtual json_t* toJson();
virtual void fromJson(json_t* rootJ);
}; };






+ 4
- 4
src/engine/Module.cpp View File

@@ -189,12 +189,12 @@ void Module::fromJson(json_t* rootJ) {


json_t* Module::paramsToJson() { json_t* Module::paramsToJson() {
json_t* rootJ = json_array(); json_t* rootJ = json_array();
for (size_t paramId = 0; paramId < params.size(); paramId++) {
for (size_t paramId = 0; paramId < paramQuantities.size(); paramId++) {
// Don't serialize unbounded Params // Don't serialize unbounded Params
if (!paramQuantities[paramId]->isBounded()) if (!paramQuantities[paramId]->isBounded())
continue; continue;


json_t* paramJ = params[paramId].toJson();
json_t* paramJ = paramQuantities[paramId]->toJson();


json_object_set_new(paramJ, "id", json_integer(paramId)); json_object_set_new(paramJ, "id", json_integer(paramId));


@@ -221,7 +221,7 @@ void Module::paramsFromJson(json_t* rootJ) {
paramId = i; paramId = i;


// Check ID bounds // Check ID bounds
if (paramId >= params.size())
if (paramId >= paramQuantities.size())
continue; continue;


// Check that the Param is bounded // Check that the Param is bounded
@@ -230,7 +230,7 @@ void Module::paramsFromJson(json_t* rootJ) {


json_t* valueJ = json_object_get(paramJ, "value"); json_t* valueJ = json_object_get(paramJ, "value");
if (valueJ) if (valueJ)
params[paramId].setValue(json_number_value(valueJ));
paramQuantities[paramId]->setValue(json_number_value(valueJ));
} }
} }




+ 0
- 23
src/engine/Param.cpp View File

@@ -1,23 +0,0 @@
#include <engine/Param.hpp>


namespace rack {
namespace engine {


json_t* Param::toJson() {
json_t* rootJ = json_object();
json_object_set_new(rootJ, "value", json_real(value));
return rootJ;
}


void Param::fromJson(json_t* rootJ) {
json_t* valueJ = json_object_get(rootJ, "value");
if (valueJ)
value = json_number_value(valueJ);
}


} // namespace engine
} // namespace rack

+ 14
- 0
src/engine/ParamQuantity.cpp View File

@@ -147,6 +147,20 @@ std::string ParamQuantity::getDescription() {
} }




json_t* ParamQuantity::toJson() {
json_t* rootJ = json_object();
json_object_set_new(rootJ, "value", json_real(getValue()));
return rootJ;
}


void ParamQuantity::fromJson(json_t* rootJ) {
json_t* valueJ = json_object_get(rootJ, "value");
if (valueJ)
setValue(json_number_value(valueJ));
}


std::string SwitchQuantity::getDisplayValueString() { std::string SwitchQuantity::getDisplayValueString() {
int index = (int) std::floor(getValue()); int index = (int) std::floor(getValue());
if (!(0 <= index && index < (int) labels.size())) if (!(0 <= index && index < (int) labels.size()))


Loading…
Cancel
Save