Fixed problems in how GUIWires automatically choose a path
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / wires / GUIWire.java
index eddf98d..2ad79c5 100644 (file)
@@ -1,6 +1,7 @@
 package net.mograsim.logic.ui.model.wires;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Arrays;\r
 import java.util.List;\r
 \r
 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
@@ -18,13 +19,34 @@ public class GUIWire
        public final int logicWidth;\r
        private Pin pin1;\r
        private Pin pin2;\r
-       private double[] path;\r
+       private Point[] path;\r
+       private double[] effectivePath;\r
 \r
        private final List<Runnable> redrawListeners;\r
 \r
        private final LogicObserver logicObs;\r
        private ReadEnd end;\r
 \r
+       public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)\r
+       {\r
+               this(model, pin1, pin2, (Point[]) null);\r
+       }\r
+\r
+       public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)\r
+       {\r
+               this(model, pin1, pin2, (Point[]) null);\r
+       }\r
+\r
+       public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)\r
+       {\r
+               this(model, pin1, pin2, (Point[]) null);\r
+       }\r
+\r
+       public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)\r
+       {\r
+               this(model, pin1, pin2, (Point[]) null);\r
+       }\r
+\r
        public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)\r
        {\r
                this(model, pin1.getPin(), pin2.getPin(), path);\r
@@ -48,51 +70,50 @@ public class GUIWire
                if (pin2.logicWidth != pin1.logicWidth)\r
                        throw new IllegalArgumentException("Can't connect pins of different logic width");\r
 \r
-               if (path.length == 0)\r
-               {\r
-                       Point pos1 = pin1.getPos(), pos2 = pin2.getPos();\r
-                       path = new Point[] { new Point((pos1.x + pos2.x) / 2, pos1.y), new Point((pos1.x + pos2.x) / 2, pos2.y) };\r
-               }\r
-\r
-               applyPath(path);\r
-\r
                this.pin1 = pin1;\r
                this.pin2 = pin2;\r
 \r
+               this.path = path == null ? null : Arrays.copyOf(path, path.length);\r
+\r
                redrawListeners = new ArrayList<>();\r
 \r
                pin1.addPinMovedListener(p -> pin1Moved());\r
                pin2.addPinMovedListener(p -> pin2Moved());\r
-               pin1Moved();\r
-               pin2Moved();\r
+\r
+               recalculateEffectivePath();\r
 \r
                model.wireCreated(this);\r
        }\r
 \r
-       private void applyPath(Point... path)\r
+       private void recalculateEffectivePath()\r
        {\r
-               this.path = new double[path.length * 2 + 4];\r
-\r
-               for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)\r
+               Point pos1 = pin1.getPos(), pos2 = pin2.getPos();\r
+               if (path == null)\r
+                       effectivePath = new double[] { pos1.x, pos1.y, (pos1.x + pos2.x) / 2, pos1.y, (pos1.x + pos2.x) / 2, pos2.y, pos2.x, pos2.y };\r
+               else\r
                {\r
-                       this.path[dstI + 0] = path[srcI].x;\r
-                       this.path[dstI + 1] = path[srcI].y;\r
+                       effectivePath = new double[path.length * 2 + 4];\r
+                       effectivePath[0] = pos1.x;\r
+                       effectivePath[1] = pos1.y;\r
+                       for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)\r
+                       {\r
+                               effectivePath[dstI + 0] = path[srcI].x;\r
+                               effectivePath[dstI + 1] = path[srcI].y;\r
+                       }\r
+                       effectivePath[effectivePath.length - 2] = pos2.x;\r
+                       effectivePath[effectivePath.length - 1] = pos2.y;\r
                }\r
        }\r
 \r
        private void pin1Moved()\r
        {\r
-               Point pos = pin1.getPos();\r
-               this.path[0] = pos.x;\r
-               this.path[1] = pos.y;\r
+               recalculateEffectivePath();\r
                callRedrawListeners();\r
        }\r
 \r
        private void pin2Moved()\r
        {\r
-               Point pos = pin2.getPos();\r
-               this.path[this.path.length - 2] = pos.x;\r
-               this.path[this.path.length - 1] = pos.y;\r
+               recalculateEffectivePath();\r
                callRedrawListeners();\r
        }\r
 \r
@@ -103,7 +124,7 @@ public class GUIWire
 \r
        public void render(GeneralGC gc)\r
        {\r
-               ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(path));\r
+               ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath));\r
        }\r
 \r
        public void setLogicModelBinding(ReadEnd end)\r