3 import java.util.concurrent.atomic.AtomicBoolean;
\r
5 import org.eclipse.swt.SWT;
\r
6 import org.eclipse.swt.layout.FillLayout;
\r
7 import org.eclipse.swt.widgets.Display;
\r
8 import org.eclipse.swt.widgets.Shell;
\r
10 import era.mi.gui.model.ViewModel;
\r
11 import era.mi.gui.modeladapter.LogicModelParameters;
\r
12 import era.mi.gui.modeladapter.ViewLogicModelAdapter;
\r
13 import era.mi.logic.timeline.Timeline;
\r
14 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
\r
15 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
\r
18 * Standalone simulation visualizer.
\r
20 * @author Daniel Kirschten
\r
22 public class LogicUIStandalone
\r
24 private ViewModel model;
\r
25 private Timeline timeline;
\r
27 private final Display display;
\r
28 private final Shell shell;
\r
29 private final LogicUICanvas ui;
\r
31 public LogicUIStandalone(ViewModel model)
\r
34 display = new Display();
\r
35 shell = new Shell(display);
\r
36 shell.setLayout(new FillLayout());
\r
37 ui = new LogicUICanvas(shell, SWT.NONE, model);
\r
39 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
\r
40 userInput.buttonDrag = 3;
\r
41 userInput.buttonZoom = 2;
\r
42 userInput.enableUserInput();
\r
43 new ZoomableCanvasOverlay(ui, null).enableScale();
\r
45 // TODO don't do this here
\r
46 LogicModelParameters params = new LogicModelParameters();
\r
47 params.gateProcessTime = 50;
\r
48 params.wireTravelTime = 10;
\r
49 timeline = ViewLogicModelAdapter.convert(model, params);
\r
52 public LogicUICanvas getLogicUICanvas()
\r
58 * Start the simulation timeline, and open the UI shell. Returns when the shell is closed.
\r
62 AtomicBoolean running = new AtomicBoolean(true);
\r
63 Thread simulationThread = new Thread(() ->
\r
65 while (running.get())
\r
67 // always execute to keep timeline from "hanging behind" for too long
\r
68 timeline.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
\r
70 if (timeline.hasNext())
\r
71 sleepTime = timeline.nextEventTime() - System.currentTimeMillis();
\r
77 Thread.sleep(sleepTime);
\r
79 catch (InterruptedException e)
\r
81 } // it is normal execution flow to be interrupted
\r
84 simulationThread.start();
\r
85 timeline.addEventAddedListener(event ->
\r
87 if (event.getTiming() <= System.currentTimeMillis())
\r
88 simulationThread.interrupt();
\r
92 while (!shell.isDisposed())
\r
93 if (!display.readAndDispatch())
\r
96 // simulationThread.interrupt();
\r