1 package net.mograsim.logic.ui.model.wires;
3 import java.util.ArrayList;
4 import java.util.Arrays;
7 import org.eclipse.swt.SWT;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.core.LogicObserver;
13 import net.mograsim.logic.core.types.BitVectorFormatter;
14 import net.mograsim.logic.core.wires.Wire.ReadEnd;
15 import net.mograsim.logic.ui.ColorHelper;
16 import net.mograsim.logic.ui.model.ViewModelModifiable;
19 * A wire connecting exactly two {@link Pin}s.
21 * @author Daniel Kirschten
26 * The model this wire is a part of.
28 private final ViewModelModifiable model;
30 * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.
32 public final int logicWidth;
34 * The {@link Pin} on one side of this wire, usually the signal source.
38 * The {@link Pin} on one side of this wire, usually the signal target.
42 * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
43 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
48 * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
50 private final Rectangle bounds;
52 * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
54 private double[] effectivePath;
56 private final List<Runnable> redrawListeners;
59 * A LogicObserver calling redrawListeners. Used for logic model bindings.
61 private final LogicObserver logicObs;
63 * A ReadEnd of the logic wire this GUI wire currently is bound to.
67 // creation and destruction
70 * Creates a new {@link GUIWire} with automatic interpolation.
72 * @author Daniel Kirschten
74 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
76 this(model, pin1, pin2, (Point[]) null);
80 * Creates a new {@link GUIWire} with automatic interpolation.
82 * @author Daniel Kirschten
84 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
86 this(model, pin1, pin2, (Point[]) null);
90 * Creates a new {@link GUIWire} with automatic interpolation.
92 * @author Daniel Kirschten
94 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
96 this(model, pin1, pin2, (Point[]) null);
100 * Creates a new {@link GUIWire} with automatic interpolation.
102 * @author Daniel Kirschten
104 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
106 this(model, pin1, pin2, (Point[]) null);
110 * Creates a new {@link GUIWire} without automatic interpolation.
112 * @author Daniel Kirschten
114 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
116 this(model, pin1.getPin(), pin2.getPin(), path);
120 * Creates a new {@link GUIWire} without automatic interpolation.
122 * @author Daniel Kirschten
124 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
126 this(model, pin1.getPin(), pin2, path);
130 * Creates a new {@link GUIWire} without automatic interpolation.
132 * @author Daniel Kirschten
134 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
136 this(model, pin1, pin2.getPin(), path);
140 * Creates a new {@link GUIWire} without automatic interpolation.
142 * @author Daniel Kirschten
144 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
146 logicObs = (i) -> callRedrawListeners();
148 this.logicWidth = pin1.logicWidth;
149 if (pin2.logicWidth != pin1.logicWidth)
150 throw new IllegalArgumentException("Can't connect pins of different logic width");
155 this.path = path == null ? null : Arrays.copyOf(path, path.length);
156 this.bounds = new Rectangle(0, 0, -1, -1);
158 redrawListeners = new ArrayList<>();
160 pin1.addPinMovedListener(p -> pinMoved());
161 pin2.addPinMovedListener(p -> pinMoved());
163 recalculateEffectivePath();
165 model.wireCreated(this);
169 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
170 * this component is a part of.
172 * @author Daniel Kirschten
174 public void destroy()
176 model.wireDestroyed(this);
182 * Returns the {@link Pin} on one side of this wire, usually the signal source.
184 * @author Daniel Kirschten
192 * Returns the {@link Pin} on one side of this wire, usually the signal target.
194 * @author Daniel Kirschten
202 * Called when {@link #pin1} or {@link #pin2} were moved.
204 * @author Daniel Kirschten
206 private void pinMoved()
208 recalculateEffectivePath();
209 callRedrawListeners();
212 // "graphical" operations
215 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
217 * @author Daniel Kirschten
219 private void recalculateEffectivePath()
221 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
223 double boundsX1 = Math.min(pos1.x, pos2.x);
224 double boundsY1 = Math.min(pos1.y, pos2.y);
225 double boundsX2 = Math.max(pos1.x, pos2.x);
226 double boundsY2 = Math.max(pos1.y, pos2.y);
229 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 };
232 effectivePath = new double[path.length * 2 + 4];
233 effectivePath[0] = pos1.x;
234 effectivePath[1] = pos1.y;
235 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
237 double pathX = path[srcI].x;
238 double pathY = path[srcI].y;
239 effectivePath[dstI + 0] = pathX;
240 effectivePath[dstI + 1] = pathY;
241 if (pathX < boundsX1)
243 if (pathX > boundsX2)
245 if (pathY < boundsY1)
247 if (pathY > boundsY2)
250 effectivePath[effectivePath.length - 2] = pos2.x;
251 effectivePath[effectivePath.length - 1] = pos2.y;
256 bounds.width = boundsX2 - boundsX1;
257 bounds.height = boundsY2 - boundsY1;
261 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
263 * @author Daniel Kirschten
265 public Rectangle getBounds()
267 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
271 * Render this wire to the given gc, in absoulute coordinates.
273 * @author Daniel Kirschten
275 public void render(GeneralGC gc)
277 ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath));
281 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
282 * in {@link #render(GeneralGC)}.<br>
283 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
286 * @author Daniel Kirschten
288 public Point[] getPath()
290 return path == null ? null : path.clone();
293 // logic model binding
296 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
297 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
298 * The argument can be null, in which case the old binding is stopped.
300 * @author Daniel Kirschten
302 public void setLogicModelBinding(ReadEnd end)
304 if (this.end != null)
305 this.end.deregisterObserver(logicObs);
308 end.registerObserver(logicObs);
314 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
316 public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);}
318 private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
322 public String toString()
324 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";