Completely changed the structure and switched to Eclipse Plugin.
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / wires / GUIWire.java
index 2ad79c5..a79054c 100644 (file)
@@ -4,64 +4,145 @@ import java.util.ArrayList;
 import java.util.Arrays;\r
 import java.util.List;\r
 \r
+import org.eclipse.swt.SWT;\r
+\r
 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
-import net.mograsim.logic.core.LogicObservable;\r
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;\r
 import net.mograsim.logic.core.LogicObserver;\r
 import net.mograsim.logic.core.types.BitVectorFormatter;\r
 import net.mograsim.logic.core.wires.Wire.ReadEnd;\r
 import net.mograsim.logic.ui.ColorHelper;\r
+import net.mograsim.logic.ui.model.ModelVisitor;\r
 import net.mograsim.logic.ui.model.ViewModelModifiable;\r
-\r
-public class GUIWire\r
+import net.mograsim.logic.ui.model.Visitable;\r
+\r
+/**\r
+ * A wire connecting exactly two {@link Pin}s.\r
+ * \r
+ * @author Daniel Kirschten\r
+ */\r
+public class GUIWire implements Visitable\r
 {\r
+       /**\r
+        * The model this wire is a part of.\r
+        */\r
        private final ViewModelModifiable model;\r
+       /**\r
+        * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.\r
+        */\r
        public final int logicWidth;\r
+       /**\r
+        * The {@link Pin} on one side of this wire, usually the signal source.\r
+        */\r
        private Pin pin1;\r
+       /**\r
+        * The {@link Pin} on one side of this wire, usually the signal target.\r
+        */\r
        private Pin pin2;\r
+       /**\r
+        * The user-defined path between {@link #pin1} and {@link #pin2}.<br>\r
+        * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any\r
+        * interpolation".\r
+        */\r
        private Point[] path;\r
+       /**\r
+        * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})\r
+        */\r
+       private final Rectangle bounds;\r
+       /**\r
+        * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.\r
+        */\r
        private double[] effectivePath;\r
 \r
        private final List<Runnable> redrawListeners;\r
 \r
+       /**\r
+        * A LogicObserver calling redrawListeners. Used for logic model bindings.\r
+        */\r
        private final LogicObserver logicObs;\r
+       /**\r
+        * A ReadEnd of the logic wire this GUI wire currently is bound to.\r
+        */\r
        private ReadEnd end;\r
 \r
+       // creation and destruction\r
+\r
+       /**\r
+        * Creates a new {@link GUIWire} with automatic interpolation.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)\r
        {\r
                this(model, pin1, pin2, (Point[]) null);\r
        }\r
 \r
+       /**\r
+        * Creates a new {@link GUIWire} with automatic interpolation.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)\r
        {\r
                this(model, pin1, pin2, (Point[]) null);\r
        }\r
 \r
+       /**\r
+        * Creates a new {@link GUIWire} with automatic interpolation.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)\r
        {\r
                this(model, pin1, pin2, (Point[]) null);\r
        }\r
 \r
+       /**\r
+        * Creates a new {@link GUIWire} with automatic interpolation.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)\r
        {\r
                this(model, pin1, pin2, (Point[]) null);\r
        }\r
 \r
+       /**\r
+        * Creates a new {@link GUIWire} without automatic interpolation.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)\r
        {\r
                this(model, pin1.getPin(), pin2.getPin(), path);\r
        }\r
 \r
+       /**\r
+        * Creates a new {@link GUIWire} without automatic interpolation.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)\r
        {\r
                this(model, pin1.getPin(), pin2, path);\r
        }\r
 \r
+       /**\r
+        * Creates a new {@link GUIWire} without automatic interpolation.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)\r
        {\r
                this(model, pin1, pin2.getPin(), path);\r
        }\r
 \r
+       /**\r
+        * Creates a new {@link GUIWire} without automatic interpolation.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)\r
        {\r
                logicObs = (i) -> callRedrawListeners();\r
@@ -74,20 +155,78 @@ public class GUIWire
                this.pin2 = pin2;\r
 \r
                this.path = path == null ? null : Arrays.copyOf(path, path.length);\r
+               this.bounds = new Rectangle(0, 0, -1, -1);\r
 \r
                redrawListeners = new ArrayList<>();\r
 \r
-               pin1.addPinMovedListener(p -> pin1Moved());\r
-               pin2.addPinMovedListener(p -> pin2Moved());\r
+               pin1.addPinMovedListener(p -> pinMoved());\r
+               pin2.addPinMovedListener(p -> pinMoved());\r
 \r
                recalculateEffectivePath();\r
 \r
                model.wireCreated(this);\r
        }\r
 \r
+       /**\r
+        * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model\r
+        * this component is a part of.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
+       public void destroy()\r
+       {\r
+               model.wireDestroyed(this);\r
+       }\r
+\r
+       // pins\r
+\r
+       /**\r
+        * Returns the {@link Pin} on one side of this wire, usually the signal source.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
+       public Pin getPin1()\r
+       {\r
+               return pin1;\r
+       }\r
+\r
+       /**\r
+        * Returns the {@link Pin} on one side of this wire, usually the signal target.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
+       public Pin getPin2()\r
+       {\r
+               return pin2;\r
+       }\r
+\r
+       /**\r
+        * Called when {@link #pin1} or {@link #pin2} were moved.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
+       private void pinMoved()\r
+       {\r
+               recalculateEffectivePath();\r
+               callRedrawListeners();\r
+       }\r
+\r
+       // "graphical" operations\r
+\r
+       /**\r
+        * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        private void recalculateEffectivePath()\r
        {\r
                Point pos1 = pin1.getPos(), pos2 = pin2.getPos();\r
+\r
+               double boundsX1 = Math.min(pos1.x, pos2.x);\r
+               double boundsY1 = Math.min(pos1.y, pos2.y);\r
+               double boundsX2 = Math.max(pos1.x, pos2.x);\r
+               double boundsY2 = Math.max(pos1.y, pos2.y);\r
+\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
@@ -97,64 +236,81 @@ public class GUIWire
                        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
+                               double pathX = path[srcI].x;\r
+                               double pathY = path[srcI].y;\r
+                               effectivePath[dstI + 0] = pathX;\r
+                               effectivePath[dstI + 1] = pathY;\r
+                               if (pathX < boundsX1)\r
+                                       boundsX1 = pathX;\r
+                               if (pathX > boundsX2)\r
+                                       boundsX2 = pathX;\r
+                               if (pathY < boundsY1)\r
+                                       boundsY1 = pathY;\r
+                               if (pathY > boundsY2)\r
+                                       boundsY2 = pathY;\r
                        }\r
                        effectivePath[effectivePath.length - 2] = pos2.x;\r
                        effectivePath[effectivePath.length - 1] = pos2.y;\r
                }\r
-       }\r
-\r
-       private void pin1Moved()\r
-       {\r
-               recalculateEffectivePath();\r
-               callRedrawListeners();\r
-       }\r
 \r
-       private void pin2Moved()\r
-       {\r
-               recalculateEffectivePath();\r
-               callRedrawListeners();\r
+               bounds.x = boundsX1;\r
+               bounds.y = boundsY1;\r
+               bounds.width = boundsX2 - boundsX1;\r
+               bounds.height = boundsY2 - boundsY1;\r
        }\r
 \r
-       public void destroy()\r
+       /**\r
+        * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
+       public Rectangle getBounds()\r
        {\r
-               model.wireDestroyed(this);\r
+               return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);\r
        }\r
 \r
+       /**\r
+        * Render this wire to the given gc, in absoulute coordinates.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
        public void render(GeneralGC gc)\r
        {\r
                ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath));\r
        }\r
 \r
-       public void setLogicModelBinding(ReadEnd end)\r
+       /**\r
+        * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn\r
+        * in {@link #render(GeneralGC)}.<br>\r
+        * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any\r
+        * interpolation".\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
+       public Point[] getPath()\r
        {\r
-               deregisterLogicObs(this.end);\r
-               this.end = end;\r
-               registerLogicObs(end);\r
+               return path == null ? null : path.clone();\r
        }\r
 \r
-       private void registerLogicObs(LogicObservable observable)\r
-       {\r
-               if (observable != null)\r
-                       observable.registerObserver(logicObs);\r
-       }\r
+       // logic model binding\r
 \r
-       private void deregisterLogicObs(LogicObservable observable)\r
-       {\r
-               if (observable != null)\r
-                       observable.deregisterObserver(logicObs);\r
-       }\r
-\r
-       public Pin getPin1()\r
+       /**\r
+        * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given\r
+        * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>\r
+        * The argument can be null, in which case the old binding is stopped.\r
+        * \r
+        * @author Daniel Kirschten\r
+        */\r
+       public void setLogicModelBinding(ReadEnd end)\r
        {\r
-               return pin1;\r
+               if (this.end != null)\r
+                       this.end.deregisterObserver(logicObs);\r
+               this.end = end;\r
+               if (end != null)\r
+                       end.registerObserver(logicObs);\r
        }\r
 \r
-       public Pin getPin2()\r
-       {\r
-               return pin2;\r
-       }\r
+       // listeners\r
 \r
        // @formatter:off\r
        public void addRedrawListener   (Runnable listener) {redrawListeners         .add   (listener);}\r
@@ -164,4 +320,15 @@ public class GUIWire
        private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}\r
        // @formatter:on\r
 \r
+       @Override\r
+       public String toString()\r
+       {\r
+               return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";\r
+       }\r
+\r
+       @Override\r
+       public void accept(ModelVisitor mv)\r
+       {\r
+               mv.visit(this);\r
+       }\r
 }
\ No newline at end of file