1 package net.mograsim.logic.model.model.wires;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashSet;
9 import org.eclipse.swt.SWT;
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
14 import net.mograsim.logic.core.LogicObserver;
15 import net.mograsim.logic.core.types.BitVector;
16 import net.mograsim.logic.core.types.BitVectorFormatter;
17 import net.mograsim.logic.core.wires.Wire;
18 import net.mograsim.logic.core.wires.Wire.ReadEnd;
19 import net.mograsim.logic.model.model.ViewModelModifiable;
20 import net.mograsim.preferences.ColorDefinition;
21 import net.mograsim.preferences.ColorManager;
24 * A wire connecting exactly two {@link Pin}s.
26 * @author Daniel Kirschten
31 * The model this wire is a part of.
33 private final ViewModelModifiable model;
35 * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.
37 public final int logicWidth;
39 * The {@link Pin} on one side of this wire, usually the signal source.
43 * The {@link Pin} on one side of this wire, usually the signal target.
47 * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
48 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
53 * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
55 private final Rectangle bounds;
57 * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
59 private double[] effectivePath;
61 private final List<Runnable> redrawListeners;
63 private final Set<PathChangedListener> pathChangedListeners;
66 * A LogicObserver calling redrawListeners. Used for logic model bindings.
68 private final LogicObserver logicObs;
70 * A ReadEnd of the logic wire this GUI wire currently is bound to.
74 // creation and destruction
77 * Creates a new {@link GUIWire} with automatic interpolation.
79 * @author Daniel Kirschten
81 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
83 this(model, pin1, pin2, (Point[]) null);
87 * Creates a new {@link GUIWire} with automatic interpolation.
89 * @author Daniel Kirschten
91 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
93 this(model, pin1, pin2, (Point[]) null);
97 * Creates a new {@link GUIWire} with automatic interpolation.
99 * @author Daniel Kirschten
101 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
103 this(model, pin1, pin2, (Point[]) null);
107 * Creates a new {@link GUIWire} with automatic interpolation.
109 * @author Daniel Kirschten
111 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
113 this(model, pin1, pin2, (Point[]) null);
117 * Creates a new {@link GUIWire} without automatic interpolation.
119 * @author Daniel Kirschten
121 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
123 this(model, pin1.getPin(), pin2.getPin(), path);
127 * Creates a new {@link GUIWire} without automatic interpolation.
129 * @author Daniel Kirschten
131 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
133 this(model, pin1.getPin(), pin2, path);
137 * Creates a new {@link GUIWire} without automatic interpolation.
139 * @author Daniel Kirschten
141 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
143 this(model, pin1, pin2.getPin(), path);
147 * Creates a new {@link GUIWire} without automatic interpolation.
149 * @author Daniel Kirschten
151 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
153 pathChangedListeners = new HashSet<>();
154 logicObs = (i) -> callRedrawListeners();
156 this.logicWidth = pin1.logicWidth;
157 if (pin2.logicWidth != pin1.logicWidth)
158 throw new IllegalArgumentException("Can't connect pins of different logic width");
163 this.path = path == null ? null : Arrays.copyOf(path, path.length);
164 this.bounds = new Rectangle(0, 0, -1, -1);
166 redrawListeners = new ArrayList<>();
168 pin1.addPinMovedListener(p -> pinMoved());
169 pin2.addPinMovedListener(p -> pinMoved());
171 recalculateEffectivePath();
173 model.wireCreated(this);
177 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
178 * this component is a part of.
180 * @author Daniel Kirschten
182 public void destroy()
184 model.wireDestroyed(this);
190 * Returns the {@link Pin} on one side of this wire, usually the signal source.
192 * @author Daniel Kirschten
200 * Returns the {@link Pin} on one side of this wire, usually the signal target.
202 * @author Daniel Kirschten
210 * Called when {@link #pin1} or {@link #pin2} were moved.
212 * @author Daniel Kirschten
214 private void pinMoved()
216 recalculateEffectivePath();
217 callRedrawListeners();
220 // "graphical" operations
223 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
225 * @author Daniel Kirschten
227 private void recalculateEffectivePath()
229 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
231 double boundsX1 = Math.min(pos1.x, pos2.x);
232 double boundsY1 = Math.min(pos1.y, pos2.y);
233 double boundsX2 = Math.max(pos1.x, pos2.x);
234 double boundsY2 = Math.max(pos1.y, pos2.y);
237 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 };
240 effectivePath = new double[path.length * 2 + 4];
241 effectivePath[0] = pos1.x;
242 effectivePath[1] = pos1.y;
243 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
245 double pathX = path[srcI].x;
246 double pathY = path[srcI].y;
247 effectivePath[dstI + 0] = pathX;
248 effectivePath[dstI + 1] = pathY;
249 if (pathX < boundsX1)
251 if (pathX > boundsX2)
253 if (pathY < boundsY1)
255 if (pathY > boundsY2)
258 effectivePath[effectivePath.length - 2] = pos2.x;
259 effectivePath[effectivePath.length - 1] = pos2.y;
264 bounds.width = boundsX2 - boundsX1;
265 bounds.height = boundsY2 - boundsY1;
269 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
271 * @author Daniel Kirschten
273 public Rectangle getBounds()
275 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
279 * Render this wire to the given gc, in absoulute coordinates.
281 * @author Daniel Kirschten
283 public void render(GeneralGC gc)
285 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
286 if (wireColor != null)
287 gc.setForeground(ColorManager.current().toColor(wireColor));
288 gc.drawPolyline(effectivePath);
292 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
293 * in {@link #render(GeneralGC)}.<br>
294 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
297 * @author Daniel Kirschten
299 public Point[] getPath()
301 return path == null ? null : path.clone();
304 // logic model binding
307 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
308 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
309 * The argument can be null, in which case the old binding is stopped.
311 * @author Daniel Kirschten
313 public void setLogicModelBinding(ReadEnd end)
315 if (this.end != null)
316 this.end.deregisterObserver(logicObs);
319 end.registerObserver(logicObs);
323 * Returns whether this {@link GUIWire} has a logic model binding or not.
325 * @author Daniel Kirschten
327 public boolean hasLogicModelBinding()
333 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
334 * corresponding to this {@link GUIWire}.
336 * @author Daniel Kirschten
338 public void forceWireValues(BitVector values)
340 end.getWire().forceValues(values);
344 * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
345 * this {@link GUIWire}.
347 * @author Daniel Kirschten
349 public BitVector getWireValues()
351 return end.getValues();
357 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
359 public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);}
361 private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
363 public void addPathChangedListener(PathChangedListener l) { pathChangedListeners.add(l); }
365 public void removePathChangedListener(PathChangedListener l) { pathChangedListeners.remove(l); }
367 public void callPathChangedListeners(int diff) { pathChangedListeners.forEach(l -> l.pathChanged(this, diff)); }
370 public static interface PathChangedListener
373 * Called whenever the {@link Wire}'s path changes
375 * @param wire The wire which had its path changed
376 * @param diff The length difference between before and after the path change.
378 public void pathChanged(GUIWire wire, int diff);
383 public String toString()
385 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";
388 public void setPath(Point[] path)
390 int diff = (path == null ? 0 : path.length) - (this.path == null ? 0 : this.path.length);
391 this.path = path == null ? null : path.clone();
392 recalculateEffectivePath();
393 callPathChangedListeners(diff);
394 callRedrawListeners();
397 public void setPathPoint(Point p, int index)
400 recalculateEffectivePath();
401 callPathChangedListeners(0);
402 callRedrawListeners();
405 public void insertPathPoint(Point p, int index)
407 Point[] path = getPath();
409 setPath(new Point[] { p });
412 Point[] newPath = new Point[path.length + 1];
413 System.arraycopy(path, 0, newPath, 0, index);
414 if (index < path.length)
415 System.arraycopy(path, index, newPath, index + 1, path.length - index);
421 public void removePathPoint(int index)
423 Point[] path = getPath();
424 Point[] newPath = new Point[path.length - 1];
425 System.arraycopy(path, 0, newPath, 0, index);
426 if (index < path.length - 1)
427 System.arraycopy(path, index + 1, newPath, index, path.length - index - 1);
432 * @throws IndexOutOfBoundsException
434 public Point getPathPoint(int index)
439 public int getPathLength()
444 public double[] getEffectivePath()
446 return effectivePath.clone();