import org.eclipse.swt.widgets.Shell;\r
\r
import era.mi.components.gui.BasicGUIComponent;\r
-import era.mi.components.gui.GUIAndGate;\r
import era.mi.components.gui.GUIManualSwitch;\r
-import era.mi.components.gui.GUIMerger;\r
-import era.mi.components.gui.GUIMux;\r
import era.mi.components.gui.GUINotGate;\r
-import era.mi.components.gui.GUISplitter;\r
+import era.mi.components.gui.GUIOrGate;\r
import era.mi.logic.Simulation;\r
import era.mi.logic.wires.WireArray;\r
+import era.mi.wires.gui.GUIWire;\r
+import era.mi.wires.gui.WireConnectionPoint;\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
\r
public class LogicUI\r
{\r
+ private static final int WIRE_DELAY = 40;\r
+ private static final int OR_DELAY = 100;\r
+ private static final int NOT_DELAY = 100;\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
\r
components = new HashSet<>();\r
componentPositions = new HashMap<>();\r
+ wires = new HashSet<>();\r
initComponents();\r
\r
- canvas.addZoomedRenderer(gc -> components.forEach(component -> drawComponent(gc, component)));\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
private void initComponents()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1), e = new WireArray(1, 1),\r
- f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1), j = new WireArray(1, 1), k = new WireArray(1, 1);\r
- addComponent(new GUIManualSwitch(a), 160, 10);\r
- addComponent(new GUIAndGate(1, f, a, b), 130, 10);\r
- addComponent(new GUINotGate(1, f, g), 100, 10);\r
- addComponent(new GUIMerger(h, c, g), 70, 10);\r
- addComponent(new GUIMux(1, i, e, h, d), 10, 10);\r
- addComponent(new GUISplitter(i, k, j), 40, 10);\r
+ WireArray r = new WireArray(1, WIRE_DELAY);\r
+ WireArray s = new WireArray(1, WIRE_DELAY);\r
+ WireArray t2 = new WireArray(1, WIRE_DELAY);\r
+ WireArray t1 = new WireArray(1, WIRE_DELAY);\r
+ WireArray q = new WireArray(1, WIRE_DELAY);\r
+ WireArray nq = new WireArray(1, WIRE_DELAY);\r
+\r
+ GUIManualSwitch rIn = addComponent(new GUIManualSwitch(r), 100, 100);\r
+ GUIManualSwitch sIn = addComponent(new GUIManualSwitch(s), 100, 200);\r
+ GUIOrGate or1 = addComponent(new GUIOrGate(OR_DELAY, t1, r, nq), 160, 102.5);\r
+ GUIOrGate or2 = addComponent(new GUIOrGate(OR_DELAY, t2, q, s), 160, 192.5);\r
+ GUINotGate not1 = addComponent(new GUINotGate(NOT_DELAY, t1, q), 200, 107.5);\r
+ GUINotGate not2 = addComponent(new GUINotGate(NOT_DELAY, t2, nq), 200, 197.5);\r
+\r
+ WireConnectionPoint p1 = addComponent(new WireConnectionPoint(q, 2), 250, 112.5);\r
+ WireConnectionPoint p2 = addComponent(new WireConnectionPoint(nq, 2), 250, 202.5);\r
+\r
+ addWire(rIn, 0, or1, 0);\r
+ addWire(sIn, 0, or2, 1);\r
+ addWire(or1, 2, not1, 0);\r
+ addWire(or2, 2, not2, 0);\r
+ addWire(not1, 1, p1, 0);\r
+ addWire(not2, 1, p2, 0);\r
+ addWire(p1, 1, or2, 0, new Point(250, 130), new Point(140, 185), new Point(140, 197.5));\r
+ addWire(p2, 1, or1, 1, new Point(250, 185), new Point(140, 130), new Point(140, 117.5));\r
}\r
- private void addComponent(BasicGUIComponent component, double x, double y)\r
+ /**\r
+ * Returns the given component for convenience.\r
+ */\r
+ private <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
+ private void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path)\r
+ {\r
+ wires.add(new GUIWire(component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path));\r
}\r
private void drawComponent(GeneralGC gc, BasicGUIComponent component)\r
{\r
--- /dev/null
+package era.mi.wires.gui;\r
+\r
+import java.util.Objects;\r
+\r
+import era.mi.components.gui.BasicGUIComponent;\r
+import era.mi.logic.wires.WireArray;\r
+import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
+\r
+public class GUIWire\r
+{\r
+ private final WireArray wa;\r
+ private final double[] path;\r
+\r
+ public GUIWire(BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos, BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path)\r
+ {\r
+ this.wa = component1.getConnectedWireArray(component1ConnectionIndex);\r
+ if(!Objects.equals(wa, component2.getConnectedWireArray(component2ConnectionIndex)))\r
+ throw new IllegalArgumentException("Given connection points are not connected!");\r
+ this.path = new double[path.length * 2 + 4];\r
+ Point component1ConnectionPoint = component1.getWireArrayConnectionPoint(component1ConnectionIndex);\r
+ this.path[0] = component1Pos.x + component1ConnectionPoint.x;\r
+ this.path[1] = component1Pos.y + component1ConnectionPoint.y;\r
+ for(int srcI = 0, dstI = 2; srcI < path.length; srcI ++, dstI += 2)\r
+ {\r
+ this.path[dstI + 0] = path[srcI].x;\r
+ this.path[dstI + 1] = path[srcI].y;\r
+ }\r
+ Point component2ConnectionPoint = component2.getWireArrayConnectionPoint(component2ConnectionIndex);\r
+ this.path[this.path.length - 2] = component2Pos.x + component2ConnectionPoint.x;\r
+ this.path[this.path.length - 1] = component2Pos.y + component2ConnectionPoint.y;\r
+ }\r
+\r
+ public void render(GeneralGC gc)\r
+ {\r
+ gc.drawPolyline(path);\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+package era.mi.wires.gui;\r
+\r
+import org.eclipse.swt.graphics.Color;\r
+\r
+import era.mi.components.gui.BasicGUIComponent;\r
+import era.mi.logic.wires.WireArray;\r
+import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;\r
+\r
+public class WireConnectionPoint implements BasicGUIComponent\r
+{\r
+ private final WireArray wa;\r
+ private final int wiresCrossing;\r
+\r
+ public WireConnectionPoint(WireArray wa, int wiresCrossing)\r
+ {\r
+ this.wa = wa;\r
+ this.wiresCrossing = wiresCrossing;\r
+ }\r
+\r
+ @Override\r
+ public void render(GeneralGC gc)\r
+ {\r
+ Color oldBG = gc.getBackground();\r
+ Color fg = gc.getForeground();\r
+ gc.setBackground(fg);\r
+ gc.fillOval(-2, -2, 4, 4);\r
+ gc.setBackground(oldBG);\r
+ }\r
+ @Override\r
+ public Rectangle getBounds()\r
+ {\r
+ return new Rectangle(0, 0, 0, 0);\r
+ }\r
+ @Override\r
+ public int getConnectedWireArraysCount()\r
+ {\r
+ return wiresCrossing;\r
+ }\r
+ @Override\r
+ public WireArray getConnectedWireArray(int connectionIndex)\r
+ {\r
+ return wa;\r
+ }\r
+ @Override\r
+ public Point getWireArrayConnectionPoint(int connectionIndex)\r
+ {\r
+ return new Point(0, 0);\r
+ }\r
+}
\ No newline at end of file