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