Started restructuring LogicUI
[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);
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 //                              model.timeline.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
59 //                              long sleepTime;
60 //                              if (model.timeline.hasNext())
61 //                                      sleepTime = model.timeline.nextEventTime() - System.currentTimeMillis();
62 //                              else
63 //                                      sleepTime = 10;
64 //                              try
65 //                              {
66 //                                      if (sleepTime > 0)
67 //                                              Thread.sleep(sleepTime);
68 //                              }
69 //                              catch (InterruptedException e)
70 //                              {
71 //                              } // it is normal execution flow to be interrupted
72 //                      }
73 //              });
74 //              simulationThread.start();
75 //              model.timeline.addEventAddedListener(event ->
76 //              {
77 //                      if (event.getTiming() <= System.currentTimeMillis())
78 //                              simulationThread.interrupt();
79 //              });
80
81                 shell.open();
82                 while (!shell.isDisposed())
83                         if (!display.readAndDispatch())
84                                 display.sleep();
85                 running.set(false);
86 //              simulationThread.interrupt();
87         }
88 }