X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=LogicUI%2Fsrc%2Fera%2Fmi%2Fexamples%2Fgui%2FLogicUI.java;h=a0ef151d9d32a5669c2e8fcc8ac3d37520c839c3;hb=97060d7f88d21c40bf3e4bcafaa360ece652eb2e;hp=f58ececbddd1168db1fcf459474daf549594d4a5;hpb=0f2c86917cc0f273fabbe4fb02fe77dada522f31;p=Mograsim.git diff --git a/LogicUI/src/era/mi/examples/gui/LogicUI.java b/LogicUI/src/era/mi/examples/gui/LogicUI.java index f58ececb..a0ef151d 100644 --- a/LogicUI/src/era/mi/examples/gui/LogicUI.java +++ b/LogicUI/src/era/mi/examples/gui/LogicUI.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; @@ -99,7 +100,7 @@ public class LogicUI } private void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path) { - wires.add(new GUIWire(component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path)); + wires.add(new GUIWire(canvas::redrawThreadsafe, component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path)); } private void drawComponent(GeneralGC gc, BasicGUIComponent component) { @@ -130,10 +131,40 @@ public class LogicUI 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 = 100; + 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() / (double) 1) + simulationThread.interrupt(); + }); + shell.open(); while(!shell.isDisposed()) if(!display.readAndDispatch()) display.sleep(); + running.set(false); + simulationThread.interrupt(); } public static void main(String[] args)