import net.mograsim.logic.ui.SimpleLogicUIStandalone;
import net.mograsim.logic.ui.model.ViewModelModifiable;
import net.mograsim.logic.ui.model.components.GUIBitDisplay;
-import net.mograsim.logic.ui.model.components.GUIComponent;
import net.mograsim.logic.ui.model.components.GUICustomComponentCreator;
import net.mograsim.logic.ui.model.components.GUIManualSwitch;
import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
import net.mograsim.logic.ui.model.components.SubmodelComponent;
-import net.mograsim.logic.ui.model.components.mi.nandbased.GUIand;
import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
-import net.mograsim.logic.ui.model.components.mi.nandbased.GUImux1;
-import net.mograsim.logic.ui.model.components.mi.nandbased.GUImux1_4;
-import net.mograsim.logic.ui.model.components.mi.nandbased.GUIsel2_4;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams;
import net.mograsim.logic.ui.model.components.params.SubComponentParams;
import net.mograsim.logic.ui.model.wires.GUIWire;
{
public static void main(String[] args)
{
- SimpleLogicUIStandalone.executeVisualisation(JsonExample::createFromJsonExample);
+ SimpleLogicUIStandalone.executeVisualisation(JsonExample::createHalfAdderExample);
}
private static class TestComponent extends SimpleRectangularSubmodelComponent
{
GUIhalfadder tmp = new GUIhalfadder(model);
tmp.moveTo(1000, 50);
- RectComponentParams p = tmp.calculateRectParams();
+ SubComponentParams p = tmp.calculateParams();
SubComponentParams pC = tmp.calculateParams();
try
{
p.writeJson("HalfAdder.rc");
pC.writeJson("HalfAdder.sc");
- p = RectComponentParams.readJson("HalfAdder.rc");
+ p = SubComponentParams.readJson("HalfAdder.rc");
pC = SubComponentParams.readJson("HalfAdder.sc");
}
catch (IOException e)
{
SimpleRectangularSubmodelComponent tmp = new GUIhalfadder(model);
tmp.moveTo(1000, 50);
- RectComponentParams p = tmp.calculateRectParams();
+ SubComponentParams p = tmp.calculateParams();
try
{
p.writeJson("HalfAdder.rc");
- p = RectComponentParams.readJson("HalfAdder.rc");
+ p = SubComponentParams.readJson("HalfAdder.rc");
}
catch (IOException e)
{
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
import net.mograsim.logic.ui.model.ViewModelModifiable;
import net.mograsim.logic.ui.model.components.params.GeneralComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams;
+import net.mograsim.logic.ui.model.components.params.GeneralComponentParams.InnerComponentParams;
import net.mograsim.logic.ui.model.components.params.SubComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerWireParams;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerWireParams;
import net.mograsim.logic.ui.model.components.params.SubComponentParams.InterfacePinParams;
import net.mograsim.logic.ui.model.wires.GUIWire;
public class GUICustomComponentCreator
{
+ private static final String rectC = SimpleRectangularSubmodelComponent.class.getSimpleName();
+
private static class CustomRectComponent extends SimpleRectangularSubmodelComponent
{
private String path;
{
try
{
- if (path.endsWith(RectComponentParams.fileExtension))
- {
- RectComponentParams params = RectComponentParams.readJson(path);
- SubmodelComponent ret = create(model, params, path);
- return ret;
- } else if (path.endsWith(SubComponentParams.fileExtension))
- {
- SubComponentParams params = SubComponentParams.readJson(path);
- SubmodelComponent ret = create(model, params, path);
- return ret;
- } else
- throw new IOException(String.format("\"%s\" does not have a valid file extension. Must be either %s or %s", path,
- RectComponentParams.fileExtension, SubComponentParams.fileExtension));
+ SubComponentParams params = SubComponentParams.readJson(path);
+ SubmodelComponent ret = create(model, params, path);
+ return ret;
}
catch (IOException e)
{
return new CustomRectComponent(model, 0, "ERROR", "NONE");
}
- /**
- * @param path This value is used when the new SubmodelComponent is an inner component to a different SubmodelComponent, which is being
- * saved to a file; Then, the new SubmodelComponent is referenced by its given path within the file.
- */
- public static SimpleRectangularSubmodelComponent create(ViewModelModifiable model, RectComponentParams params, String path)
- {
- CustomRectComponent comp = new CustomRectComponent(model, params.logicWidth, params.displayName, path);
- comp.setSubmodelScale(params.composition.innerScale);
- comp.setInputCount(params.inputCount);
- comp.setOutputCount(params.outputCount);
- initSubmodelComponents(comp, params.composition);
- return comp;
- }
-
/**
* @param path This value is used when the new SubmodelComponent is an inner component to a different SubmodelComponent, which is being
* saved to a file; Then, the new SubmodelComponent is referenced by its given path within the file.
*/
public static SubmodelComponent create(ViewModelModifiable model, SubComponentParams params, String path)
{
- // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents
- CustomRectComponent comp = new CustomRectComponent(model, 0, "", path);
- comp.setSubmodelScale(params.composition.innerScale);
- comp.setSize(params.width, params.height);
- for (InterfacePinParams iPinParams : params.interfacePins)
+ // TODO: Clean up this mess
+ SubmodelComponent comp = null;
+ if (rectC.equals(params.type))
+ {
+ try
+ {
+ Map<String, Object> m = params.specialized;
+ SimpleRectangularSubmodelComponent rect = new CustomRectComponent(model,
+ ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(),
+ (String) m.get(SimpleRectangularSubmodelComponent.kLabel), path);
+ rect.setSubmodelScale(params.composition.innerScale);
+ rect.setSize(params.width, params.height);
+ rect.setInputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kInCount)).intValue());
+ rect.setOutputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kOutCount)).intValue());
+ comp = rect;
+ }
+ catch (ClassCastException | NullPointerException e)
+ {
+ System.err.println("Failed to specialize component!");
+ e.printStackTrace();
+ }
+ }
+ if (comp == null)
{
- comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y);
+ // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents
+ comp = new CustomRectComponent(model, 0, "", path);
+
+ comp.setSubmodelScale(params.composition.innerScale);
+ comp.setSize(params.width, params.height);
+ for (InterfacePinParams iPinParams : params.interfacePins)
+ {
+ comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y);
+ }
}
initSubmodelComponents(comp, params.composition);
return comp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams;
import net.mograsim.logic.ui.model.wires.Pin;
public class SimpleRectangularSubmodelComponent extends SubmodelComponent
{
+ public static String kLabel = "label", kInCount = "input_count", kOutCount = "output_count", kLogicWidth = "logic_width";
+
private static final double width = 35;
private static final double pinDistance = 10;
private static final double fontHeight = 5;
gc.drawRectangle(getBounds());
}
- public RectComponentParams calculateRectParams()
+ @Override
+ public SubComponentParams calculateParams()
{
- RectComponentParams params = new RectComponentParams();
- params.displayName = label;
- params.inputCount = inputSupermodelPins.size();
- params.outputCount = outputSubmodelPins.size();
- params.logicWidth = logicWidth;
- params.composition = calculateCompositionParams();
- return params;
+ SubComponentParams ret = super.calculateParams();
+ ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
+ Map<String, Object> m = new TreeMap<>();
+ m.put(kLabel, label);
+ m.put(kInCount, inputSupermodelPins.size());
+ m.put(kOutCount, outputSupermodelPins.size());
+ m.put(kLogicWidth, logicWidth);
+ ret.specialized = m;
+ return ret;
}
}
\ No newline at end of file
import net.mograsim.logic.ui.model.ViewModel;
import net.mograsim.logic.ui.model.ViewModelModifiable;
import net.mograsim.logic.ui.model.components.params.GeneralComponentParams;
+import net.mograsim.logic.ui.model.components.params.GeneralComponentParams.InnerComponentParams;
import net.mograsim.logic.ui.model.components.params.SubComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerPinParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerWireParams;
import net.mograsim.logic.ui.model.components.params.SubComponentParams.InterfacePinParams;
import net.mograsim.logic.ui.model.wires.GUIWire;
import net.mograsim.logic.ui.model.wires.Pin;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerPinParams;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerWireParams;
public abstract class SubmodelComponent extends GUIComponent
{
public SubComponentParams calculateParams()
{
SubComponentParams params = new SubComponentParams();
+ params.type = SubmodelComponent.class.getSimpleName();
params.composition = calculateCompositionParams();
Rectangle bounds = getBounds();
package net.mograsim.logic.ui.model.components.params;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerWireParams;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerWireParams;
public class GeneralComponentParams
{
public double innerScale;
public InnerComponentParams[] subComps;
public InnerWireParams[] innerWires;
+
+ public static class InnerComponentParams
+ {
+ public Point pos;
+ public String type;
+ public int logicWidth;
+ }
}
+++ /dev/null
-package net.mograsim.logic.ui.model.components.params;
-
-import java.io.IOException;
-
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
-
-public class RectComponentParams
-{
- public final static String fileExtension = ".rc";
- public String displayName;
- public int inputCount, outputCount, logicWidth;
- public GeneralComponentParams composition;
-
- public static class InnerComponentParams
- {
- public Point pos;
- public String type;
- public int logicWidth;
- }
-
- public static class InnerWireParams
- {
- public InnerPinParams pin1, pin2;
- public Point[] path;
- }
-
- public static class InnerPinParams
- {
- public int compId, pinIndex;
- }
-
- public static RectComponentParams readJson(String path) throws IOException
- {
- return JsonHandler.readJson(path, RectComponentParams.class);
- }
-
- /**
- * Writes this {@link RectComponentParams} object into a file in json format. The correct file extension is important! Check
- * {@link RectComponentParams}.fileExtension
- */
- public void writeJson(String path)
- {
- try
- {
- JsonHandler.writeJson(this, path);
- }
- catch (IOException e)
- {
- System.err.println("Failed to write RectComponentParams to file");
- e.printStackTrace();
- }
- }
-}
package net.mograsim.logic.ui.model.components.params;
import java.io.IOException;
+import java.util.Map;
import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
public class SubComponentParams
{
+ public String type;
public final static String fileExtension = ".sc";
public double width, height;
public InterfacePinParams[] interfacePins;
public GeneralComponentParams composition;
+ public Map<String, Object> specialized;
public static class InterfacePinParams
{
public int logicWidth;
}
+ public static class InnerWireParams
+ {
+ public InnerPinParams pin1, pin2;
+ public Point[] path;
+ }
+
+ public static class InnerPinParams
+ {
+ public int compId, pinIndex;
+ }
+
public static SubComponentParams readJson(String path) throws IOException
{
return JsonHandler.readJson(path, SubComponentParams.class);