9596a6934cd2b2de2be3ff199dcfcca12f834e7b
[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.SubmodelComponentParams;
13 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
14 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
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::refJsonFromJsonTest);
22         }
23
24         private static class TestComponent extends SimpleRectangularSubmodelComponent
25         {
26                 protected TestComponent(ViewModelModifiable model)
27                 {
28                         super(model, 1, "Test");
29                         setSubmodelScale(.4);
30                         setInputPins("Input pin #0");
31                         GUICustomComponentCreator.create(submodelModifiable, "HalfAdder.json");
32                 }
33         }
34
35         // Execute only after HalfAdder.json has been created
36         public static void refJsonFromJsonTest(ViewModelModifiable model)
37         {
38                 TestComponent t = new TestComponent(model);
39                 t.calculateParams().writeJson("Test.json");
40                 SubmodelComponent c = GUICustomComponentCreator.create(model, "Test.json");
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                 SubmodelComponentParams p = tmp.calculateParams();
50                 try
51                 {
52                         p.writeJson("HalfAdder.json");
53                         p = SubmodelComponentParams.readJson("HalfAdder.json");
54                 }
55                 catch (IOException e)
56                 {
57                         e.printStackTrace();
58                 }
59
60                 GUICustomComponentCreator.create(model, p, "");
61         }
62
63         @SuppressWarnings("unused") // for GUIWires being created
64         public static void createFromJsonExample(ViewModelModifiable model)
65         {
66                 SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model);
67                 SubmodelComponentParams pC = tmp.calculateParams();
68                 tmp.moveTo(1000, 100);
69                 try
70                 {
71                         pC.writeJson("FullAdder.json");
72                         pC = SubmodelComponentParams.readJson("FullAdder.json");
73                 }
74                 catch (IOException e)
75                 {
76                         e.printStackTrace();
77                 }
78
79                 SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) GUICustomComponentCreator.create(model,
80                                 "FullAdder.json");
81
82                 GUIManualSwitch swA = new GUIManualSwitch(model);
83                 swA.moveTo(0, 0);
84                 GUIManualSwitch swB = new GUIManualSwitch(model);
85                 swB.moveTo(0, 25);
86                 GUIManualSwitch swC = new GUIManualSwitch(model);
87                 swC.moveTo(0, 50);
88
89                 adder.moveTo(30, 10);
90                 GUIBitDisplay bdY = new GUIBitDisplay(model);
91                 bdY.moveTo(90, 12.5);
92                 GUIBitDisplay bdZ = new GUIBitDisplay(model);
93                 bdZ.moveTo(90, 30);
94
95                 new GUIWire(model, swA.getOutputPin(), adder.getPin("A"));
96                 new GUIWire(model, swB.getOutputPin(), adder.getPin("B"));
97                 new GUIWire(model, swC.getOutputPin(), adder.getPin("C"));
98
99                 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
100                 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
101
102                 SubmodelComponent adder2 = GUICustomComponentCreator.create(model, pC, "");
103
104                 swA = new GUIManualSwitch(model);
105                 swA.moveTo(0, 70);
106                 swB = new GUIManualSwitch(model);
107                 swB.moveTo(0, 85);
108                 swC = new GUIManualSwitch(model);
109                 swC.moveTo(0, 100);
110
111                 adder2.moveTo(30, 80);
112                 bdY = new GUIBitDisplay(model);
113                 bdY.moveTo(90, 70);
114                 bdZ = new GUIBitDisplay(model);
115                 bdZ.moveTo(90, 85);
116
117                 new GUIWire(model, swA.getOutputPin(), adder.getPin("A"));
118                 new GUIWire(model, swB.getOutputPin(), adder.getPin("B"));
119                 new GUIWire(model, swC.getOutputPin(), adder.getPin("C"));
120
121                 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
122                 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
123         }
124 }