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;
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.
*/
public class GUIWire
{
+
+ private final Set<PathChangedListener> pathChangedListeners;
+
/**
* The model this wire is a part of.
*/
/**
* 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<Runnable> redrawListeners;
*/
GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
{
+ pathChangedListeners = new HashSet<>();
logicObs = (i) -> callRedrawListeners();
this.model = model;
this.logicWidth = pin1.logicWidth;
*/
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);
}
/**
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
{
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