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<Runnable> redrawListeners;
65 private final List<Consumer<GUIWire>> pathChangedListeners;
68 * A LogicObserver calling redrawListeners. Used for logic model bindings.
70 private final LogicObserver logicObs;
72 * A ReadEnd of the logic wire this GUI wire currently is bound to.
76 // creation and destruction
79 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
81 * @author Daniel Kirschten
83 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
85 this(model, null, pin1, pin2);
89 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
91 * @author Daniel Kirschten
93 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
95 this(model, null, pin1, pin2);
99 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
101 * @author Daniel Kirschten
103 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
105 this(model, null, pin1, pin2);
109 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
111 * @author Daniel Kirschten
113 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
115 this(model, null, pin1, pin2);
119 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
121 * @author Daniel Kirschten
123 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
125 this(model, null, pin1, pin2, path);
129 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
131 * @author Daniel Kirschten
133 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
135 this(model, null, pin1, pin2, path);
139 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
141 * @author Daniel Kirschten
143 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
145 this(model, null, pin1, pin2, path);
149 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
151 * @author Daniel Kirschten
153 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
155 this(model, null, pin1, pin2, path);
159 * Creates a new {@link GUIWire} with automatic interpolation.
161 * @author Daniel Kirschten
163 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, WireCrossPoint pin2)
165 this(model, name, pin1, pin2, (Point[]) null);
169 * Creates a new {@link GUIWire} with automatic interpolation.
171 * @author Daniel Kirschten
173 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, Pin pin2)
175 this(model, name, pin1, pin2, (Point[]) null);
179 * Creates a new {@link GUIWire} with automatic interpolation.
181 * @author Daniel Kirschten
183 public GUIWire(ViewModelModifiable model, String name, Pin pin1, WireCrossPoint pin2)
185 this(model, name, pin1, pin2, (Point[]) null);
189 * Creates a new {@link GUIWire} with automatic interpolation.
191 * @author Daniel Kirschten
193 public GUIWire(ViewModelModifiable model, String name, Pin pin1, Pin pin2)
195 this(model, name, pin1, pin2, (Point[]) null);
199 * Creates a new {@link GUIWire} without automatic interpolation.
201 * @author Daniel Kirschten
203 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
205 this(model, name, pin1.getPin(), pin2.getPin(), path);
209 * Creates a new {@link GUIWire} without automatic interpolation.
211 * @author Daniel Kirschten
213 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, Pin pin2, Point... path)
215 this(model, name, pin1.getPin(), pin2, path);
219 * Creates a new {@link GUIWire} without automatic interpolation.
221 * @author Daniel Kirschten
223 public GUIWire(ViewModelModifiable model, String name, Pin pin1, WireCrossPoint pin2, Point... path)
225 this(model, name, pin1, pin2.getPin(), path);
229 * Creates a new {@link GUIWire} without automatic interpolation.
231 * @author Daniel Kirschten
233 public GUIWire(ViewModelModifiable model, String name, Pin pin1, Pin pin2, Point... path)
236 this.name = name == null ? model.getDefaultWireName() : name;
237 this.logicWidth = pin1.logicWidth;
238 if (pin2.logicWidth != pin1.logicWidth)
239 throw new IllegalArgumentException("Can't connect pins of different logic width");
244 this.path = path == null ? null : Arrays.copyOf(path, path.length);
245 this.bounds = new Rectangle(0, 0, -1, -1);
247 redrawListeners = new ArrayList<>();
248 pathChangedListeners = new ArrayList<>();
250 logicObs = (i) -> callRedrawListeners();
252 pin1.addPinMovedListener(p -> pinMoved());
253 pin2.addPinMovedListener(p -> pinMoved());
255 recalculateEffectivePath();
257 model.wireCreated(this);
261 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
262 * this component is a part of.
264 * @author Daniel Kirschten
266 public void destroy()
268 model.wireDestroyed(this);
274 * Returns the {@link Pin} on one side of this wire, usually the signal source.
276 * @author Daniel Kirschten
284 * Returns the {@link Pin} on one side of this wire, usually the signal target.
286 * @author Daniel Kirschten
294 * Called when {@link #pin1} or {@link #pin2} were moved.
296 * @author Daniel Kirschten
298 private void pinMoved()
300 recalculateEffectivePath();
301 callRedrawListeners();
304 // "graphical" operations
307 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
309 * @author Daniel Kirschten
311 private void recalculateEffectivePath()
313 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
315 double boundsX1 = Math.min(pos1.x, pos2.x);
316 double boundsY1 = Math.min(pos1.y, pos2.y);
317 double boundsX2 = Math.max(pos1.x, pos2.x);
318 double boundsY2 = Math.max(pos1.y, pos2.y);
321 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 };
324 effectivePath = new double[path.length * 2 + 4];
325 effectivePath[0] = pos1.x;
326 effectivePath[1] = pos1.y;
327 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
329 double pathX = path[srcI].x;
330 double pathY = path[srcI].y;
331 effectivePath[dstI + 0] = pathX;
332 effectivePath[dstI + 1] = pathY;
333 if (pathX < boundsX1)
335 if (pathX > boundsX2)
337 if (pathY < boundsY1)
339 if (pathY > boundsY2)
342 effectivePath[effectivePath.length - 2] = pos2.x;
343 effectivePath[effectivePath.length - 1] = pos2.y;
348 bounds.width = boundsX2 - boundsX1;
349 bounds.height = boundsY2 - boundsY1;
353 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
355 * @author Daniel Kirschten
357 public Rectangle getBounds()
359 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
363 * Render this wire to the given gc, in absoulute coordinates.
365 * @author Daniel Kirschten
367 public void render(GeneralGC gc)
369 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
370 if (wireColor != null)
371 gc.setForeground(ColorManager.current().toColor(wireColor));
372 gc.drawPolyline(effectivePath);
375 // operations concerning the path
378 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
379 * in {@link #render(GeneralGC)}.<br>
380 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
383 * @author Daniel Kirschten
385 public Point[] getPath()
387 return deepPathCopy(path);
390 public void setPath(Point... path)
392 this.path = deepPathCopy(path);
393 recalculateEffectivePath();
394 callPathChangedListeners();
395 callRedrawListeners();
398 public Point getPathPoint(int index)
400 return pointCopy(path[index]);
403 public void setPathPoint(Point p, int index)
405 path[index] = pointCopy(p);
406 recalculateEffectivePath();
407 callPathChangedListeners();
408 callRedrawListeners();
411 public void insertPathPoint(Point p, int index)
414 path = new Point[] { pointCopy(p) };
417 Point[] oldPath = path;
418 path = new Point[oldPath.length + 1];
419 System.arraycopy(oldPath, 0, path, 0, index);
420 if (index < oldPath.length)
421 System.arraycopy(oldPath, index, path, index + 1, oldPath.length - index);
422 path[index] = pointCopy(p);
426 public void removePathPoint(int index)
428 if (path.length == 0)
432 Point[] oldPath = path;
433 path = new Point[oldPath.length - 1];
434 System.arraycopy(oldPath, 0, path, 0, index);
435 if (index < oldPath.length - 1)
436 System.arraycopy(oldPath, index + 1, path, index, oldPath.length - index - 1);
440 public double[] getEffectivePath()
442 return Arrays.copyOf(effectivePath, effectivePath.length);
445 private static Point[] deepPathCopy(Point[] path)
449 Point[] copy = new Point[path.length];
450 for (int i = 0; i < path.length; i++)
451 copy[i] = pointCopy(path[i]);
455 private static Point pointCopy(Point p)
457 return new Point(p.x, p.y);
460 // logic model binding
463 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
464 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
465 * The argument can be null, in which case the old binding is stopped.
467 * @author Daniel Kirschten
469 public void setLogicModelBinding(ReadEnd end)
471 if (this.end != null)
472 this.end.deregisterObserver(logicObs);
475 end.registerObserver(logicObs);
479 * Returns whether this {@link GUIWire} has a logic model binding or not.
481 * @author Daniel Kirschten
483 public boolean hasLogicModelBinding()
489 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
490 * corresponding to this {@link GUIWire}.
492 * @author Daniel Kirschten
494 public void forceWireValues(BitVector values)
496 end.getWire().forceValues(values);
500 * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
501 * this {@link GUIWire}.
503 * @author Daniel Kirschten
505 public BitVector getWireValues()
507 return end.getValues();
513 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
514 public void addPathChangedListener (Consumer<GUIWire> listener) {pathChangedListeners.add (listener);}
516 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
517 public void removePathChangedListener(Consumer<GUIWire> listener) {pathChangedListeners.remove(listener);}
519 private void callRedrawListeners () {redrawListeners .forEach(l -> l.run ( ));}
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()) + "]";