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