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, ConnectionPoint pin1, ConnectionPoint pin2)
79 this(model, pin1, pin2, (Point[]) null);
83 * Creates a new {@link GUIWire} without automatic interpolation.
85 * @author Daniel Kirschten
87 public GUIWire(ViewModelModifiable model, ConnectionPoint pin1, ConnectionPoint pin2, Point... path)
89 this(model, pin1.getPin(), pin2.getPin(), path);
93 * Creates a new {@link GUIWire} without automatic interpolation.
95 * @author Daniel Kirschten
97 GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
99 logicObs = (i) -> callRedrawListeners();
101 this.logicWidth = pin1.logicWidth;
102 if (pin2.logicWidth != pin1.logicWidth)
103 throw new IllegalArgumentException("Can't connect pins of different logic width");
108 this.path = path == null ? null : Arrays.copyOf(path, path.length);
109 this.bounds = new Rectangle(0, 0, -1, -1);
111 redrawListeners = new ArrayList<>();
113 pin1.addPinMovedListener(p -> pinMoved());
114 pin2.addPinMovedListener(p -> pinMoved());
116 recalculateEffectivePath();
118 model.wireCreated(this);
122 * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
123 * this component is a part of.
125 * @author Daniel Kirschten
127 public void destroy()
129 model.wireDestroyed(this);
135 * Returns the {@link Pin} on one side of this wire, usually the signal source.
137 * @author Daniel Kirschten
145 * Returns the {@link Pin} on one side of this wire, usually the signal target.
147 * @author Daniel Kirschten
155 * Called when {@link #pin1} or {@link #pin2} were moved.
157 * @author Daniel Kirschten
159 private void pinMoved()
161 recalculateEffectivePath();
162 callRedrawListeners();
165 // "graphical" operations
168 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
170 * @author Daniel Kirschten
172 private void recalculateEffectivePath()
174 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
176 double boundsX1 = Math.min(pos1.x, pos2.x);
177 double boundsY1 = Math.min(pos1.y, pos2.y);
178 double boundsX2 = Math.max(pos1.x, pos2.x);
179 double boundsY2 = Math.max(pos1.y, pos2.y);
182 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 };
185 effectivePath = new double[path.length * 2 + 4];
186 effectivePath[0] = pos1.x;
187 effectivePath[1] = pos1.y;
188 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
190 double pathX = path[srcI].x;
191 double pathY = path[srcI].y;
192 effectivePath[dstI + 0] = pathX;
193 effectivePath[dstI + 1] = pathY;
194 if (pathX < boundsX1)
196 if (pathX > boundsX2)
198 if (pathY < boundsY1)
200 if (pathY > boundsY2)
203 effectivePath[effectivePath.length - 2] = pos2.x;
204 effectivePath[effectivePath.length - 1] = pos2.y;
209 bounds.width = boundsX2 - boundsX1;
210 bounds.height = boundsY2 - boundsY1;
214 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
216 * @author Daniel Kirschten
218 public Rectangle getBounds()
220 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
224 * Render this wire to the given gc, in absoulute coordinates.
226 * @author Daniel Kirschten
228 public void render(GeneralGC gc)
230 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
231 if (wireColor != null)
232 gc.setForeground(ColorManager.current().toColor(wireColor));
233 gc.drawPolyline(effectivePath);
237 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
238 * in {@link #render(GeneralGC)}.<br>
239 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
242 * @author Daniel Kirschten
244 public Point[] getPath()
246 return path == null ? null : path.clone();
249 // logic model binding
252 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
253 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
254 * The argument can be null, in which case the old binding is stopped.
256 * @author Daniel Kirschten
258 public void setLogicModelBinding(ReadEnd end)
260 if (this.end != null)
261 this.end.deregisterObserver(logicObs);
264 end.registerObserver(logicObs);
268 * Returns whether this {@link GUIWire} has a logic model binding or not.
270 * @author Daniel Kirschten
272 public boolean hasLogicModelBinding()
278 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
279 * corresponding to this {@link GUIWire}.
281 * @author Daniel Kirschten
283 public void forceWireValues(BitVector values)
285 end.getWire().forceValues(values);
289 * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
290 * this {@link GUIWire}.
292 * @author Daniel Kirschten
294 public BitVector getWireValues()
296 return end.getValues();
302 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
304 public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);}
306 private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
310 public String toString()
312 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";