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