1 package net.mograsim.logic.model.serializing;
3 import java.io.IOException;
6 import net.mograsim.logic.model.model.ViewModelModifiable;
7 import net.mograsim.logic.model.model.components.GUIComponent;
8 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
9 import net.mograsim.logic.model.model.wires.GUIWire;
10 import net.mograsim.logic.model.model.wires.MovablePin;
11 import net.mograsim.logic.model.serializing.SubmodelComponentParams.InterfacePinParams;
12 import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters;
13 import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerComponentParams;
14 import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerWireParams;
17 * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams}
19 public final class SubmodelComponentDeserializer
22 * Like {@link #create(ViewModelModifiable, String, String)}, but using the default name.
24 public static SubmodelComponent create(ViewModelModifiable model, String path)
26 return create(model, path, null);
30 * Creates a {@link SubmodelComponent} from the {@link SubmodelComponentParams} located at the given path as a JSON file. The returned
31 * SubmodelComponent is a {@link DeserializedSubmodelComponent}.
33 * @param path The path of the file describing the {@link SubmodelComponentParams}, which define the new {@link SubmodelComponent}
34 * @return A new SubmodelComponent, as described in the file located at the given path
36 public static SubmodelComponent create(ViewModelModifiable model, String path, String name)
40 SubmodelComponentParams params = SubmodelComponentParams.readJson(path);
41 SubmodelComponent ret = create(model, params, name);
46 throw new RuntimeException("Failed to construct GUICustomComponent. Parameters were not found.", e);
51 * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams}. The returned SubmodelComponent is a
52 * {@link DeserializedSubmodelComponent}.
54 * @param params The parameters describing the {@link SubmodelComponent}
56 * @return A new SubmodelComponent, as described by the {@link SubmodelComponentParams}
58 public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params, String name)
60 DeserializedSubmodelComponent comp = createSubmodelComponent(model, params, name);
61 initSubmodel(comp, params.submodel);
65 private static DeserializedSubmodelComponent createSubmodelComponent(ViewModelModifiable model, SubmodelComponentParams params,
68 DeserializedSubmodelComponent comp = new DeserializedSubmodelComponent(model, name);
69 comp.setSubmodelScale(params.submodel.innerScale);
70 comp.setOutlineRenderer(CodeSnippetSupplier.outlineRendererSupplier.getSnippetSupplier(params.outlineRendererSnippetID).create(comp,
71 params.outlineRendererParams));
72 comp.setSymbolRenderer(CodeSnippetSupplier.symbolRendererSupplier.getSnippetSupplier(params.symbolRendererSnippetID).create(comp,
73 params.symbolRendererParams));
74 // TODO high level states
75 comp.setSize(params.width, params.height);
76 for (InterfacePinParams iPinParams : params.interfacePins)
77 comp.addSubmodelInterface(
78 new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y));
82 @SuppressWarnings("unused") // GUIWire being created
83 private static void initSubmodel(DeserializedSubmodelComponent comp, SubmodelParameters params)
85 ViewModelModifiable submodelModifiable = comp.getSubmodelModifiable();
86 Map<String, GUIComponent> componentsByName = submodelModifiable.getComponentsByName();
87 GUIComponent[] components = new GUIComponent[params.subComps.length];
88 for (int i = 0; i < components.length; i++)
90 InnerComponentParams cParams = params.subComps[i];
91 components[i] = IndirectGUIComponentCreator.createComponent(submodelModifiable, cParams.id, cParams.params, cParams.name);
92 components[i].moveTo(cParams.pos.x, cParams.pos.y);
95 for (int i = 0; i < params.innerWires.length; i++)
97 InnerWireParams innerWire = params.innerWires[i];
98 new GUIWire(submodelModifiable, componentsByName.get(innerWire.pin1.compName).getPin(innerWire.pin1.pinName),
99 componentsByName.get(innerWire.pin2.compName).getPin(innerWire.pin2.pinName), innerWire.path);