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