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