X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=LogicUI%2Fsrc%2Fera%2Fmi%2Fgui%2FLogicUIStandalone.java;h=c50af4ae51ef6a9d0732c7879fe4f1dcd290b617;hb=dba76a37f244d6b216c23f47bd020a87ea1ce328;hp=fea7e184078a00fc3114f8c0af830e2992419f12;hpb=29132a3ced2ade6609a747f731614e235830b9b9;p=Mograsim.git diff --git a/LogicUI/src/era/mi/gui/LogicUIStandalone.java b/LogicUI/src/era/mi/gui/LogicUIStandalone.java index fea7e184..c50af4ae 100644 --- a/LogicUI/src/era/mi/gui/LogicUIStandalone.java +++ b/LogicUI/src/era/mi/gui/LogicUIStandalone.java @@ -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; + +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.timeline.Timeline; +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; + private Timeline timeline; + + public LogicUIStandalone(Timeline timeline) + { + this.timeline = timeline; + 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 + timeline.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10); + long sleepTime; + if (timeline.hasNext()) + sleepTime = 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(); + 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(); + } } \ No newline at end of file