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, ConnectionPoint pin1, ConnectionPoint pin2)
78 this(model, pin1, pin2, (Point[]) null);
82 * Creates a new {@link GUIWire} without automatic interpolation.
84 * @author Daniel Kirschten
86 public GUIWire(ViewModelModifiable model, ConnectionPoint pin1, ConnectionPoint pin2, Point... path)
88 this(model, pin1.getPin(), pin2.getPin(), path);
92 * Creates a new {@link GUIWire} without automatic interpolation.
94 * @author Daniel Kirschten
96 GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
98 logicObs = (i) -> callRedrawListeners();
100 this.logicWidth = pin1.logicWidth;
101 if (pin2.logicWidth != pin1.logicWidth)
102 throw new IllegalArgumentException("Can't connect pins of different logic width");
107 this.path = path == null ? null : Arrays.copyOf(path, path.length);
108 this.bounds = new Rectangle(0, 0, -1, -1);
110 redrawListeners = new ArrayList<>();
112 pin1.addPinMovedListener(p -> pinMoved());
113 pin2.addPinMovedListener(p -> pinMoved());
115 recalculateEffectivePath();
117 model.wireCreated(this);
121 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
122 * this component is a part of.
124 * @author Daniel Kirschten
126 public void destroy()
128 model.wireDestroyed(this);
134 * Returns the {@link Pin} on one side of this wire, usually the signal source.
136 * @author Daniel Kirschten
144 * Returns the {@link Pin} on one side of this wire, usually the signal target.
146 * @author Daniel Kirschten
154 * Called when {@link #pin1} or {@link #pin2} were moved.
156 * @author Daniel Kirschten
158 private void pinMoved()
160 recalculateEffectivePath();
161 callRedrawListeners();
164 // "graphical" operations
167 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
169 * @author Daniel Kirschten
171 private void recalculateEffectivePath()
173 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
175 double boundsX1 = Math.min(pos1.x, pos2.x);
176 double boundsY1 = Math.min(pos1.y, pos2.y);
177 double boundsX2 = Math.max(pos1.x, pos2.x);
178 double boundsY2 = Math.max(pos1.y, pos2.y);
181 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 };
184 effectivePath = new double[path.length * 2 + 4];
185 effectivePath[0] = pos1.x;
186 effectivePath[1] = pos1.y;
187 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
189 double pathX = path[srcI].x;
190 double pathY = path[srcI].y;
191 effectivePath[dstI + 0] = pathX;
192 effectivePath[dstI + 1] = pathY;
193 if (pathX < boundsX1)
195 if (pathX > boundsX2)
197 if (pathY < boundsY1)
199 if (pathY > boundsY2)
202 effectivePath[effectivePath.length - 2] = pos2.x;
203 effectivePath[effectivePath.length - 1] = pos2.y;
208 bounds.width = boundsX2 - boundsX1;
209 bounds.height = boundsY2 - boundsY1;
213 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
215 * @author Daniel Kirschten
217 public Rectangle getBounds()
219 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
223 * Render this wire to the given gc, in absoulute coordinates.
225 * @author Daniel Kirschten
227 public void render(GeneralGC gc)
229 ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath));
233 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
234 * in {@link #render(GeneralGC)}.<br>
235 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
238 * @author Daniel Kirschten
240 public Point[] getPath()
242 return path == null ? null : path.clone();
245 // logic model binding
248 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
249 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
250 * The argument can be null, in which case the old binding is stopped.
252 * @author Daniel Kirschten
254 public void setLogicModelBinding(ReadEnd end)
256 if (this.end != null)
257 this.end.deregisterObserver(logicObs);
260 end.registerObserver(logicObs);
264 * Returns whether this {@link GUIWire} has a logic model binding or not.
266 * @author Daniel Kirschten
268 public boolean hasLogicModelBinding()
274 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
275 * corresponding to this {@link GUIWire}.
277 * @author Daniel Kirschten
279 public void forceWireValues(BitVector values)
281 end.getWire().forceValues(values);
285 * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
286 * this {@link GUIWire}.
288 * @author Daniel Kirschten
290 public BitVector getWireValues()
292 return end.getValues();
298 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
300 public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);}
302 private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
306 public String toString()
308 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";