3 import java.util.concurrent.atomic.AtomicBoolean;
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;
10 import era.mi.logic.Simulation;
11 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
12 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
15 * Standalone simulation visualizer.
17 * @author Daniel Kirschten
19 public class LogicUIStandalone
21 private final Display display;
22 private final Shell shell;
23 private final LogicUICanvas ui;
25 public LogicUIStandalone()
27 display = new Display();
28 shell = new Shell(display);
29 shell.setLayout(new FillLayout());
30 ui = new LogicUICanvas(shell, SWT.NONE);
32 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
33 userInput.buttonDrag = 3;
34 userInput.buttonZoom = 2;
35 userInput.enableUserInput();
36 new ZoomableCanvasOverlay(ui, null).enableScale();
39 public LogicUICanvas getLogicUICanvas()
45 * Start the simulation timeline, and open the UI shell. Returns when the shell is closed.
49 AtomicBoolean running = new AtomicBoolean(true);
50 Thread simulationThread = new Thread(() ->
54 // always execute to keep timeline from "hanging behind" for too long
55 Simulation.TIMELINE.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
57 if (Simulation.TIMELINE.hasNext())
58 sleepTime = Simulation.TIMELINE.nextEventTime() - System.currentTimeMillis();
64 Thread.sleep(sleepTime);
66 catch (InterruptedException e)
68 } // it is normal execution flow to be interrupted
71 simulationThread.start();
72 Simulation.TIMELINE.addEventAddedListener(event ->
74 if (event.getTiming() <= System.currentTimeMillis())
75 simulationThread.interrupt();
79 while (!shell.isDisposed())
80 if (!display.readAndDispatch())
83 simulationThread.interrupt();