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(CodeSnippetSupplier.outlineRendererSupplier.getSnippetSupplier(params.outlineRendererSnippetID).create(comp,
65 params.outlineRendererParams));
66 comp.setSymbolRenderer(CodeSnippetSupplier.symbolRendererSupplier.getSnippetSupplier(params.symbolRendererSnippetID).create(comp,
67 params.symbolRendererParams));
68 // TODO high level states
69 comp.setSize(params.width, params.height);
70 for (InterfacePinParams iPinParams : params.interfacePins)
71 comp.addSubmodelInterface(
72 new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y));
76 @SuppressWarnings("unused")
77 private static void initSubmodel(DeserializedSubmodelComponent comp, SubmodelParameters params)
79 GUIComponent[] components = new GUIComponent[params.subComps.length];
80 for (int i = 0; i < components.length; i++)
82 InnerComponentParams cParams = params.subComps[i];
83 String path = cParams.id;
84 components[i] = IndirectGUIComponentCreator.createComponent(comp.getSubmodelModifiable(), cParams.id, cParams.params);
85 components[i].moveTo(cParams.pos.x, cParams.pos.y);
88 for (int i = 0; i < params.innerWires.length; i++)
90 InnerWireParams innerWire = params.innerWires[i];
91 new GUIWire(comp.getSubmodelModifiable(),
92 comp.getSubmodelModifiable().getComponents().get(innerWire.pin1.compId).getPin(innerWire.pin1.pinName),
93 comp.getSubmodelModifiable().getComponents().get(innerWire.pin2.compId).getPin(innerWire.pin2.pinName), innerWire.path);