X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2Fwires%2FGUIWire.java;h=7146d63166189dca4348eb7d9208b23efa2c69d3;hb=cc6e329dc014542eb743833ef865b9d59047abbb;hp=c1070e53267de7cb6f6f7c035912b6aff026848b;hpb=1ddc8ecf420fd4ccbe351c0cbd58cc9f5792f0c7;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/wires/GUIWire.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/wires/GUIWire.java index c1070e53..7146d631 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/wires/GUIWire.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/wires/GUIWire.java @@ -3,6 +3,7 @@ package net.mograsim.logic.model.model.wires; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.Consumer; import org.eclipse.swt.SWT; @@ -57,6 +58,7 @@ public class GUIWire private double[] effectivePath; private final List redrawListeners; + private final List> pathChangedListeners; /** * A LogicObserver calling redrawListeners. Used for logic model bindings. @@ -146,7 +148,6 @@ public class GUIWire */ 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) @@ -159,6 +160,9 @@ public class GUIWire this.bounds = new Rectangle(0, 0, -1, -1); redrawListeners = new ArrayList<>(); + pathChangedListeners = new ArrayList<>(); + + logicObs = (i) -> callRedrawListeners(); pin1.addPinMovedListener(p -> pinMoved()); pin2.addPinMovedListener(p -> pinMoved()); @@ -283,6 +287,8 @@ public class GUIWire gc.drawPolyline(effectivePath); } + // operations concerning the path + /** * 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)}.
@@ -293,7 +299,77 @@ public class GUIWire */ public Point[] getPath() { - return path == null ? null : path.clone(); + return deepPathCopy(path); + } + + public void setPath(Point... path) + { + this.path = deepPathCopy(path); + recalculateEffectivePath(); + callPathChangedListeners(); + callRedrawListeners(); + } + + public Point getPathPoint(int index) + { + return pointCopy(path[index]); + } + + public void setPathPoint(Point p, int index) + { + path[index] = pointCopy(p); + recalculateEffectivePath(); + callPathChangedListeners(); + callRedrawListeners(); + } + + public void insertPathPoint(Point p, int index) + { + if (path == null) + path = new Point[] { pointCopy(p) }; + else + { + Point[] oldPath = path; + path = new Point[oldPath.length + 1]; + System.arraycopy(oldPath, 0, path, 0, index); + if (index < oldPath.length) + System.arraycopy(oldPath, index, path, index + 1, oldPath.length - index); + path[index] = pointCopy(p); + } + } + + public void removePathPoint(int index) + { + if (path.length == 0) + path = null; + else + { + Point[] oldPath = path; + path = new Point[oldPath.length - 1]; + System.arraycopy(oldPath, 0, path, 0, index); + if (index < oldPath.length - 1) + System.arraycopy(oldPath, index + 1, path, index, oldPath.length - index - 1); + } + } + + public double[] getEffectivePath() + { + return Arrays.copyOf(effectivePath, effectivePath.length); + } + + private static Point[] deepPathCopy(Point[] path) + { + if (path == null) + return null; + Point[] copy = new Point[path.length]; + for (int i = 0; i < path.length; i++) + copy[i] = pointCopy(path[i]); + return copy; + } + + private static Point pointCopy(Point p) + { + return new Point(p.x, p.y); } // logic model binding @@ -349,11 +425,14 @@ public class GUIWire // listeners // @formatter:off - public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);} + public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);} + public void addPathChangedListener (Consumer listener) {pathChangedListeners.add (listener);} - public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);} + public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);} + public void removePathChangedListener(Consumer listener) {pathChangedListeners.remove(listener);} - private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());} + private void callRedrawListeners () {redrawListeners .forEach(l -> l.run ( ));} + private void callPathChangedListeners() {pathChangedListeners.forEach(l -> l.accept(this));} // @formatter:on @Override