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