1 package net.mograsim.logic.model.model.wires;
3 import java.util.ArrayList;
4 import java.util.Arrays;
6 import java.util.function.Consumer;
8 import org.eclipse.swt.SWT;
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.core.LogicObserver;
14 import net.mograsim.logic.core.types.BitVector;
15 import net.mograsim.logic.core.types.BitVectorFormatter;
16 import net.mograsim.logic.core.wires.Wire;
17 import net.mograsim.logic.core.wires.Wire.ReadEnd;
18 import net.mograsim.logic.model.model.ViewModelModifiable;
19 import net.mograsim.preferences.ColorDefinition;
20 import net.mograsim.preferences.ColorManager;
23 * A wire connecting exactly two {@link Pin}s.
25 * @author Daniel Kirschten
30 * The model this wire is a part of.
32 private final ViewModelModifiable model;
34 * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.
36 public final int logicWidth;
38 * The {@link Pin} on one side of this wire, usually the signal source.
42 * The {@link Pin} on one side of this wire, usually the signal target.
46 * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
47 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
52 * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
54 private final Rectangle bounds;
56 * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
58 private double[] effectivePath;
60 private final List<Runnable> redrawListeners;
61 private final List<Consumer<GUIWire>> pathChangedListeners;
64 * A LogicObserver calling redrawListeners. Used for logic model bindings.
66 private final LogicObserver logicObs;
68 * A ReadEnd of the logic wire this GUI wire currently is bound to.
72 // creation and destruction
75 * Creates a new {@link GUIWire} with automatic interpolation.
77 * @author Daniel Kirschten
79 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
81 this(model, pin1, pin2, (Point[]) null);
85 * Creates a new {@link GUIWire} with automatic interpolation.
87 * @author Daniel Kirschten
89 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
91 this(model, pin1, pin2, (Point[]) null);
95 * Creates a new {@link GUIWire} with automatic interpolation.
97 * @author Daniel Kirschten
99 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
101 this(model, pin1, pin2, (Point[]) null);
105 * Creates a new {@link GUIWire} with automatic interpolation.
107 * @author Daniel Kirschten
109 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
111 this(model, pin1, pin2, (Point[]) null);
115 * Creates a new {@link GUIWire} without automatic interpolation.
117 * @author Daniel Kirschten
119 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
121 this(model, pin1.getPin(), pin2.getPin(), path);
125 * Creates a new {@link GUIWire} without automatic interpolation.
127 * @author Daniel Kirschten
129 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
131 this(model, pin1.getPin(), pin2, path);
135 * Creates a new {@link GUIWire} without automatic interpolation.
137 * @author Daniel Kirschten
139 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
141 this(model, pin1, pin2.getPin(), path);
145 * Creates a new {@link GUIWire} without automatic interpolation.
147 * @author Daniel Kirschten
149 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
152 this.logicWidth = pin1.logicWidth;
153 if (pin2.logicWidth != pin1.logicWidth)
154 throw new IllegalArgumentException("Can't connect pins of different logic width");
159 this.path = path == null ? null : Arrays.copyOf(path, path.length);
160 this.bounds = new Rectangle(0, 0, -1, -1);
162 redrawListeners = new ArrayList<>();
163 pathChangedListeners = new ArrayList<>();
165 logicObs = (i) -> callRedrawListeners();
167 pin1.addPinMovedListener(p -> pinMoved());
168 pin2.addPinMovedListener(p -> pinMoved());
170 recalculateEffectivePath();
172 model.wireCreated(this);
176 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
177 * this component is a part of.
179 * @author Daniel Kirschten
181 public void destroy()
183 model.wireDestroyed(this);
189 * Returns the {@link Pin} on one side of this wire, usually the signal source.
191 * @author Daniel Kirschten
199 * Returns the {@link Pin} on one side of this wire, usually the signal target.
201 * @author Daniel Kirschten
209 * Called when {@link #pin1} or {@link #pin2} were moved.
211 * @author Daniel Kirschten
213 private void pinMoved()
215 recalculateEffectivePath();
216 callRedrawListeners();
219 // "graphical" operations
222 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
224 * @author Daniel Kirschten
226 private void recalculateEffectivePath()
228 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
230 double boundsX1 = Math.min(pos1.x, pos2.x);
231 double boundsY1 = Math.min(pos1.y, pos2.y);
232 double boundsX2 = Math.max(pos1.x, pos2.x);
233 double boundsY2 = Math.max(pos1.y, pos2.y);
236 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 };
239 effectivePath = new double[path.length * 2 + 4];
240 effectivePath[0] = pos1.x;
241 effectivePath[1] = pos1.y;
242 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
244 double pathX = path[srcI].x;
245 double pathY = path[srcI].y;
246 effectivePath[dstI + 0] = pathX;
247 effectivePath[dstI + 1] = pathY;
248 if (pathX < boundsX1)
250 if (pathX > boundsX2)
252 if (pathY < boundsY1)
254 if (pathY > boundsY2)
257 effectivePath[effectivePath.length - 2] = pos2.x;
258 effectivePath[effectivePath.length - 1] = pos2.y;
263 bounds.width = boundsX2 - boundsX1;
264 bounds.height = boundsY2 - boundsY1;
268 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
270 * @author Daniel Kirschten
272 public Rectangle getBounds()
274 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
278 * Render this wire to the given gc, in absoulute coordinates.
280 * @author Daniel Kirschten
282 public void render(GeneralGC gc)
284 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
285 if (wireColor != null)
286 gc.setForeground(ColorManager.current().toColor(wireColor));
287 gc.drawPolyline(effectivePath);
290 // operations concerning the path
293 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
294 * in {@link #render(GeneralGC)}.<br>
295 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
298 * @author Daniel Kirschten
300 public Point[] getPath()
302 return deepPathCopy(path);
305 public void setPath(Point... path)
307 this.path = deepPathCopy(path);
308 recalculateEffectivePath();
309 callPathChangedListeners();
310 callRedrawListeners();
313 public Point getPathPoint(int index)
315 return pointCopy(path[index]);
318 public void setPathPoint(Point p, int index)
320 path[index] = pointCopy(p);
321 recalculateEffectivePath();
322 callPathChangedListeners();
323 callRedrawListeners();
326 public void insertPathPoint(Point p, int index)
329 path = new Point[] { pointCopy(p) };
332 Point[] oldPath = path;
333 path = new Point[oldPath.length + 1];
334 System.arraycopy(oldPath, 0, path, 0, index);
335 if (index < oldPath.length)
336 System.arraycopy(oldPath, index, path, index + 1, oldPath.length - index);
337 path[index] = pointCopy(p);
341 public void removePathPoint(int index)
343 if (path.length == 0)
347 Point[] oldPath = path;
348 path = new Point[oldPath.length - 1];
349 System.arraycopy(oldPath, 0, path, 0, index);
350 if (index < oldPath.length - 1)
351 System.arraycopy(oldPath, index + 1, path, index, oldPath.length - index - 1);
355 public double[] getEffectivePath()
357 return Arrays.copyOf(effectivePath, effectivePath.length);
360 private static Point[] deepPathCopy(Point[] path)
364 Point[] copy = new Point[path.length];
365 for (int i = 0; i < path.length; i++)
366 copy[i] = pointCopy(path[i]);
370 private static Point pointCopy(Point p)
372 return new Point(p.x, p.y);
375 // logic model binding
378 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
379 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
380 * The argument can be null, in which case the old binding is stopped.
382 * @author Daniel Kirschten
384 public void setLogicModelBinding(ReadEnd end)
386 if (this.end != null)
387 this.end.deregisterObserver(logicObs);
390 end.registerObserver(logicObs);
394 * Returns whether this {@link GUIWire} has a logic model binding or not.
396 * @author Daniel Kirschten
398 public boolean hasLogicModelBinding()
404 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
405 * corresponding to this {@link GUIWire}.
407 * @author Daniel Kirschten
409 public void forceWireValues(BitVector values)
411 end.getWire().forceValues(values);
415 * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
416 * this {@link GUIWire}.
418 * @author Daniel Kirschten
420 public BitVector getWireValues()
422 return end.getValues();
428 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
429 public void addPathChangedListener (Consumer<GUIWire> listener) {pathChangedListeners.add (listener);}
431 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
432 public void removePathChangedListener(Consumer<GUIWire> listener) {pathChangedListeners.remove(listener);}
434 private void callRedrawListeners () {redrawListeners .forEach(l -> l.run ( ));}
435 private void callPathChangedListeners() {pathChangedListeners.forEach(l -> l.accept(this));}
439 public String toString()
441 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";