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.timeline.Timeline;
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;
24 private Timeline timeline;
26 public LogicUIStandalone(Timeline timeline)
28 this.timeline = timeline;
29 display = new Display();
30 shell = new Shell(display);
31 shell.setLayout(new FillLayout());
32 ui = new LogicUICanvas(shell, SWT.NONE);
34 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
35 userInput.buttonDrag = 3;
36 userInput.buttonZoom = 2;
37 userInput.enableUserInput();
38 new ZoomableCanvasOverlay(ui, null).enableScale();
41 public LogicUICanvas getLogicUICanvas()
47 * Start the simulation timeline, and open the UI shell. Returns when the shell is closed.
51 AtomicBoolean running = new AtomicBoolean(true);
52 Thread simulationThread = new Thread(() ->
56 // always execute to keep timeline from "hanging behind" for too long
57 timeline.executeUntil(timeline.laterThan(System.currentTimeMillis()), System.currentTimeMillis() + 10);
59 if (timeline.hasNext())
60 sleepTime = timeline.nextEventTime() - System.currentTimeMillis();
66 Thread.sleep(sleepTime);
68 catch (InterruptedException e)
70 } // it is normal execution flow to be interrupted
73 simulationThread.start();
74 timeline.addEventAddedListener(event ->
76 if (event.getTiming() <= System.currentTimeMillis())
77 simulationThread.interrupt();
81 while (!shell.isDisposed())
82 if (!display.readAndDispatch())
85 simulationThread.interrupt();