Fixed redrawing bug
[Mograsim.git] / LogicUI / src / era / mi / gui / LogicUIStandalone.java
1 package era.mi.gui;
2
3 import java.util.concurrent.atomic.AtomicBoolean;
4
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.layout.FillLayout;
7 import org.eclipse.swt.widgets.Display;
8 import org.eclipse.swt.widgets.Shell;
9
10 import era.mi.gui.model.ViewModel;
11 import era.mi.gui.modeladapter.LogicModelParameters;
12 import era.mi.gui.modeladapter.ViewLogicModelAdapter;
13 import era.mi.logic.timeline.Timeline;
14 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
15 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
16
17 /**
18  * Standalone simulation visualizer.
19  * 
20  * @author Daniel Kirschten
21  */
22 public class LogicUIStandalone
23 {
24         private ViewModel model;
25         private Timeline timeline;
26
27         private final Display display;
28         private final Shell shell;
29         private final LogicUICanvas ui;
30
31         public LogicUIStandalone(ViewModel model)
32         {
33                 this.model = model;
34                 display = new Display();
35                 shell = new Shell(display);
36                 shell.setLayout(new FillLayout());
37                 ui = new LogicUICanvas(shell, SWT.NONE, model);
38
39                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
40                 userInput.buttonDrag = 3;
41                 userInput.buttonZoom = 2;
42                 userInput.enableUserInput();
43                 new ZoomableCanvasOverlay(ui, null).enableScale();
44
45                 // TODO don't do this here
46                 LogicModelParameters params = new LogicModelParameters();
47                 params.gateProcessTime = 50;
48                 params.wireTravelTime = 10;
49                 timeline = ViewLogicModelAdapter.convert(model, params);
50         }
51
52         public LogicUICanvas getLogicUICanvas()
53         {
54                 return ui;
55         }
56
57         /**
58          * Start the simulation timeline, and open the UI shell. Returns when the shell is closed.
59          */
60         public void run()
61         {
62                 AtomicBoolean running = new AtomicBoolean(true);
63                 Thread simulationThread = new Thread(() ->
64                 {
65                         while (running.get())
66                         {
67                                 // always execute to keep timeline from "hanging behind" for too long
68                                 timeline.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
69                                 long sleepTime;
70                                 if (timeline.hasNext())
71                                         sleepTime = timeline.nextEventTime() - System.currentTimeMillis();
72                                 else
73                                         sleepTime = 10;
74                                 try
75                                 {
76                                         if (sleepTime > 0)
77                                                 Thread.sleep(sleepTime);
78                                 }
79                                 catch (InterruptedException e)
80                                 {
81                                 } // it is normal execution flow to be interrupted
82                         }
83                 });
84                 simulationThread.start();
85                 timeline.addEventAddedListener(event ->
86                 {
87                         if (event.getTiming() <= System.currentTimeMillis())
88                                 simulationThread.interrupt();
89                 });
90
91                 shell.open();
92                 while (!shell.isDisposed())
93                         if (!display.readAndDispatch())
94                                 display.sleep();
95                 running.set(false);
96 //              simulationThread.interrupt();
97         }
98 }