Browse Source

Serialize wire color

pull/1639/head
Andrew Belt 6 years ago
parent
commit
f41cb7e780
6 changed files with 68 additions and 30 deletions
  1. +11
    -1
      include/app.hpp
  2. +0
    -3
      include/widgets.hpp
  3. +19
    -0
      src/app.cpp
  4. +25
    -26
      src/app/RackWidget.cpp
  5. +12
    -0
      src/app/WireWidget.cpp
  6. +1
    -0
      src/widgets.cpp

+ 11
- 1
include/app.hpp View File

@@ -7,6 +7,10 @@
namespace rack {


#define CHECKMARK_STRING "✔"
#define CHECKMARK(_cond) ((_cond) ? CHECKMARK_STRING : "")


struct Model;
struct Module;
struct Wire;
@@ -90,6 +94,8 @@ struct WireWidget : OpaqueWidget {
void updateWire();
Vec getOutputPos();
Vec getInputPos();
json_t *toJson();
void fromJson(json_t *rootJ);
void draw(NVGcontext *vg) override;
void drawPlugs(NVGcontext *vg);
};
@@ -132,7 +138,7 @@ struct RackWidget : OpaqueWidget {
void fromJson(json_t *rootJ);

void addModule(ModuleWidget *m);
/** Transfers ownership to the caller so they must `delete` it if that is the intension */
/** Removes the module and transfers ownership to the caller */
void deleteModule(ModuleWidget *m);
void cloneModule(ModuleWidget *m);
/** Sets a module's box if non-colliding. Returns true if set */
@@ -420,4 +426,8 @@ extern Toolbar *gToolbar;
void sceneInit();
void sceneDestroy();

json_t *colorToJson(NVGcolor color);
NVGcolor jsonToColor(json_t *colorJ);


} // namespace rack

+ 0
- 3
include/widgets.hpp View File

@@ -25,9 +25,6 @@ inline Vec mm2px(Vec millimeters) {
return millimeters.mult(SVG_DPI / 25.4);
}

#define CHECKMARK_STRING "✔"
#define CHECKMARK(_cond) ((_cond) ? CHECKMARK_STRING : "")


////////////////////
// resources


+ 19
- 0
src/app.cpp View File

@@ -24,4 +24,23 @@ void sceneDestroy() {
}


json_t *colorToJson(NVGcolor color) {
json_t *colorJ = json_object();
json_object_set_new(colorJ, "r", json_real(color.r));
json_object_set_new(colorJ, "g", json_real(color.g));
json_object_set_new(colorJ, "b", json_real(color.b));
json_object_set_new(colorJ, "a", json_real(color.a));
return colorJ;
}

NVGcolor jsonToColor(json_t *colorJ) {
NVGcolor color;
color.r = json_number_value(json_object_get(colorJ, "r"));
color.g = json_number_value(json_object_get(colorJ, "g"));
color.b = json_number_value(json_object_get(colorJ, "b"));
color.a = json_number_value(json_object_get(colorJ, "a"));
return color;
}


} // namespace rack

+ 25
- 26
src/app/RackWidget.cpp View File

@@ -172,26 +172,26 @@ json_t *RackWidget::toJson() {
if (!(wireWidget->outputPort && wireWidget->inputPort))
continue;
// wire
json_t *wire = json_object();
{
// Get the modules at each end of the wire
ModuleWidget *outputModuleWidget = wireWidget->outputPort->getAncestorOfType<ModuleWidget>();
assert(outputModuleWidget);
int outputModuleId = moduleIds[outputModuleWidget];
ModuleWidget *inputModuleWidget = wireWidget->inputPort->getAncestorOfType<ModuleWidget>();
assert(inputModuleWidget);
int inputModuleId = moduleIds[inputModuleWidget];
// Get output/input ports
int outputId = wireWidget->outputPort->portId;
int inputId = wireWidget->inputPort->portId;
json_object_set_new(wire, "outputModuleId", json_integer(outputModuleId));
json_object_set_new(wire, "outputId", json_integer(outputId));
json_object_set_new(wire, "inputModuleId", json_integer(inputModuleId));
json_object_set_new(wire, "inputId", json_integer(inputId));
}
json_t *wire = wireWidget->toJson();
// Get the modules at each end of the wire
ModuleWidget *outputModuleWidget = wireWidget->outputPort->getAncestorOfType<ModuleWidget>();
assert(outputModuleWidget);
int outputModuleId = moduleIds[outputModuleWidget];
ModuleWidget *inputModuleWidget = wireWidget->inputPort->getAncestorOfType<ModuleWidget>();
assert(inputModuleWidget);
int inputModuleId = moduleIds[inputModuleWidget];
// Get output/input ports
int outputId = wireWidget->outputPort->portId;
int inputId = wireWidget->inputPort->portId;
json_object_set_new(wire, "outputModuleId", json_integer(outputModuleId));
json_object_set_new(wire, "outputId", json_integer(outputId));
json_object_set_new(wire, "inputModuleId", json_integer(inputModuleId));
json_object_set_new(wire, "inputId", json_integer(inputId));
json_array_append_new(wires, wire);
}
json_object_set_new(rootJ, "wires", wires);
@@ -281,12 +281,10 @@ void RackWidget::fromJson(json_t *rootJ) {
size_t wireId;
json_t *wireJ;
json_array_foreach(wiresJ, wireId, wireJ) {
int outputModuleId, outputId;
int inputModuleId, inputId;
int err = json_unpack(wireJ, "{s:i, s:i, s:i, s:i}",
"outputModuleId", &outputModuleId, "outputId", &outputId,
"inputModuleId", &inputModuleId, "inputId", &inputId);
if (err) continue;
int outputModuleId = json_integer_value(json_object_get(wireJ, "outputModuleId"));
int outputId = json_integer_value(json_object_get(wireJ, "outputId"));
int inputModuleId = json_integer_value(json_object_get(wireJ, "inputModuleId"));
int inputId = json_integer_value(json_object_get(wireJ, "inputId"));

// Get module widgets
ModuleWidget *outputModuleWidget = moduleWidgets[outputModuleId];
@@ -320,6 +318,7 @@ void RackWidget::fromJson(json_t *rootJ) {

// Create WireWidget
WireWidget *wireWidget = new WireWidget();
wireWidget->fromJson(wireJ);
wireWidget->outputPort = outputPort;
wireWidget->inputPort = inputPort;
wireWidget->updateWire();


+ 12
- 0
src/app/WireWidget.cpp View File

@@ -142,6 +142,18 @@ Vec WireWidget::getInputPos() {
}
}

json_t *WireWidget::toJson() {
json_t *rootJ = json_object();
json_object_set_new(rootJ, "color", colorToJson(color));
return rootJ;
}

void WireWidget::fromJson(json_t *rootJ) {
json_t *colorJ = json_object_get(rootJ, "color");
if (colorJ)
color = jsonToColor(colorJ);
}

void WireWidget::draw(NVGcontext *vg) {
float opacity = gToolbar->wireOpacitySlider->value / 100.0;
float tension = gToolbar->wireTensionSlider->value;


+ 1
- 0
src/widgets.cpp View File

@@ -9,4 +9,5 @@ Widget *gFocusedWidget = NULL;

Scene *gScene = NULL;


} // namespace rack

Loading…
Cancel
Save