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