Simplified file representation of SubmodelComponents
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / examples / JsonExample.java
1 package net.mograsim.logic.ui.examples;
2
3 import java.io.IOException;
4
5 import net.mograsim.logic.ui.SimpleLogicUIStandalone;
6 import net.mograsim.logic.ui.model.ViewModelModifiable;
7 import net.mograsim.logic.ui.model.components.GUIBitDisplay;
8 import net.mograsim.logic.ui.model.components.GUICustomComponentCreator;
9 import net.mograsim.logic.ui.model.components.GUIManualSwitch;
10 import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
11 import net.mograsim.logic.ui.model.components.SubmodelComponent;
12 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
13 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
14 import net.mograsim.logic.ui.model.components.params.SubComponentParams;
15 import net.mograsim.logic.ui.model.wires.GUIWire;
16
17 public class JsonExample
18 {
19         public static void main(String[] args)
20         {
21                 SimpleLogicUIStandalone.executeVisualisation(JsonExample::createHalfAdderExample);
22         }
23
24         private static class TestComponent extends SimpleRectangularSubmodelComponent
25         {
26                 protected TestComponent(ViewModelModifiable model)
27                 {
28                         super(model, 1, "Test");
29                         setInputCount(1);
30                         setSubmodelScale(.4);
31                         GUICustomComponentCreator.create(submodelModifiable, "HalfAdder.rc");
32                 }
33         }
34
35         // Execute only after HalfAdder.rc has been created
36         public static void refJsonFromJsonTest(ViewModelModifiable model)
37         {
38                 TestComponent t = new TestComponent(model);
39                 t.calculateParams().writeJson("Test.sc");
40                 SubmodelComponent c = GUICustomComponentCreator.create(model, "Test.sc");
41                 c.moveTo(0, 50);
42
43         }
44
45         public static void createHalfAdderExample(ViewModelModifiable model)
46         {
47                 GUIhalfadder tmp = new GUIhalfadder(model);
48                 tmp.moveTo(1000, 50);
49                 SubComponentParams p = tmp.calculateParams();
50                 SubComponentParams pC = tmp.calculateParams();
51                 try
52                 {
53                         p.writeJson("HalfAdder.rc");
54                         pC.writeJson("HalfAdder.sc");
55                         p = SubComponentParams.readJson("HalfAdder.rc");
56                         pC = SubComponentParams.readJson("HalfAdder.sc");
57                 }
58                 catch (IOException e)
59                 {
60                         e.printStackTrace();
61                 }
62
63                 SubmodelComponent adder = GUICustomComponentCreator.create(model, p, "");
64                 adder = GUICustomComponentCreator.create(model, pC, "");
65                 adder.moveTo(0, 200);
66         }
67
68         @SuppressWarnings("unused") // for GUIWires being created
69         public static void createFromJsonExample(ViewModelModifiable model)
70         {
71                 SimpleRectangularSubmodelComponent tmp = new GUIhalfadder(model);
72                 tmp.moveTo(1000, 50);
73                 SubComponentParams p = tmp.calculateParams();
74                 try
75                 {
76                         p.writeJson("HalfAdder.rc");
77                         p = SubComponentParams.readJson("HalfAdder.rc");
78                 }
79                 catch (IOException e)
80                 {
81                         e.printStackTrace();
82                 }
83                 tmp = new GUIfulladder(model);
84                 SubComponentParams pC = tmp.calculateParams();
85                 tmp.moveTo(1000, 100);
86                 try
87                 {
88                         pC.writeJson("FullAdder.sc");
89                         pC = SubComponentParams.readJson("FullAdder.sc");
90                 }
91                 catch (IOException e)
92                 {
93                         e.printStackTrace();
94                 }
95
96                 SimpleRectangularSubmodelComponent adder = new GUIfulladder(model);
97
98                 GUIManualSwitch swA = new GUIManualSwitch(model);
99                 swA.moveTo(0, 0);
100                 GUIManualSwitch swB = new GUIManualSwitch(model);
101                 swB.moveTo(0, 25);
102                 GUIManualSwitch swC = new GUIManualSwitch(model);
103                 swC.moveTo(0, 50);
104
105                 adder.moveTo(30, 10);
106                 GUIBitDisplay bdY = new GUIBitDisplay(model);
107                 bdY.moveTo(90, 12.5);
108                 GUIBitDisplay bdZ = new GUIBitDisplay(model);
109                 bdZ.moveTo(90, 30);
110
111                 new GUIWire(model, swA.getOutputPin(), adder.getInputPins().get(0));
112                 new GUIWire(model, swB.getOutputPin(), adder.getInputPins().get(1));
113                 new GUIWire(model, swC.getOutputPin(), adder.getInputPins().get(2));
114
115                 new GUIWire(model, adder.getOutputPins().get(0), bdY.getInputPin());
116                 new GUIWire(model, adder.getOutputPins().get(1), bdZ.getInputPin());
117
118                 SubmodelComponent adder2 = GUICustomComponentCreator.create(model, pC, "");
119
120                 swA = new GUIManualSwitch(model);
121                 swA.moveTo(0, 70);
122                 swB = new GUIManualSwitch(model);
123                 swB.moveTo(0, 85);
124                 swC = new GUIManualSwitch(model);
125                 swC.moveTo(0, 100);
126
127                 adder2.moveTo(30, 80);
128                 bdY = new GUIBitDisplay(model);
129                 bdY.moveTo(90, 70);
130                 bdZ = new GUIBitDisplay(model);
131                 bdZ.moveTo(90, 85);
132
133                 new GUIWire(model, swA.getOutputPin(), adder2.getPins().get(0));
134                 new GUIWire(model, swB.getOutputPin(), adder2.getPins().get(1));
135                 new GUIWire(model, swC.getOutputPin(), adder2.getPins().get(2));
136
137                 new GUIWire(model, adder2.getPins().get(3), bdY.getInputPin());
138                 new GUIWire(model, adder2.getPins().get(4), bdZ.getInputPin());
139         }
140 }