X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=LogicUI%2Fsrc%2Fera%2Fmi%2Fgui%2FLogicUI.java;h=45a8fa2a597cd8c02e0636da0dde2b3a0c9bb76d;hb=d51bf9c3df364fa37f80730a02a67009edf729e4;hp=74d516fdd567d65d52cc87c5f7308595886bd852;hpb=e0731f62800e4b2d8a77c759c1b204f9ece0e852;p=Mograsim.git diff --git a/LogicUI/src/era/mi/gui/LogicUI.java b/LogicUI/src/era/mi/gui/LogicUI.java index 74d516fd..45a8fa2a 100644 --- a/LogicUI/src/era/mi/gui/LogicUI.java +++ b/LogicUI/src/era/mi/gui/LogicUI.java @@ -22,14 +22,19 @@ import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas; import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay; import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput; +/** + * Standalone simulation visualizer. + * + * @author Daniel Kirschten + */ public class LogicUI { - private final Display display; - private final Shell shell; - private final ZoomableCanvas canvas; - private final Set components; - private final Map componentPositions; - private final Set wires; + private final Display display; + private final Shell shell; + private final ZoomableCanvas canvas; + private final Set components; + private final Map componentPositions; + private final Set wires; public LogicUI() { @@ -51,8 +56,11 @@ public class LogicUI new ZoomableCanvasOverlay(canvas, null).enableScale(); canvas.addListener(SWT.MouseDown, this::mouseDown); } + /** - * Returns the given component for convenience. + * Add a component to be drawn. Returns the given component for convenience. + * + * @author Daniel Kirschten */ public C addComponent(C component, double x, double y) { @@ -60,70 +68,79 @@ public class LogicUI componentPositions.put(component, new Point(x, y)); return component; } - public void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path) + + /** + * Add a graphical wire between the given connection points of the given components. The given components have to be added and the given + * connection points have to be connected logically first. + * + * @author Daniel Kirschten + */ + public void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, + int component2ConnectionIndex, Point... path) { - wires.add(new GUIWire(canvas::redrawThreadsafe, 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) { TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component)); component.render(tgc); tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); -// for(int i = 0; i < component.getConnectedWireArraysCount(); i ++) -// { -// Point connectionPoint = component.getWireArrayConnectionPoint(i); -// if(connectionPoint != null) -// tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2); -// } } + private void mouseDown(Event e) { - if(e.button == 1) + if (e.button == 1) { Point click = canvas.displayToWorldCoords(e.x, e.y); - for(BasicGUIComponent component : components) - if(component.getBounds().translate(componentPositions.get(component)).contains(click)) + for (BasicGUIComponent component : components) + if (component.getBounds().translate(componentPositions.get(component)).contains(click)) { - if(component.clicked(click.x, click.y)) + if (component.clicked(click.x, click.y)) canvas.redraw(); break; } } } + /** + * 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()) + while (running.get()) { - //always execute to keep timeline from "hanging behind" for too long + // 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()) - { + if (Simulation.TIMELINE.hasNext()) sleepTime = Simulation.TIMELINE.nextEventTime() - System.currentTimeMillis(); - } else - sleepTime = 100; + else + sleepTime = 10; try { - if(sleepTime > 0) + if (sleepTime > 0) Thread.sleep(sleepTime); - } catch(InterruptedException e) - {} //it is normal execution flow to be interrupted + } + catch (InterruptedException e) + { + } // it is normal execution flow to be interrupted } }); simulationThread.start(); Simulation.TIMELINE.addEventAddedListener(event -> { - if(event.getTiming() >= System.currentTimeMillis() / (double) 1) + if (event.getTiming() <= System.currentTimeMillis()) simulationThread.interrupt(); }); shell.open(); - while(!shell.isDisposed()) - if(!display.readAndDispatch()) + while (!shell.isDisposed()) + if (!display.readAndDispatch()) display.sleep(); running.set(false); simulationThread.interrupt();