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 name of this wire. Is unique for all wires in its model.
36 public final String name;
38 * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.
40 public final int logicWidth;
42 * The {@link Pin} on one side of this wire, usually the signal source.
46 * The {@link Pin} on one side of this wire, usually the signal target.
50 * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
51 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
56 * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
58 private final Rectangle bounds;
60 * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
62 private double[] effectivePath;
64 private final List<Consumer<GUIWire>> pathChangedListeners;
67 * A LogicObserver calling redrawListeners. Used for logic model bindings.
69 private final LogicObserver logicObs;
71 * A ReadEnd of the logic wire this GUI wire currently is bound to.
75 // creation and destruction
78 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
80 * @author Daniel Kirschten
82 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
84 this(model, null, pin1, pin2);
88 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
90 * @author Daniel Kirschten
92 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
94 this(model, null, pin1, pin2);
98 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
100 * @author Daniel Kirschten
102 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
104 this(model, null, pin1, pin2);
108 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
110 * @author Daniel Kirschten
112 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
114 this(model, null, pin1, pin2);
118 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
120 * @author Daniel Kirschten
122 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
124 this(model, null, pin1, pin2, path);
128 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
130 * @author Daniel Kirschten
132 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
134 this(model, null, pin1, pin2, path);
138 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
140 * @author Daniel Kirschten
142 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
144 this(model, null, pin1, pin2, path);
148 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
150 * @author Daniel Kirschten
152 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
154 this(model, null, pin1, pin2, path);
158 * Creates a new {@link GUIWire} with automatic interpolation.
160 * @author Daniel Kirschten
162 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, WireCrossPoint pin2)
164 this(model, name, pin1, pin2, (Point[]) null);
168 * Creates a new {@link GUIWire} with automatic interpolation.
170 * @author Daniel Kirschten
172 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, Pin pin2)
174 this(model, name, pin1, pin2, (Point[]) null);
178 * Creates a new {@link GUIWire} with automatic interpolation.
180 * @author Daniel Kirschten
182 public GUIWire(ViewModelModifiable model, String name, Pin pin1, WireCrossPoint pin2)
184 this(model, name, pin1, pin2, (Point[]) null);
188 * Creates a new {@link GUIWire} with automatic interpolation.
190 * @author Daniel Kirschten
192 public GUIWire(ViewModelModifiable model, String name, Pin pin1, Pin pin2)
194 this(model, name, pin1, pin2, (Point[]) null);
198 * Creates a new {@link GUIWire} without automatic interpolation.
200 * @author Daniel Kirschten
202 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
204 this(model, name, pin1.getPin(), pin2.getPin(), path);
208 * Creates a new {@link GUIWire} without automatic interpolation.
210 * @author Daniel Kirschten
212 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, Pin pin2, Point... path)
214 this(model, name, pin1.getPin(), pin2, path);
218 * Creates a new {@link GUIWire} without automatic interpolation.
220 * @author Daniel Kirschten
222 public GUIWire(ViewModelModifiable model, String name, Pin pin1, WireCrossPoint pin2, Point... path)
224 this(model, name, pin1, pin2.getPin(), path);
228 * Creates a new {@link GUIWire} without automatic interpolation.
230 * @author Daniel Kirschten
232 public GUIWire(ViewModelModifiable model, String name, Pin pin1, Pin pin2, Point... path)
235 this.name = name == null ? model.getDefaultWireName() : name;
236 this.logicWidth = pin1.logicWidth;
237 if (pin2.logicWidth != pin1.logicWidth)
238 throw new IllegalArgumentException("Can't connect pins of different logic width");
243 this.path = path == null ? null : Arrays.copyOf(path, path.length);
244 this.bounds = new Rectangle(0, 0, -1, -1);
246 pathChangedListeners = new ArrayList<>();
248 logicObs = (i) -> model.requestRedraw();
250 pin1.addPinMovedListener(p -> pinMoved());
251 pin2.addPinMovedListener(p -> pinMoved());
253 recalculateEffectivePath();
255 model.wireCreated(this);
259 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
260 * this component is a part of.
262 * @author Daniel Kirschten
264 public void destroy()
266 model.wireDestroyed(this);
272 * Returns the {@link Pin} on one side of this wire, usually the signal source.
274 * @author Daniel Kirschten
282 * Returns the {@link Pin} on one side of this wire, usually the signal target.
284 * @author Daniel Kirschten
292 * Called when {@link #pin1} or {@link #pin2} were moved.
294 * @author Daniel Kirschten
296 private void pinMoved()
298 recalculateEffectivePath();
299 model.requestRedraw();
302 // "graphical" operations
305 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
307 * @author Daniel Kirschten
309 private void recalculateEffectivePath()
311 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
313 double boundsX1 = Math.min(pos1.x, pos2.x);
314 double boundsY1 = Math.min(pos1.y, pos2.y);
315 double boundsX2 = Math.max(pos1.x, pos2.x);
316 double boundsY2 = Math.max(pos1.y, pos2.y);
319 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 };
322 effectivePath = new double[path.length * 2 + 4];
323 effectivePath[0] = pos1.x;
324 effectivePath[1] = pos1.y;
325 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
327 double pathX = path[srcI].x;
328 double pathY = path[srcI].y;
329 effectivePath[dstI + 0] = pathX;
330 effectivePath[dstI + 1] = pathY;
331 if (pathX < boundsX1)
333 if (pathX > boundsX2)
335 if (pathY < boundsY1)
337 if (pathY > boundsY2)
340 effectivePath[effectivePath.length - 2] = pos2.x;
341 effectivePath[effectivePath.length - 1] = pos2.y;
346 bounds.width = boundsX2 - boundsX1;
347 bounds.height = boundsY2 - boundsY1;
351 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
353 * @author Daniel Kirschten
355 public Rectangle getBounds()
357 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
361 * Render this wire to the given gc, in absoulute coordinates.
363 * @author Daniel Kirschten
365 public void render(GeneralGC gc)
367 // TODO maybe make wires with logicWidth!=1 thicker? Maybe make thickness selectable?
368 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
369 if (wireColor != null)
370 gc.setForeground(ColorManager.current().toColor(wireColor));
371 gc.drawPolyline(effectivePath);
374 // operations concerning the path
377 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
378 * in {@link #render(GeneralGC)}.<br>
379 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
382 * @author Daniel Kirschten
384 public Point[] getPath()
386 return deepPathCopy(path);
389 public void setPath(Point... path)
391 this.path = deepPathCopy(path);
392 recalculateEffectivePath();
393 callPathChangedListeners();
394 model.requestRedraw();
397 public Point getPathPoint(int index)
399 return pointCopy(path[index]);
402 public void setPathPoint(Point p, int index)
404 path[index] = pointCopy(p);
405 recalculateEffectivePath();
406 callPathChangedListeners();
407 model.requestRedraw();
410 public void insertPathPoint(Point p, int index)
413 path = new Point[] { pointCopy(p) };
416 Point[] oldPath = path;
417 path = new Point[oldPath.length + 1];
418 System.arraycopy(oldPath, 0, path, 0, index);
419 if (index < oldPath.length)
420 System.arraycopy(oldPath, index, path, index + 1, oldPath.length - index);
421 path[index] = pointCopy(p);
423 recalculateEffectivePath();
424 callPathChangedListeners();
427 public void removePathPoint(int index)
429 if (path.length == 0)
433 Point[] oldPath = path;
434 path = new Point[oldPath.length - 1];
435 System.arraycopy(oldPath, 0, path, 0, index);
436 if (index < oldPath.length - 1)
437 System.arraycopy(oldPath, index + 1, path, index, oldPath.length - index - 1);
439 recalculateEffectivePath();
440 callPathChangedListeners();
443 public double[] getEffectivePath()
445 return Arrays.copyOf(effectivePath, effectivePath.length);
448 private static Point[] deepPathCopy(Point[] path)
452 Point[] copy = new Point[path.length];
453 for (int i = 0; i < path.length; i++)
454 copy[i] = pointCopy(path[i]);
458 private static Point pointCopy(Point p)
460 return new Point(p.x, p.y);
463 // logic model binding
466 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
467 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
468 * The argument can be null, in which case the old binding is stopped.
470 * @author Daniel Kirschten
472 public void setLogicModelBinding(ReadEnd end)
474 if (this.end != null)
475 this.end.deregisterObserver(logicObs);
478 end.registerObserver(logicObs);
482 * Returns whether this {@link GUIWire} has a logic model binding or not.
484 * @author Daniel Kirschten
486 public boolean hasLogicModelBinding()
492 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
493 * corresponding to this {@link GUIWire}.
495 * @author Daniel Kirschten
497 public void forceWireValues(BitVector values)
499 end.getWire().forceValues(values);
503 * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
504 * this {@link GUIWire}.
506 * @author Daniel Kirschten
508 public BitVector getWireValues()
510 return end.getValues();
516 public void addPathChangedListener (Consumer<GUIWire> listener) {pathChangedListeners.add (listener);}
518 public void removePathChangedListener(Consumer<GUIWire> listener) {pathChangedListeners.remove(listener);}
520 private void callPathChangedListeners() {pathChangedListeners.forEach(l -> l.accept(this));}
524 public String toString()
526 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";