1 package net.mograsim.logic.ui.serializing;
3 import java.io.IOException;
5 import net.mograsim.logic.ui.model.ViewModelModifiable;
6 import net.mograsim.logic.ui.model.components.GUIComponent;
7 import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
8 import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
9 import net.mograsim.logic.ui.model.wires.GUIWire;
10 import net.mograsim.logic.ui.model.wires.MovablePin;
11 import net.mograsim.logic.ui.serializing.SubmodelComponentParams.InterfacePinParams;
12 import net.mograsim.logic.ui.serializing.SubmodelComponentParams.SubmodelParameters;
13 import net.mograsim.logic.ui.serializing.SubmodelComponentParams.SubmodelParameters.InnerComponentParams;
14 import net.mograsim.logic.ui.serializing.SubmodelComponentParams.SubmodelParameters.InnerWireParams;
17 * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams}
19 public final class SubmodelComponentDeserializer
22 * Creates a {@link SubmodelComponent} from the {@link SubmodelComponentParams}, specified at the given path. The returned
23 * SubmodelComponent can also be e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the
24 * {@link SubmodelComponentParams} describe.
26 * @param path The path of the file describing the {@link SubmodelComponentParams}, which define the new {@link SubmodelComponent}
27 * @return A new SubmodelComponent, as described in the file located at the given path
29 public static SubmodelComponent create(ViewModelModifiable model, String path)
33 SubmodelComponentParams params = SubmodelComponentParams.readJson(path);
34 SubmodelComponent ret = create(model, params);
39 System.err.println("Failed to construct GUICustomComponent. Parameters were not found.");
42 return new SimpleRectangularSubmodelComponent(model, 0, "ERROR");
46 * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams}. The returned SubmodelComponent can also be
47 * e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the {@link SubmodelComponentParams} describe.
49 * @param params The parameters describing the {@link SubmodelComponent}
51 * @return A new SubmodelComponent, as described by the {@link SubmodelComponentParams}
53 public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params)
55 DeserializedSubmodelComponent comp = createSubmodelComponent(model, params);
56 initSubmodel(comp, params.submodel);
60 private static DeserializedSubmodelComponent createSubmodelComponent(ViewModelModifiable model, SubmodelComponentParams params)
62 DeserializedSubmodelComponent comp = new DeserializedSubmodelComponent(model);
63 comp.setSubmodelScale(params.submodel.innerScale);
64 comp.setOutlineRenderer(
65 CodeSnippetSupplier.createOutlineRenderer(params.outlineRendererSnippetID, comp, params.outlineRendererParams));
66 comp.setSymbolRenderer(CodeSnippetSupplier.createSymbolRenderer(params.symbolRendererSnippetID, comp, params.symbolRendererParams));
67 // TODO high level states
68 comp.setSize(params.width, params.height);
69 for (InterfacePinParams iPinParams : params.interfacePins)
70 comp.addSubmodelInterface(
71 new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y));
75 @SuppressWarnings("unused")
76 private static void initSubmodel(DeserializedSubmodelComponent comp, SubmodelParameters params)
78 GUIComponent[] components = new GUIComponent[params.subComps.length];
79 for (int i = 0; i < components.length; i++)
81 InnerComponentParams cParams = params.subComps[i];
82 String path = cParams.id;
83 components[i] = IndirectGUIComponentCreator.createComponent(comp.getSubmodelModifiable(), cParams.id, cParams.params);
84 components[i].moveTo(cParams.pos.x, cParams.pos.y);
87 for (int i = 0; i < params.innerWires.length; i++)
89 InnerWireParams innerWire = params.innerWires[i];
90 new GUIWire(comp.getSubmodelModifiable(),
91 comp.getSubmodelModifiable().getComponents().get(innerWire.pin1.compId).getPin(innerWire.pin1.pinName),
92 comp.getSubmodelModifiable().getComponents().get(innerWire.pin2.compId).getPin(innerWire.pin2.pinName), innerWire.path);