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.model.ViewModelModifiable;
18 import net.mograsim.preferences.ColorDefinition;
19 import net.mograsim.preferences.ColorManager;
22 * A wire connecting exactly two {@link Pin}s.
24 * @author Daniel Kirschten
29 * The model this wire is a part of.
31 private final ViewModelModifiable model;
33 * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.
35 public final int logicWidth;
37 * The {@link Pin} on one side of this wire, usually the signal source.
41 * The {@link Pin} on one side of this wire, usually the signal target.
45 * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
46 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
51 * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
53 private final Rectangle bounds;
55 * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
57 private double[] effectivePath;
59 private final List<Runnable> redrawListeners;
62 * A LogicObserver calling redrawListeners. Used for logic model bindings.
64 private final LogicObserver logicObs;
66 * A ReadEnd of the logic wire this GUI wire currently is bound to.
70 // creation and destruction
73 * Creates a new {@link GUIWire} with automatic interpolation.
75 * @author Daniel Kirschten
77 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
79 this(model, pin1, pin2, (Point[]) null);
83 * Creates a new {@link GUIWire} with automatic interpolation.
85 * @author Daniel Kirschten
87 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
89 this(model, pin1, pin2, (Point[]) null);
93 * Creates a new {@link GUIWire} with automatic interpolation.
95 * @author Daniel Kirschten
97 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
99 this(model, pin1, pin2, (Point[]) null);
103 * Creates a new {@link GUIWire} with automatic interpolation.
105 * @author Daniel Kirschten
107 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
109 this(model, pin1, pin2, (Point[]) null);
113 * Creates a new {@link GUIWire} without automatic interpolation.
115 * @author Daniel Kirschten
117 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
119 this(model, pin1.getPin(), pin2.getPin(), path);
123 * Creates a new {@link GUIWire} without automatic interpolation.
125 * @author Daniel Kirschten
127 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
129 this(model, pin1.getPin(), pin2, path);
133 * Creates a new {@link GUIWire} without automatic interpolation.
135 * @author Daniel Kirschten
137 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
139 this(model, pin1, pin2.getPin(), path);
143 * Creates a new {@link GUIWire} without automatic interpolation.
145 * @author Daniel Kirschten
147 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
149 logicObs = (i) -> callRedrawListeners();
151 this.logicWidth = pin1.logicWidth;
152 if (pin2.logicWidth != pin1.logicWidth)
153 throw new IllegalArgumentException("Can't connect pins of different logic width");
158 this.path = path == null ? null : Arrays.copyOf(path, path.length);
159 this.bounds = new Rectangle(0, 0, -1, -1);
161 redrawListeners = new ArrayList<>();
163 pin1.addPinMovedListener(p -> pinMoved());
164 pin2.addPinMovedListener(p -> pinMoved());
166 recalculateEffectivePath();
168 model.wireCreated(this);
172 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
173 * this component is a part of.
175 * @author Daniel Kirschten
177 public void destroy()
179 model.wireDestroyed(this);
185 * Returns the {@link Pin} on one side of this wire, usually the signal source.
187 * @author Daniel Kirschten
195 * Returns the {@link Pin} on one side of this wire, usually the signal target.
197 * @author Daniel Kirschten
205 * Called when {@link #pin1} or {@link #pin2} were moved.
207 * @author Daniel Kirschten
209 private void pinMoved()
211 recalculateEffectivePath();
212 callRedrawListeners();
215 // "graphical" operations
218 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
220 * @author Daniel Kirschten
222 private void recalculateEffectivePath()
224 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
226 double boundsX1 = Math.min(pos1.x, pos2.x);
227 double boundsY1 = Math.min(pos1.y, pos2.y);
228 double boundsX2 = Math.max(pos1.x, pos2.x);
229 double boundsY2 = Math.max(pos1.y, pos2.y);
232 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 };
235 effectivePath = new double[path.length * 2 + 4];
236 effectivePath[0] = pos1.x;
237 effectivePath[1] = pos1.y;
238 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
240 double pathX = path[srcI].x;
241 double pathY = path[srcI].y;
242 effectivePath[dstI + 0] = pathX;
243 effectivePath[dstI + 1] = pathY;
244 if (pathX < boundsX1)
246 if (pathX > boundsX2)
248 if (pathY < boundsY1)
250 if (pathY > boundsY2)
253 effectivePath[effectivePath.length - 2] = pos2.x;
254 effectivePath[effectivePath.length - 1] = pos2.y;
259 bounds.width = boundsX2 - boundsX1;
260 bounds.height = boundsY2 - boundsY1;
264 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
266 * @author Daniel Kirschten
268 public Rectangle getBounds()
270 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
274 * Render this wire to the given gc, in absoulute coordinates.
276 * @author Daniel Kirschten
278 public void render(GeneralGC gc)
280 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
281 if (wireColor != null)
282 gc.setForeground(ColorManager.current().toColor(wireColor));
283 gc.drawPolyline(effectivePath);
287 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
288 * in {@link #render(GeneralGC)}.<br>
289 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
292 * @author Daniel Kirschten
294 public Point[] getPath()
296 return path == null ? null : path.clone();
299 // logic model binding
302 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
303 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
304 * The argument can be null, in which case the old binding is stopped.
306 * @author Daniel Kirschten
308 public void setLogicModelBinding(ReadEnd end)
310 if (this.end != null)
311 this.end.deregisterObserver(logicObs);
314 end.registerObserver(logicObs);
318 * Returns whether this {@link GUIWire} has a logic model binding or not.
320 * @author Daniel Kirschten
322 public boolean hasLogicModelBinding()
328 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
329 * corresponding to this {@link GUIWire}.
331 * @author Daniel Kirschten
333 public void forceWireValues(BitVector values)
335 end.getWire().forceValues(values);
339 * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
340 * this {@link GUIWire}.
342 * @author Daniel Kirschten
344 public BitVector getWireValues()
346 return end.getValues();
352 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
354 public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);}
356 private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
360 public String toString()
362 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";