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