1 package net.mograsim.logic.ui.model.wires;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashSet;
9 import org.eclipse.swt.SWT;
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
14 import net.mograsim.logic.core.LogicObserver;
15 import net.mograsim.logic.core.types.BitVector;
16 import net.mograsim.logic.core.types.BitVectorFormatter;
17 import net.mograsim.logic.core.wires.Wire;
18 import net.mograsim.logic.core.wires.Wire.ReadEnd;
19 import net.mograsim.logic.ui.model.ViewModelModifiable;
20 import net.mograsim.preferences.ColorDefinition;
21 import net.mograsim.preferences.ColorManager;
24 * A wire connecting exactly two {@link Pin}s.
26 * @author Daniel Kirschten
31 private final Set<PathChangedListener> pathChangedListeners;
34 * The model this wire is a part of.
36 private final ViewModelModifiable model;
38 * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.
40 public final int logicWidth;
42 * The {@link Pin} on one side of this wire, usually the signal source.
46 * The {@link Pin} on one side of this wire, usually the signal target.
50 * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
51 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
56 * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
58 private final Rectangle bounds;
60 * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
62 protected double[] effectivePath;
64 private final List<Runnable> redrawListeners;
67 * A LogicObserver calling redrawListeners. Used for logic model bindings.
69 private final LogicObserver logicObs;
71 * A ReadEnd of the logic wire this GUI wire currently is bound to.
75 // creation and destruction
78 * Creates a new {@link GUIWire} with automatic interpolation.
80 * @author Daniel Kirschten
82 public GUIWire(ViewModelModifiable model, ConnectionPoint pin1, ConnectionPoint pin2)
84 this(model, pin1, pin2, (Point[]) null);
88 * Creates a new {@link GUIWire} without automatic interpolation.
90 * @author Daniel Kirschten
92 public GUIWire(ViewModelModifiable model, ConnectionPoint pin1, ConnectionPoint pin2, Point... path)
94 this(model, pin1.getPin(), pin2.getPin(), path);
98 * Creates a new {@link GUIWire} without automatic interpolation.
100 * @author Daniel Kirschten
102 GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
104 pathChangedListeners = new HashSet<>();
105 logicObs = (i) -> callRedrawListeners();
107 this.logicWidth = pin1.logicWidth;
108 if (pin2.logicWidth != pin1.logicWidth)
109 throw new IllegalArgumentException("Can't connect pins of different logic width");
114 this.path = path == null ? null : Arrays.copyOf(path, path.length);
115 this.bounds = new Rectangle(0, 0, -1, -1);
117 redrawListeners = new ArrayList<>();
119 pin1.addPinMovedListener(p -> pinMoved());
120 pin2.addPinMovedListener(p -> pinMoved());
122 recalculateEffectivePath();
124 model.wireCreated(this);
128 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
129 * this component is a part of.
131 * @author Daniel Kirschten
133 public void destroy()
135 model.wireDestroyed(this);
141 * Returns the {@link Pin} on one side of this wire, usually the signal source.
143 * @author Daniel Kirschten
151 * Returns the {@link Pin} on one side of this wire, usually the signal target.
153 * @author Daniel Kirschten
161 * Called when {@link #pin1} or {@link #pin2} were moved.
163 * @author Daniel Kirschten
165 private void pinMoved()
167 recalculateEffectivePath();
168 callRedrawListeners();
171 // "graphical" operations
174 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
176 * @author Daniel Kirschten
178 private void recalculateEffectivePath()
180 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
182 double boundsX1 = Math.min(pos1.x, pos2.x);
183 double boundsY1 = Math.min(pos1.y, pos2.y);
184 double boundsX2 = Math.max(pos1.x, pos2.x);
185 double boundsY2 = Math.max(pos1.y, pos2.y);
188 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 };
191 effectivePath = new double[path.length * 2 + 4];
192 effectivePath[0] = pos1.x;
193 effectivePath[1] = pos1.y;
194 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
196 double pathX = path[srcI].x;
197 double pathY = path[srcI].y;
198 effectivePath[dstI + 0] = pathX;
199 effectivePath[dstI + 1] = pathY;
200 if (pathX < boundsX1)
202 if (pathX > boundsX2)
204 if (pathY < boundsY1)
206 if (pathY > boundsY2)
209 effectivePath[effectivePath.length - 2] = pos2.x;
210 effectivePath[effectivePath.length - 1] = pos2.y;
215 bounds.width = boundsX2 - boundsX1;
216 bounds.height = boundsY2 - boundsY1;
220 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
222 * @author Daniel Kirschten
224 public Rectangle getBounds()
226 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
230 * Render this wire to the given gc, in absoulute coordinates.
232 * @author Daniel Kirschten
234 public void render(GeneralGC gc)
236 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
237 if (wireColor != null)
238 gc.setForeground(ColorManager.current().toColor(wireColor));
239 gc.drawPolyline(effectivePath);
243 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
244 * in {@link #render(GeneralGC)}.<br>
245 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
248 * @author Daniel Kirschten
250 public Point[] getPath()
252 return path == null ? null : path.clone();
255 // logic model binding
258 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
259 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
260 * The argument can be null, in which case the old binding is stopped.
262 * @author Daniel Kirschten
264 public void setLogicModelBinding(ReadEnd end)
266 if (this.end != null)
267 this.end.deregisterObserver(logicObs);
270 end.registerObserver(logicObs);
274 * Returns whether this {@link GUIWire} has a logic model binding or not.
276 * @author Daniel Kirschten
278 public boolean hasLogicModelBinding()
284 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
285 * corresponding to this {@link GUIWire}.
287 * @author Daniel Kirschten
289 public void forceWireValues(BitVector values)
291 end.getWire().forceValues(values);
295 * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
296 * this {@link GUIWire}.
298 * @author Daniel Kirschten
300 public BitVector getWireValues()
302 return end.getValues();
308 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
310 public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);}
312 private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
314 public void addPathChangedListener(PathChangedListener l) { pathChangedListeners.add(l); }
316 public void removePathChangedListener(PathChangedListener l) { pathChangedListeners.remove(l); }
318 public void callPathChangedListeners(int diff) { pathChangedListeners.forEach(l -> l.pathChanged(this, diff)); }
321 public static interface PathChangedListener
324 * Called whenever the {@link Wire}'s path changes
326 * @param wire The wire which had its path changed
327 * @param diff The length difference between before and after the path change.
329 public void pathChanged(GUIWire wire, int diff);
334 public String toString()
336 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";
339 public void setPath(Point[] path)
341 int diff = (path == null ? 0 : path.length) - (this.path == null ? 0 : this.path.length);
342 this.path = path == null ? null : path.clone();
343 recalculateEffectivePath();
344 callPathChangedListeners(diff);
345 callRedrawListeners();
348 public void setPathPoint(Point p, int index)
351 recalculateEffectivePath();
352 callPathChangedListeners(0);
353 callRedrawListeners();
356 public void insertPathPoint(Point p, int index)
358 Point[] path = getPath();
360 setPath(new Point[] { p });
363 Point[] newPath = new Point[path.length + 1];
364 System.arraycopy(path, 0, newPath, 0, index);
365 if (index < path.length)
366 System.arraycopy(path, index, newPath, index + 1, path.length - index);
372 public void removePathPoint(int index)
374 Point[] path = getPath();
375 Point[] newPath = new Point[path.length - 1];
376 System.arraycopy(path, 0, newPath, 0, index);
377 if (index < path.length - 1)
378 System.arraycopy(path, index + 1, newPath, index, path.length - index - 1);
383 * @throws IndexOutOfBoundsException
385 public Point getPathPoint(int index)
390 public int getPathLength()
395 public double[] getEffectivePath()
397 return effectivePath.clone();