Implemented the RS-Latch-Example
[Mograsim.git] / LogicUI / src / era / mi / examples / gui / LogicUI.java
1 package era.mi.examples.gui;\r
2 \r
3 import java.util.HashMap;\r
4 import java.util.HashSet;\r
5 import java.util.Map;\r
6 import java.util.Set;\r
7 \r
8 import org.eclipse.swt.SWT;\r
9 import org.eclipse.swt.layout.FillLayout;\r
10 import org.eclipse.swt.widgets.Display;\r
11 import org.eclipse.swt.widgets.Event;\r
12 import org.eclipse.swt.widgets.Shell;\r
13 \r
14 import era.mi.components.gui.BasicGUIComponent;\r
15 import era.mi.components.gui.GUIManualSwitch;\r
16 import era.mi.components.gui.GUINotGate;\r
17 import era.mi.components.gui.GUIOrGate;\r
18 import era.mi.logic.Simulation;\r
19 import era.mi.logic.wires.WireArray;\r
20 import era.mi.wires.gui.GUIWire;\r
21 import era.mi.wires.gui.WireConnectionPoint;\r
22 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
23 import net.haspamelodica.swt.helper.gcs.TranslatedGC;\r
24 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
25 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;\r
26 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;\r
27 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;\r
28 \r
29 public class LogicUI\r
30 {\r
31         private static final int                                        WIRE_DELAY      = 40;\r
32         private static final int                                        OR_DELAY        = 100;\r
33         private static final int                                        NOT_DELAY       = 100;\r
34         private final Display                                           display;\r
35         private final Shell                                                     shell;\r
36         private final ZoomableCanvas                            canvas;\r
37         private final Set<BasicGUIComponent>            components;\r
38         private final Map<BasicGUIComponent, Point>     componentPositions;\r
39         private final Set<GUIWire>                                      wires;\r
40 \r
41         public LogicUI()\r
42         {\r
43                 display = new Display();\r
44                 shell = new Shell(display);\r
45                 shell.setLayout(new FillLayout());\r
46                 canvas = new ZoomableCanvas(shell, SWT.NONE);\r
47 \r
48                 components = new HashSet<>();\r
49                 componentPositions = new HashMap<>();\r
50                 wires = new HashSet<>();\r
51                 initComponents();\r
52 \r
53                 canvas.addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));\r
54                 canvas.addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));\r
55                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);\r
56                 userInput.buttonDrag = 3;\r
57                 userInput.buttonZoom = 2;\r
58                 userInput.enableUserInput();\r
59                 new ZoomableCanvasOverlay(canvas, null).enableScale();\r
60                 canvas.addListener(SWT.MouseDown, this::mouseDown);\r
61         }\r
62         private void initComponents()\r
63         {\r
64                 Simulation.TIMELINE.reset();\r
65                 WireArray r = new WireArray(1, WIRE_DELAY);\r
66                 WireArray s = new WireArray(1, WIRE_DELAY);\r
67                 WireArray t2 = new WireArray(1, WIRE_DELAY);\r
68                 WireArray t1 = new WireArray(1, WIRE_DELAY);\r
69                 WireArray q = new WireArray(1, WIRE_DELAY);\r
70                 WireArray nq = new WireArray(1, WIRE_DELAY);\r
71 \r
72                 GUIManualSwitch rIn = addComponent(new GUIManualSwitch(r), 100, 100);\r
73                 GUIManualSwitch sIn = addComponent(new GUIManualSwitch(s), 100, 200);\r
74                 GUIOrGate or1 = addComponent(new GUIOrGate(OR_DELAY, t1, r, nq), 160, 102.5);\r
75                 GUIOrGate or2 = addComponent(new GUIOrGate(OR_DELAY, t2, q, s), 160, 192.5);\r
76                 GUINotGate not1 = addComponent(new GUINotGate(NOT_DELAY, t1, q), 200, 107.5);\r
77                 GUINotGate not2 = addComponent(new GUINotGate(NOT_DELAY, t2, nq), 200, 197.5);\r
78 \r
79                 WireConnectionPoint p1 = addComponent(new WireConnectionPoint(q, 2), 250, 112.5);\r
80                 WireConnectionPoint p2 = addComponent(new WireConnectionPoint(nq, 2), 250, 202.5);\r
81 \r
82                 addWire(rIn, 0, or1, 0);\r
83                 addWire(sIn, 0, or2, 1);\r
84                 addWire(or1, 2, not1, 0);\r
85                 addWire(or2, 2, not2, 0);\r
86                 addWire(not1, 1, p1, 0);\r
87                 addWire(not2, 1, p2, 0);\r
88                 addWire(p1, 1, or2, 0, new Point(250, 130), new Point(140, 185), new Point(140, 197.5));\r
89                 addWire(p2, 1, or1, 1, new Point(250, 185), new Point(140, 130), new Point(140, 117.5));\r
90         }\r
91         /**\r
92          * Returns the given component for convenience.\r
93          */\r
94         private <C extends BasicGUIComponent> C addComponent(C component, double x, double y)\r
95         {\r
96                 components.add(component);\r
97                 componentPositions.put(component, new Point(x, y));\r
98                 return component;\r
99         }\r
100         private void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path)\r
101         {\r
102                 wires.add(new GUIWire(component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path));\r
103         }\r
104         private void drawComponent(GeneralGC gc, BasicGUIComponent component)\r
105         {\r
106                 TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));\r
107                 component.render(tgc);\r
108                 tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));\r
109                 for(int i = 0; i < component.getConnectedWireArraysCount(); i ++)\r
110                 {\r
111                         Point connectionPoint = component.getWireArrayConnectionPoint(i);\r
112                         if(connectionPoint != null)\r
113                                 tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);\r
114                 }\r
115         }\r
116         private void mouseDown(Event e)\r
117         {\r
118                 if(e.button == 1)\r
119                 {\r
120                         Point click = canvas.displayToWorldCoords(e.x, e.y);\r
121                         for(BasicGUIComponent component : components)\r
122                                 if(component.getBounds().translate(componentPositions.get(component)).contains(click))\r
123                                 {\r
124                                         if(component.clicked(click.x, click.y))\r
125                                                 canvas.redraw();\r
126                                         break;\r
127                                 }\r
128                 }\r
129         }\r
130 \r
131         public void run()\r
132         {\r
133                 shell.open();\r
134                 while(!shell.isDisposed())\r
135                         if(!display.readAndDispatch())\r
136                                 display.sleep();\r
137         }\r
138 \r
139         public static void main(String[] args)\r
140         {\r
141                 new LogicUI().run();\r
142         }\r
143 }