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