Fixed a bug causing new timeline events to not be processed
[Mograsim.git] / LogicUI / src / era / mi / gui / LogicUI.java
1 package era.mi.gui;
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.wires.GUIWire;
17 import era.mi.logic.Simulation;
18 import net.haspamelodica.swt.helper.gcs.GeneralGC;
19 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
20 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
21 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
22 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
23 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
24
25 /**
26  * Standalone simulation visualizer.
27  * 
28  * @author Daniel Kirschten
29  */
30 public class LogicUI
31 {
32         private final Display                                           display;
33         private final Shell                                                     shell;
34         private final ZoomableCanvas                            canvas;
35         private final Set<BasicGUIComponent>            components;
36         private final Map<BasicGUIComponent, Point>     componentPositions;
37         private final Set<GUIWire>                                      wires;
38
39         public LogicUI()
40         {
41                 display = new Display();
42                 shell = new Shell(display);
43                 shell.setLayout(new FillLayout());
44                 canvas = new ZoomableCanvas(shell, SWT.NONE);
45
46                 components = new HashSet<>();
47                 componentPositions = new HashMap<>();
48                 wires = new HashSet<>();
49
50                 canvas.addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));
51                 canvas.addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));
52                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
53                 userInput.buttonDrag = 3;
54                 userInput.buttonZoom = 2;
55                 userInput.enableUserInput();
56                 new ZoomableCanvasOverlay(canvas, null).enableScale();
57                 canvas.addListener(SWT.MouseDown, this::mouseDown);
58         }
59         /**
60          * Add a component to be drawn.
61          * Returns the given component for convenience.
62          * 
63          * @author Daniel Kirschten
64          */
65         public <C extends BasicGUIComponent> C addComponent(C component, double x, double y)
66         {
67                 components.add(component);
68                 componentPositions.put(component, new Point(x, y));
69                 return component;
70         }
71         /**
72          * Add a graphical wire between the given connection points of the given components.
73          * The given components have to be added and the given connection points have to be connected logically first.
74          * 
75          * @author Daniel Kirschten
76          */
77         public void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path)
78         {
79                 wires.add(new GUIWire(canvas::redrawThreadsafe, component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path));
80         }
81         private void drawComponent(GeneralGC gc, BasicGUIComponent component)
82         {
83                 TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));
84                 component.render(tgc);
85                 tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
86         }
87         private void mouseDown(Event e)
88         {
89                 if(e.button == 1)
90                 {
91                         Point click = canvas.displayToWorldCoords(e.x, e.y);
92                         for(BasicGUIComponent component : components)
93                                 if(component.getBounds().translate(componentPositions.get(component)).contains(click))
94                                 {
95                                         if(component.clicked(click.x, click.y))
96                                                 canvas.redraw();
97                                         break;
98                                 }
99                 }
100         }
101
102         /**
103          * Start the simulation timeline, and open the UI shell.
104          * Returns when the shell is closed.
105          */
106         public void run()
107         {
108                 AtomicBoolean running = new AtomicBoolean(true);
109                 Thread simulationThread = new Thread(() ->
110                 {
111                         while(running.get())
112                         {
113                                 //always execute to keep timeline from "hanging behind" for too long
114                                 Simulation.TIMELINE.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
115                                 long sleepTime;
116                                 if(Simulation.TIMELINE.hasNext())
117                                         sleepTime = Simulation.TIMELINE.nextEventTime() - System.currentTimeMillis();
118                                 else
119                                         sleepTime = 10;
120                                 try
121                                 {
122                                         if(sleepTime > 0)
123                                                 Thread.sleep(sleepTime);
124                                 } catch(InterruptedException e)
125                                 {} //it is normal execution flow to be interrupted
126                         }
127                 });
128                 simulationThread.start();
129                 Simulation.TIMELINE.addEventAddedListener(event ->
130                 {
131                         if(event.getTiming() <= System.currentTimeMillis())
132                                 simulationThread.interrupt();
133                 });
134
135                 shell.open();
136                 while(!shell.isDisposed())
137                         if(!display.readAndDispatch())
138                                 display.sleep();
139                 running.set(false);
140                 simulationThread.interrupt();
141         }
142 }