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.BitVector;
14 import net.mograsim.logic.core.types.BitVectorFormatter;
15 import net.mograsim.logic.core.wires.Wire;
16 import net.mograsim.logic.core.wires.Wire.ReadEnd;
17 import net.mograsim.logic.ui.ColorHelper;
18 import net.mograsim.logic.ui.model.ViewModelModifiable;
21 * A wire connecting exactly two {@link Pin}s.
23 * @author Daniel Kirschten
28 * The model this wire is a part of.
30 private final ViewModelModifiable model;
32 * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.
34 public final int logicWidth;
36 * The {@link Pin} on one side of this wire, usually the signal source.
40 * The {@link Pin} on one side of this wire, usually the signal target.
44 * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
45 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
50 * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
52 private final Rectangle bounds;
54 * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
56 private double[] effectivePath;
58 private final List<Runnable> redrawListeners;
61 * A LogicObserver calling redrawListeners. Used for logic model bindings.
63 private final LogicObserver logicObs;
65 * A ReadEnd of the logic wire this GUI wire currently is bound to.
69 // creation and destruction
72 * Creates a new {@link GUIWire} with automatic interpolation.
74 * @author Daniel Kirschten
76 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
78 this(model, pin1, pin2, (Point[]) null);
82 * Creates a new {@link GUIWire} with automatic interpolation.
84 * @author Daniel Kirschten
86 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
88 this(model, pin1, pin2, (Point[]) null);
92 * Creates a new {@link GUIWire} with automatic interpolation.
94 * @author Daniel Kirschten
96 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
98 this(model, pin1, pin2, (Point[]) null);
102 * Creates a new {@link GUIWire} with automatic interpolation.
104 * @author Daniel Kirschten
106 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
108 this(model, pin1, pin2, (Point[]) null);
112 * Creates a new {@link GUIWire} without automatic interpolation.
114 * @author Daniel Kirschten
116 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
118 this(model, pin1.getPin(), pin2.getPin(), path);
122 * Creates a new {@link GUIWire} without automatic interpolation.
124 * @author Daniel Kirschten
126 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
128 this(model, pin1.getPin(), pin2, path);
132 * Creates a new {@link GUIWire} without automatic interpolation.
134 * @author Daniel Kirschten
136 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
138 this(model, pin1, pin2.getPin(), path);
142 * Creates a new {@link GUIWire} without automatic interpolation.
144 * @author Daniel Kirschten
146 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
148 logicObs = (i) -> callRedrawListeners();
150 this.logicWidth = pin1.logicWidth;
151 if (pin2.logicWidth != pin1.logicWidth)
152 throw new IllegalArgumentException("Can't connect pins of different logic width");
157 this.path = path == null ? null : Arrays.copyOf(path, path.length);
158 this.bounds = new Rectangle(0, 0, -1, -1);
160 redrawListeners = new ArrayList<>();
162 pin1.addPinMovedListener(p -> pinMoved());
163 pin2.addPinMovedListener(p -> pinMoved());
165 recalculateEffectivePath();
167 model.wireCreated(this);
171 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
172 * this component is a part of.
174 * @author Daniel Kirschten
176 public void destroy()
178 model.wireDestroyed(this);
184 * Returns the {@link Pin} on one side of this wire, usually the signal source.
186 * @author Daniel Kirschten
194 * Returns the {@link Pin} on one side of this wire, usually the signal target.
196 * @author Daniel Kirschten
204 * Called when {@link #pin1} or {@link #pin2} were moved.
206 * @author Daniel Kirschten
208 private void pinMoved()
210 recalculateEffectivePath();
211 callRedrawListeners();
214 // "graphical" operations
217 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
219 * @author Daniel Kirschten
221 private void recalculateEffectivePath()
223 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
225 double boundsX1 = Math.min(pos1.x, pos2.x);
226 double boundsY1 = Math.min(pos1.y, pos2.y);
227 double boundsX2 = Math.max(pos1.x, pos2.x);
228 double boundsY2 = Math.max(pos1.y, pos2.y);
231 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 };
234 effectivePath = new double[path.length * 2 + 4];
235 effectivePath[0] = pos1.x;
236 effectivePath[1] = pos1.y;
237 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
239 double pathX = path[srcI].x;
240 double pathY = path[srcI].y;
241 effectivePath[dstI + 0] = pathX;
242 effectivePath[dstI + 1] = pathY;
243 if (pathX < boundsX1)
245 if (pathX > boundsX2)
247 if (pathY < boundsY1)
249 if (pathY > boundsY2)
252 effectivePath[effectivePath.length - 2] = pos2.x;
253 effectivePath[effectivePath.length - 1] = pos2.y;
258 bounds.width = boundsX2 - boundsX1;
259 bounds.height = boundsY2 - boundsY1;
263 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
265 * @author Daniel Kirschten
267 public Rectangle getBounds()
269 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
273 * Render this wire to the given gc, in absoulute coordinates.
275 * @author Daniel Kirschten
277 public void render(GeneralGC gc)
279 ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath));
283 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
284 * in {@link #render(GeneralGC)}.<br>
285 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
288 * @author Daniel Kirschten
290 public Point[] getPath()
292 return path == null ? null : path.clone();
295 // logic model binding
298 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
299 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
300 * The argument can be null, in which case the old binding is stopped.
302 * @author Daniel Kirschten
304 public void setLogicModelBinding(ReadEnd end)
306 if (this.end != null)
307 this.end.deregisterObserver(logicObs);
310 end.registerObserver(logicObs);
314 * Returns whether this {@link GUIWire} has a logic model binding or not.
316 public boolean hasLogicModelBinding()
322 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
323 * corresponding to this {@link GUIWire}.
325 public void forceWireValues(BitVector values)
327 end.getWire().forceValues(values);
333 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
335 public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);}
337 private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
341 public String toString()
343 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";