Timeline now passed via constructor
[Mograsim.git] / LogicUI / src / era / mi / gui / LogicUIStandalone.java
index fea7e18..c50af4a 100644 (file)
@@ -1,85 +1,87 @@
-package era.mi.gui;
-
-import java.util.concurrent.atomic.AtomicBoolean;
-
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.layout.FillLayout;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Shell;
-
-import era.mi.logic.Simulation;
-import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
-import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
-
-/**
- * Standalone simulation visualizer.
- * 
- * @author Daniel Kirschten
- */
-public class LogicUIStandalone
-{
-       private final Display display;
-       private final Shell shell;
-       private final LogicUICanvas ui;
-
-       public LogicUIStandalone()
-       {
-               display = new Display();
-               shell = new Shell(display);
-               shell.setLayout(new FillLayout());
-               ui = new LogicUICanvas(shell, SWT.NONE);
-
-               ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
-               userInput.buttonDrag = 3;
-               userInput.buttonZoom = 2;
-               userInput.enableUserInput();
-               new ZoomableCanvasOverlay(ui, null).enableScale();
-       }
-
-       public LogicUICanvas getLogicUICanvas()
-       {
-               return ui;
-       }
-
-       /**
-        * Start the simulation timeline, and open the UI shell. Returns when the shell is closed.
-        */
-       public void run()
-       {
-               AtomicBoolean running = new AtomicBoolean(true);
-               Thread simulationThread = new Thread(() ->
-               {
-                       while (running.get())
-                       {
-                               // always execute to keep timeline from "hanging behind" for too long
-                               Simulation.TIMELINE.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
-                               long sleepTime;
-                               if (Simulation.TIMELINE.hasNext())
-                                       sleepTime = Simulation.TIMELINE.nextEventTime() - System.currentTimeMillis();
-                               else
-                                       sleepTime = 10;
-                               try
-                               {
-                                       if (sleepTime > 0)
-                                               Thread.sleep(sleepTime);
-                               }
-                               catch (InterruptedException e)
-                               {
-                               } // it is normal execution flow to be interrupted
-                       }
-               });
-               simulationThread.start();
-               Simulation.TIMELINE.addEventAddedListener(event ->
-               {
-                       if (event.getTiming() <= System.currentTimeMillis())
-                               simulationThread.interrupt();
-               });
-
-               shell.open();
-               while (!shell.isDisposed())
-                       if (!display.readAndDispatch())
-                               display.sleep();
-               running.set(false);
-               simulationThread.interrupt();
-       }
+package era.mi.gui;\r
+\r
+import java.util.concurrent.atomic.AtomicBoolean;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Shell;\r
+\r
+import era.mi.logic.timeline.Timeline;\r
+import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;\r
+import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;\r
+\r
+/**\r
+ * Standalone simulation visualizer.\r
+ * \r
+ * @author Daniel Kirschten\r
+ */\r
+public class LogicUIStandalone\r
+{\r
+       private final Display display;\r
+       private final Shell shell;\r
+       private final LogicUICanvas ui;\r
+       private Timeline timeline;\r
+\r
+       public LogicUIStandalone(Timeline timeline)\r
+       {\r
+               this.timeline = timeline;\r
+               display = new Display();\r
+               shell = new Shell(display);\r
+               shell.setLayout(new FillLayout());\r
+               ui = new LogicUICanvas(shell, SWT.NONE);\r
+\r
+               ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);\r
+               userInput.buttonDrag = 3;\r
+               userInput.buttonZoom = 2;\r
+               userInput.enableUserInput();\r
+               new ZoomableCanvasOverlay(ui, null).enableScale();\r
+       }\r
+\r
+       public LogicUICanvas getLogicUICanvas()\r
+       {\r
+               return ui;\r
+       }\r
+\r
+       /**\r
+        * Start the simulation timeline, and open the UI shell. Returns when the shell is closed.\r
+        */\r
+       public void run()\r
+       {\r
+               AtomicBoolean running = new AtomicBoolean(true);\r
+               Thread simulationThread = new Thread(() ->\r
+               {\r
+                       while (running.get())\r
+                       {\r
+                               // always execute to keep timeline from "hanging behind" for too long\r
+                               timeline.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);\r
+                               long sleepTime;\r
+                               if (timeline.hasNext())\r
+                                       sleepTime = timeline.nextEventTime() - System.currentTimeMillis();\r
+                               else\r
+                                       sleepTime = 10;\r
+                               try\r
+                               {\r
+                                       if (sleepTime > 0)\r
+                                               Thread.sleep(sleepTime);\r
+                               }\r
+                               catch (InterruptedException e)\r
+                               {\r
+                               } // it is normal execution flow to be interrupted\r
+                       }\r
+               });\r
+               simulationThread.start();\r
+               timeline.addEventAddedListener(event ->\r
+               {\r
+                       if (event.getTiming() <= System.currentTimeMillis())\r
+                               simulationThread.interrupt();\r
+               });\r
+\r
+               shell.open();\r
+               while (!shell.isDisposed())\r
+                       if (!display.readAndDispatch())\r
+                               display.sleep();\r
+               running.set(false);\r
+               simulationThread.interrupt();\r
+       }\r
 }
\ No newline at end of file