Removed drawing of connection points
[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                 component.render(new TranslatedGC(gc, componentPositions.get(component)));\r
108         }\r
109         private void mouseDown(Event e)\r
110         {\r
111                 if(e.button == 1)\r
112                 {\r
113                         Point click = canvas.displayToWorldCoords(e.x, e.y);\r
114                         for(BasicGUIComponent component : components)\r
115                                 if(component.getBounds().translate(componentPositions.get(component)).contains(click))\r
116                                 {\r
117                                         if(component.clicked(click.x, click.y))\r
118                                                 canvas.redraw();\r
119                                         break;\r
120                                 }\r
121                 }\r
122         }\r
123 \r
124         public void run()\r
125         {\r
126                 AtomicBoolean running = new AtomicBoolean(true);\r
127                 Thread simulationThread = new Thread(() ->\r
128                 {\r
129                         while(running.get())\r
130                         {\r
131                                 //always execute to keep timeline from "hanging behind" for too long\r
132                                 Simulation.TIMELINE.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);\r
133                                 long sleepTime;\r
134                                 if(Simulation.TIMELINE.hasNext())\r
135                                 {\r
136                                         sleepTime = Simulation.TIMELINE.nextEventTime() - System.currentTimeMillis();\r
137                                 } else\r
138                                         sleepTime = 100;\r
139                                 try\r
140                                 {\r
141                                         if(sleepTime > 0)\r
142                                                 Thread.sleep(sleepTime);\r
143                                 } catch(InterruptedException e)\r
144                                 {} //it is normal execution flow to be interrupted\r
145                         }\r
146                 });\r
147                 simulationThread.start();\r
148                 Simulation.TIMELINE.addEventAddedListener(event ->\r
149                 {\r
150                         if(event.getTiming() >= System.currentTimeMillis() / (double) 1)\r
151                                 simulationThread.interrupt();\r
152                 });\r
153 \r
154                 shell.open();\r
155                 while(!shell.isDisposed())\r
156                         if(!display.readAndDispatch())\r
157                                 display.sleep();\r
158                 running.set(false);\r
159                 simulationThread.interrupt();\r
160         }\r
161 \r
162         public static void main(String[] args)\r
163         {\r
164                 new LogicUI().run();\r
165         }\r
166 }