X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fwires%2FGUIWire.java;h=2e36c61760c720d1d5ba007420423739fc3d126a;hb=421535c322e30427c90e62c8dd03a740a309f329;hp=08b1d42ef3c0e11c1d644e3c4de3a4112d8b2abe;hpb=74868b10728aee0e85e4ff8af4073516b7590268;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/GUIWire.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/GUIWire.java index 08b1d42e..2e36c617 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/GUIWire.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/GUIWire.java @@ -2,7 +2,9 @@ package net.mograsim.logic.ui.model.wires; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.eclipse.swt.SWT; @@ -10,10 +12,13 @@ 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.LogicObserver; +import net.mograsim.logic.core.types.BitVector; import net.mograsim.logic.core.types.BitVectorFormatter; +import net.mograsim.logic.core.wires.Wire; import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.ui.ColorHelper; import net.mograsim.logic.ui.model.ViewModelModifiable; +import net.mograsim.preferences.ColorDefinition; +import net.mograsim.preferences.ColorManager; /** * A wire connecting exactly two {@link Pin}s. @@ -22,6 +27,9 @@ import net.mograsim.logic.ui.model.ViewModelModifiable; */ public class GUIWire { + + private final Set pathChangedListeners; + /** * The model this wire is a part of. */ @@ -51,7 +59,7 @@ public class GUIWire /** * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null. */ - private double[] effectivePath; + protected double[] effectivePath; private final List redrawListeners; @@ -71,37 +79,7 @@ public class GUIWire * * @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) + public GUIWire(ViewModelModifiable model, ConnectionPoint pin1, ConnectionPoint pin2) { this(model, pin1, pin2, (Point[]) null); } @@ -111,7 +89,7 @@ public class GUIWire * * @author Daniel Kirschten */ - public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path) + public GUIWire(ViewModelModifiable model, ConnectionPoint pin1, ConnectionPoint pin2, Point... path) { this(model, pin1.getPin(), pin2.getPin(), path); } @@ -121,28 +99,9 @@ public class GUIWire * * @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) + GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path) { + pathChangedListeners = new HashSet<>(); logicObs = (i) -> callRedrawListeners(); this.model = model; this.logicWidth = pin1.logicWidth; @@ -274,7 +233,10 @@ public class GUIWire */ public void render(GeneralGC gc) { - ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath)); + ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end); + if (wireColor != null) + gc.setForeground(ColorManager.current().toColor(wireColor)); + gc.drawPolyline(effectivePath); } /** @@ -308,6 +270,38 @@ public class GUIWire end.registerObserver(logicObs); } + /** + * Returns whether this {@link GUIWire} has a logic model binding or not. + * + * @author Daniel Kirschten + */ + public boolean hasLogicModelBinding() + { + return end != null; + } + + /** + * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire} + * corresponding to this {@link GUIWire}. + * + * @author Daniel Kirschten + */ + public void forceWireValues(BitVector values) + { + end.getWire().forceValues(values); + } + + /** + * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to + * this {@link GUIWire}. + * + * @author Daniel Kirschten + */ + public BitVector getWireValues() + { + return end.getValues(); + } + // listeners // @formatter:off @@ -316,6 +310,24 @@ public class GUIWire public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);} private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());} + + public void addPathChangedListener(PathChangedListener l) { pathChangedListeners.add(l); } + + public void removePathChangedListener(PathChangedListener l) { pathChangedListeners.remove(l); } + + public void callPathChangedListeners(int diff) { pathChangedListeners.forEach(l -> l.pathChanged(this, diff)); } + + @FunctionalInterface + public static interface PathChangedListener + { + /** + * Called whenever the {@link Wire}'s path changes + * + * @param wire The wire which had its path changed + * @param diff The length difference between before and after the path change. + */ + public void pathChanged(GUIWire wire, int diff); + } // @formatter:on @Override @@ -323,4 +335,65 @@ public class GUIWire { return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]"; } + + public void setPath(Point[] path) + { + int diff = (path == null ? 0 : path.length) - (this.path == null ? 0 : this.path.length); + this.path = path == null ? null : path.clone(); + recalculateEffectivePath(); + callPathChangedListeners(diff); + callRedrawListeners(); + } + + public void setPathPoint(Point p, int index) + { + path[index] = p; + recalculateEffectivePath(); + callPathChangedListeners(0); + callRedrawListeners(); + } + + public void insertPathPoint(Point p, int index) + { + Point[] path = getPath(); + if (path == null) + setPath(new Point[] { p }); + else + { + Point[] newPath = new Point[path.length + 1]; + System.arraycopy(path, 0, newPath, 0, index); + if (index < path.length) + System.arraycopy(path, index, newPath, index + 1, path.length - index); + newPath[index] = p; + setPath(newPath); + } + } + + public void removePathPoint(int index) + { + Point[] path = getPath(); + Point[] newPath = new Point[path.length - 1]; + System.arraycopy(path, 0, newPath, 0, index); + if (index < path.length - 1) + System.arraycopy(path, index + 1, newPath, index, path.length - index - 1); + setPath(newPath); + } + + /** + * @throws IndexOutOfBoundsException + */ + public Point getPathPoint(int index) + { + return path[index]; + } + + public int getPathLength() + { + return path.length; + } + + public double[] getEffectivePath() + { + return effectivePath.clone(); + } } \ No newline at end of file