ebe61b8f0af640b1499cded4aaf91e384d537687
[Mograsim.git] / LogicUI / src / era / mi / gui / examples / LogicUI.java
1 package era.mi.gui.examples;\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 import java.util.concurrent.atomic.AtomicBoolean;\r
8 \r
9 import org.eclipse.swt.SWT;\r
10 import org.eclipse.swt.layout.FillLayout;\r
11 import org.eclipse.swt.widgets.Display;\r
12 import org.eclipse.swt.widgets.Event;\r
13 import org.eclipse.swt.widgets.Shell;\r
14 \r
15 import era.mi.gui.components.BasicGUIComponent;\r
16 import era.mi.gui.components.GUIManualSwitch;\r
17 import era.mi.gui.components.GUINotGate;\r
18 import era.mi.gui.components.GUIOrGate;\r
19 import era.mi.gui.wires.GUIWire;\r
20 import era.mi.gui.wires.WireConnectionPoint;\r
21 import era.mi.logic.Simulation;\r
22 import era.mi.logic.wires.WireArray;\r
23 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
24 import net.haspamelodica.swt.helper.gcs.TranslatedGC;\r
25 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
26 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;\r
27 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;\r
28 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;\r
29 \r
30 public class LogicUI\r
31 {\r
32         private static final int                                        WIRE_DELAY      = 40;\r
33         private static final int                                        OR_DELAY        = 100;\r
34         private static final int                                        NOT_DELAY       = 100;\r
35         private final Display                                           display;\r
36         private final Shell                                                     shell;\r
37         private final ZoomableCanvas                            canvas;\r
38         private final Set<BasicGUIComponent>            components;\r
39         private final Map<BasicGUIComponent, Point>     componentPositions;\r
40         private final Set<GUIWire>                                      wires;\r
41 \r
42         public LogicUI()\r
43         {\r
44                 display = new Display();\r
45                 shell = new Shell(display);\r
46                 shell.setLayout(new FillLayout());\r
47                 canvas = new ZoomableCanvas(shell, SWT.NONE);\r
48 \r
49                 components = new HashSet<>();\r
50                 componentPositions = new HashMap<>();\r
51                 wires = new HashSet<>();\r
52                 initComponents();\r
53 \r
54                 canvas.addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));\r
55                 canvas.addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));\r
56                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);\r
57                 userInput.buttonDrag = 3;\r
58                 userInput.buttonZoom = 2;\r
59                 userInput.enableUserInput();\r
60                 new ZoomableCanvasOverlay(canvas, null).enableScale();\r
61                 canvas.addListener(SWT.MouseDown, this::mouseDown);\r
62         }\r
63         private void initComponents()\r
64         {\r
65                 Simulation.TIMELINE.reset();\r
66                 WireArray r = new WireArray(1, WIRE_DELAY);\r
67                 WireArray s = new WireArray(1, WIRE_DELAY);\r
68                 WireArray t2 = new WireArray(1, WIRE_DELAY);\r
69                 WireArray t1 = new WireArray(1, WIRE_DELAY);\r
70                 WireArray q = new WireArray(1, WIRE_DELAY);\r
71                 WireArray nq = new WireArray(1, WIRE_DELAY);\r
72 \r
73                 GUIManualSwitch rIn = addComponent(new GUIManualSwitch(r), 100, 100);\r
74                 GUIManualSwitch sIn = addComponent(new GUIManualSwitch(s), 100, 200);\r
75                 GUIOrGate or1 = addComponent(new GUIOrGate(OR_DELAY, t1, r, nq), 160, 102.5);\r
76                 GUIOrGate or2 = addComponent(new GUIOrGate(OR_DELAY, t2, q, s), 160, 192.5);\r
77                 GUINotGate not1 = addComponent(new GUINotGate(NOT_DELAY, t1, q), 200, 107.5);\r
78                 GUINotGate not2 = addComponent(new GUINotGate(NOT_DELAY, t2, nq), 200, 197.5);\r
79 \r
80                 WireConnectionPoint p1 = addComponent(new WireConnectionPoint(q, 2), 250, 112.5);\r
81                 WireConnectionPoint p2 = addComponent(new WireConnectionPoint(nq, 2), 250, 202.5);\r
82 \r
83                 addWire(rIn, 0, or1, 0);\r
84                 addWire(sIn, 0, or2, 1);\r
85                 addWire(or1, 2, not1, 0);\r
86                 addWire(or2, 2, not2, 0);\r
87                 addWire(not1, 1, p1, 0);\r
88                 addWire(not2, 1, p2, 0);\r
89                 addWire(p1, 1, or2, 0, new Point(250, 130), new Point(140, 185), new Point(140, 197.5));\r
90                 addWire(p2, 1, or1, 1, new Point(250, 185), new Point(140, 130), new Point(140, 117.5));\r
91         }\r
92         /**\r
93          * Returns the given component for convenience.\r
94          */\r
95         private <C extends BasicGUIComponent> C addComponent(C component, double x, double y)\r
96         {\r
97                 components.add(component);\r
98                 componentPositions.put(component, new Point(x, y));\r
99                 return component;\r
100         }\r
101         private void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path)\r
102         {\r
103                 wires.add(new GUIWire(canvas::redrawThreadsafe, component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path));\r
104         }\r
105         private void drawComponent(GeneralGC gc, BasicGUIComponent component)\r
106         {\r
107                 TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));\r
108                 component.render(tgc);\r
109                 tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));\r
110                 for(int i = 0; i < component.getConnectedWireArraysCount(); i ++)\r
111                 {\r
112                         Point connectionPoint = component.getWireArrayConnectionPoint(i);\r
113                         if(connectionPoint != null)\r
114                                 tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);\r
115                 }\r
116         }\r
117         private void mouseDown(Event e)\r
118         {\r
119                 if(e.button == 1)\r
120                 {\r
121                         Point click = canvas.displayToWorldCoords(e.x, e.y);\r
122                         for(BasicGUIComponent component : components)\r
123                                 if(component.getBounds().translate(componentPositions.get(component)).contains(click))\r
124                                 {\r
125                                         if(component.clicked(click.x, click.y))\r
126                                                 canvas.redraw();\r
127                                         break;\r
128                                 }\r
129                 }\r
130         }\r
131 \r
132         public void run()\r
133         {\r
134                 AtomicBoolean running = new AtomicBoolean(true);\r
135                 Thread simulationThread = new Thread(() ->\r
136                 {\r
137                         while(running.get())\r
138                         {\r
139                                 //always execute to keep timeline from "hanging behind" for too long\r
140                                 Simulation.TIMELINE.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);\r
141                                 long sleepTime;\r
142                                 if(Simulation.TIMELINE.hasNext())\r
143                                 {\r
144                                         sleepTime = Simulation.TIMELINE.nextEventTime() - System.currentTimeMillis();\r
145                                 } else\r
146                                         sleepTime = 100;\r
147                                 try\r
148                                 {\r
149                                         if(sleepTime > 0)\r
150                                                 Thread.sleep(sleepTime);\r
151                                 } catch(InterruptedException e)\r
152                                 {} //it is normal execution flow to be interrupted\r
153                         }\r
154                 });\r
155                 simulationThread.start();\r
156                 Simulation.TIMELINE.addEventAddedListener(event ->\r
157                 {\r
158                         if(event.getTiming() >= System.currentTimeMillis() / (double) 1)\r
159                                 simulationThread.interrupt();\r
160                 });\r
161 \r
162                 shell.open();\r
163                 while(!shell.isDisposed())\r
164                         if(!display.readAndDispatch())\r
165                                 display.sleep();\r
166                 running.set(false);\r
167                 simulationThread.interrupt();\r
168         }\r
169 \r
170         public static void main(String[] args)\r
171         {\r
172                 new LogicUI().run();\r
173         }\r
174 }