+++ /dev/null
-package era.mi.gui;\r
-\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.concurrent.atomic.AtomicBoolean;\r
-\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.layout.FillLayout;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.swt.widgets.Event;\r
-import org.eclipse.swt.widgets.Shell;\r
-\r
-import era.mi.gui.components.BasicGUIComponent;\r
-import era.mi.gui.wires.GUIWire;\r
-import era.mi.logic.Simulation;\r
-import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
-import net.haspamelodica.swt.helper.gcs.TranslatedGC;\r
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
-import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;\r
-import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;\r
-import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;\r
-\r
-/**\r
- * Standalone simulation visualizer.\r
- * \r
- * @author Daniel Kirschten\r
- */\r
-public class LogicUI\r
-{\r
- private final Display display;\r
- private final Shell shell;\r
- private final ZoomableCanvas canvas;\r
- private final Set<BasicGUIComponent> components;\r
- private final Map<BasicGUIComponent, Point> componentPositions;\r
- private final Set<GUIWire> wires;\r
-\r
- public LogicUI()\r
- {\r
- display = new Display();\r
- shell = new Shell(display);\r
- shell.setLayout(new FillLayout());\r
- canvas = new ZoomableCanvas(shell, SWT.NONE);\r
-\r
- components = new HashSet<>();\r
- componentPositions = new HashMap<>();\r
- wires = new HashSet<>();\r
-\r
- canvas.addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));\r
- canvas.addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));\r
- ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);\r
- userInput.buttonDrag = 3;\r
- userInput.buttonZoom = 2;\r
- userInput.enableUserInput();\r
- new ZoomableCanvasOverlay(canvas, null).enableScale();\r
- canvas.addListener(SWT.MouseDown, this::mouseDown);\r
- }\r
-\r
- /**\r
- * Add a component to be drawn. Returns the given component for convenience.\r
- * \r
- * @author Daniel Kirschten\r
- */\r
- public <C extends BasicGUIComponent> C addComponent(C component, double x, double y)\r
- {\r
- components.add(component);\r
- componentPositions.put(component, new Point(x, y));\r
- return component;\r
- }\r
-\r
- /**\r
- * Add a graphical wire between the given connection points of the given components. The given components have to be added and the given\r
- * connection points have to be connected logically first.\r
- * \r
- * @author Daniel Kirschten\r
- */\r
- public void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2,\r
- int component2ConnectionIndex, Point... path)\r
- {\r
- wires.add(new GUIWire(canvas::redrawThreadsafe, component1, component1ConnectionIndex, componentPositions.get(component1),\r
- component2, component2ConnectionIndex, componentPositions.get(component2), path));\r
- }\r
-\r
- private void drawComponent(GeneralGC gc, BasicGUIComponent component)\r
- {\r
- TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));\r
- component.render(tgc);\r
- tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));\r
- }\r
-\r
- private void mouseDown(Event e)\r
- {\r
- if (e.button == 1)\r
- {\r
- Point click = canvas.displayToWorldCoords(e.x, e.y);\r
- for (BasicGUIComponent component : components)\r
- if (component.getBounds().translate(componentPositions.get(component)).contains(click))\r
- {\r
- if (component.clicked(click.x, click.y))\r
- canvas.redraw();\r
- break;\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Start the simulation timeline, and open the UI shell. Returns when the shell is closed.\r
- */\r
- public void run()\r
- {\r
- AtomicBoolean running = new AtomicBoolean(true);\r
- Thread simulationThread = new Thread(() ->\r
- {\r
- while (running.get())\r
- {\r
- // always execute to keep timeline from "hanging behind" for too long\r
- Simulation.TIMELINE.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);\r
- long sleepTime;\r
- if (Simulation.TIMELINE.hasNext())\r
- sleepTime = Simulation.TIMELINE.nextEventTime() - System.currentTimeMillis();\r
- else\r
- sleepTime = 10;\r
- try\r
- {\r
- if (sleepTime > 0)\r
- Thread.sleep(sleepTime);\r
- }\r
- catch (InterruptedException e)\r
- {\r
- } // it is normal execution flow to be interrupted\r
- }\r
- });\r
- simulationThread.start();\r
- Simulation.TIMELINE.addEventAddedListener(event ->\r
- {\r
- if (event.getTiming() <= System.currentTimeMillis())\r
- simulationThread.interrupt();\r
- });\r
-\r
- shell.open();\r
- while (!shell.isDisposed())\r
- if (!display.readAndDispatch())\r
- display.sleep();\r
- running.set(false);\r
- simulationThread.interrupt();\r
- }\r
-}
\ No newline at end of file
--- /dev/null
+package era.mi.gui;\r
+\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Event;\r
+\r
+import era.mi.gui.components.BasicGUIComponent;\r
+import era.mi.gui.wires.GUIWire;\r
+import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
+import net.haspamelodica.swt.helper.gcs.TranslatedGC;\r
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
+import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;\r
+\r
+/**\r
+ * Simulation visualizer canvas.\r
+ * \r
+ * @author Daniel Kirschten\r
+ */\r
+public class LogicUICanvas extends ZoomableCanvas\r
+{\r
+ private final Set<BasicGUIComponent> components;\r
+ private final Map<BasicGUIComponent, Point> componentPositions;\r
+ private final Set<GUIWire> wires;\r
+\r
+ public LogicUICanvas(Composite parent, int style)\r
+ {\r
+ super(parent, style);\r
+\r
+ components = new HashSet<>();\r
+ componentPositions = new HashMap<>();\r
+ wires = new HashSet<>();\r
+\r
+ addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));\r
+ addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));\r
+ addListener(SWT.MouseDown, this::mouseDown);\r
+ }\r
+\r
+ /**\r
+ * Add a component to be drawn. Returns the given component for convenience.\r
+ * \r
+ * @author Daniel Kirschten\r
+ */\r
+ public <C extends BasicGUIComponent> C addComponent(C component, double x, double y)\r
+ {\r
+ components.add(component);\r
+ componentPositions.put(component, new Point(x, y));\r
+ return component;\r
+ }\r
+\r
+ /**\r
+ * Add a graphical wire between the given connection points of the given components. The given components have to be added and the given\r
+ * connection points have to be connected logically first.\r
+ * \r
+ * @author Daniel Kirschten\r
+ */\r
+ public void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2,\r
+ int component2ConnectionIndex, Point... path)\r
+ {\r
+ wires.add(new GUIWire(this::redrawThreadsafe, component1, component1ConnectionIndex, componentPositions.get(component1), component2,\r
+ component2ConnectionIndex, componentPositions.get(component2), path));\r
+ }\r
+\r
+ private void drawComponent(GeneralGC gc, BasicGUIComponent component)\r
+ {\r
+ TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));\r
+ component.render(tgc);\r
+ tgc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLUE));\r
+ }\r
+\r
+ private void mouseDown(Event e)\r
+ {\r
+ if (e.button == 1)\r
+ {\r
+ Point click = displayToWorldCoords(e.x, e.y);\r
+ for (BasicGUIComponent component : components)\r
+ if (component.getBounds().translate(componentPositions.get(component)).contains(click))\r
+ {\r
+ if (component.clicked(click.x, click.y))\r
+ redraw();\r
+ break;\r
+ }\r
+ }\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+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();
+ }
+}
\ No newline at end of file
package era.mi.gui.examples;\r
\r
-import era.mi.gui.LogicUI;\r
+import era.mi.gui.LogicUICanvas;\r
+import era.mi.gui.LogicUIStandalone;\r
import era.mi.gui.components.GUIManualSwitch;\r
import era.mi.gui.components.GUINotGate;\r
import era.mi.gui.components.GUIOrGate;\r
\r
public static void main(String[] args)\r
{\r
- LogicUI ui = new LogicUI();\r
- initComponents(ui);\r
+ LogicUIStandalone ui = new LogicUIStandalone();\r
+ initComponents(ui.getLogicUICanvas());\r
ui.run();\r
}\r
\r
- private static void initComponents(LogicUI ui)\r
+ private static void initComponents(LogicUICanvas ui)\r
{\r
Simulation.TIMELINE.reset();\r
Wire r = new Wire(1, WIRE_DELAY);\r