@@ -7,6 +7,10 @@ | |||||
namespace rack { | namespace rack { | ||||
#define CHECKMARK_STRING "✔" | |||||
#define CHECKMARK(_cond) ((_cond) ? CHECKMARK_STRING : "") | |||||
struct Model; | struct Model; | ||||
struct Module; | struct Module; | ||||
struct Wire; | struct Wire; | ||||
@@ -90,6 +94,8 @@ struct WireWidget : OpaqueWidget { | |||||
void updateWire(); | void updateWire(); | ||||
Vec getOutputPos(); | Vec getOutputPos(); | ||||
Vec getInputPos(); | Vec getInputPos(); | ||||
json_t *toJson(); | |||||
void fromJson(json_t *rootJ); | |||||
void draw(NVGcontext *vg) override; | void draw(NVGcontext *vg) override; | ||||
void drawPlugs(NVGcontext *vg); | void drawPlugs(NVGcontext *vg); | ||||
}; | }; | ||||
@@ -132,7 +138,7 @@ struct RackWidget : OpaqueWidget { | |||||
void fromJson(json_t *rootJ); | void fromJson(json_t *rootJ); | ||||
void addModule(ModuleWidget *m); | 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 deleteModule(ModuleWidget *m); | ||||
void cloneModule(ModuleWidget *m); | void cloneModule(ModuleWidget *m); | ||||
/** Sets a module's box if non-colliding. Returns true if set */ | /** Sets a module's box if non-colliding. Returns true if set */ | ||||
@@ -420,4 +426,8 @@ extern Toolbar *gToolbar; | |||||
void sceneInit(); | void sceneInit(); | ||||
void sceneDestroy(); | void sceneDestroy(); | ||||
json_t *colorToJson(NVGcolor color); | |||||
NVGcolor jsonToColor(json_t *colorJ); | |||||
} // namespace rack | } // namespace rack |
@@ -25,9 +25,6 @@ inline Vec mm2px(Vec millimeters) { | |||||
return millimeters.mult(SVG_DPI / 25.4); | return millimeters.mult(SVG_DPI / 25.4); | ||||
} | } | ||||
#define CHECKMARK_STRING "✔" | |||||
#define CHECKMARK(_cond) ((_cond) ? CHECKMARK_STRING : "") | |||||
//////////////////// | //////////////////// | ||||
// resources | // resources | ||||
@@ -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 | } // namespace rack |
@@ -172,26 +172,26 @@ json_t *RackWidget::toJson() { | |||||
if (!(wireWidget->outputPort && wireWidget->inputPort)) | if (!(wireWidget->outputPort && wireWidget->inputPort)) | ||||
continue; | continue; | ||||
// wire | // 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_array_append_new(wires, wire); | ||||
} | } | ||||
json_object_set_new(rootJ, "wires", wires); | json_object_set_new(rootJ, "wires", wires); | ||||
@@ -281,12 +281,10 @@ void RackWidget::fromJson(json_t *rootJ) { | |||||
size_t wireId; | size_t wireId; | ||||
json_t *wireJ; | json_t *wireJ; | ||||
json_array_foreach(wiresJ, wireId, 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 | // Get module widgets | ||||
ModuleWidget *outputModuleWidget = moduleWidgets[outputModuleId]; | ModuleWidget *outputModuleWidget = moduleWidgets[outputModuleId]; | ||||
@@ -320,6 +318,7 @@ void RackWidget::fromJson(json_t *rootJ) { | |||||
// Create WireWidget | // Create WireWidget | ||||
WireWidget *wireWidget = new WireWidget(); | WireWidget *wireWidget = new WireWidget(); | ||||
wireWidget->fromJson(wireJ); | |||||
wireWidget->outputPort = outputPort; | wireWidget->outputPort = outputPort; | ||||
wireWidget->inputPort = inputPort; | wireWidget->inputPort = inputPort; | ||||
wireWidget->updateWire(); | wireWidget->updateWire(); | ||||
@@ -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) { | void WireWidget::draw(NVGcontext *vg) { | ||||
float opacity = gToolbar->wireOpacitySlider->value / 100.0; | float opacity = gToolbar->wireOpacitySlider->value / 100.0; | ||||
float tension = gToolbar->wireTensionSlider->value; | float tension = gToolbar->wireTensionSlider->value; | ||||
@@ -9,4 +9,5 @@ Widget *gFocusedWidget = NULL; | |||||
Scene *gScene = NULL; | Scene *gScene = NULL; | ||||
} // namespace rack | } // namespace rack |