Added fulladder. GUIWires are now drawn with right angles.
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / wires / GUIWire.java
index 50871fe..63c2160 100644 (file)
@@ -10,11 +10,11 @@ import net.mograsim.logic.core.LogicObserver;
 import net.mograsim.logic.core.types.BitVectorFormatter;
 import net.mograsim.logic.core.wires.Wire.ReadEnd;
 import net.mograsim.logic.ui.ColorHelper;
-import net.mograsim.logic.ui.model.ViewModel;
+import net.mograsim.logic.ui.model.ViewModelModifiable;
 
 public class GUIWire
 {
-       private final ViewModel model;
+       private final ViewModelModifiable model;
        public final int logicWidth;
        private Pin pin1;
        private Pin pin2;
@@ -25,20 +25,37 @@ public class GUIWire
        private final LogicObserver logicObs;
        private ReadEnd end;
 
-       public GUIWire(ViewModel model, Pin pin1, Pin pin2, Point... path)
+       public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
+       {
+               this(model, pin1.getPin(), pin2.getPin(), path);
+       }
+
+       public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
+       {
+               this(model, pin1.getPin(), pin2, path);
+       }
+
+       public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
+       {
+               this(model, pin1, pin2.getPin(), path);
+       }
+
+       public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
        {
                logicObs = (i) -> callRedrawListeners();
                this.model = model;
                this.logicWidth = pin1.logicWidth;
                if (pin2.logicWidth != pin1.logicWidth)
                        throw new IllegalArgumentException("Can't connect pins of different logic width");
-               this.path = new double[path.length * 2 + 4];
-               for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
+
+               if (path.length == 0)
                {
-                       this.path[dstI + 0] = path[srcI].x;
-                       this.path[dstI + 1] = path[srcI].y;
+                       Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
+                       path = new Point[] { new Point((pos1.x + pos2.x) / 2, pos1.y), new Point((pos1.x + pos2.x) / 2, pos2.y) };
                }
 
+               applyPath(path);
+
                this.pin1 = pin1;
                this.pin2 = pin2;
 
@@ -52,6 +69,17 @@ public class GUIWire
                model.wireCreated(this);
        }
 
+       private void applyPath(Point... path)
+       {
+               this.path = new double[path.length * 2 + 4];
+
+               for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
+               {
+                       this.path[dstI + 0] = path[srcI].x;
+                       this.path[dstI + 1] = path[srcI].y;
+               }
+       }
+
        private void pin1Moved()
        {
                Point pos = pin1.getPos();