1 package net.mograsim.logic.model.model.wires;
3 import java.util.ArrayList;
4 import java.util.Arrays;
6 import java.util.function.Consumer;
8 import org.eclipse.swt.SWT;
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.core.LogicObserver;
14 import net.mograsim.logic.core.types.BitVector;
15 import net.mograsim.logic.core.types.BitVectorFormatter;
16 import net.mograsim.logic.core.wires.Wire;
17 import net.mograsim.logic.core.wires.Wire.ReadEnd;
18 import net.mograsim.logic.model.model.ViewModelModifiable;
19 import net.mograsim.preferences.ColorDefinition;
20 import net.mograsim.preferences.ColorManager;
21 import net.mograsim.preferences.Preferences;
24 * A wire connecting exactly two {@link Pin}s.
26 * @author Daniel Kirschten
31 * The model this wire is a part of.
33 private final ViewModelModifiable model;
35 * The name of this wire. Is unique for all wires in its model.
37 public final String name;
39 * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.
41 public final int logicWidth;
43 * The {@link Pin} on one side of this wire, usually the signal source.
47 * The {@link Pin} on one side of this wire, usually the signal target.
51 * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
52 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
57 * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
59 private final Rectangle bounds;
61 * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
63 private double[] effectivePath;
65 private final List<Consumer<GUIWire>> pathChangedListeners;
68 * A LogicObserver calling redrawListeners. Used for logic model bindings.
70 private final LogicObserver logicObs;
72 * A ReadEnd of the logic wire this GUI wire currently is bound to.
76 // creation and destruction
79 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
81 * @author Daniel Kirschten
83 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
85 this(model, null, pin1, pin2);
89 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
91 * @author Daniel Kirschten
93 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
95 this(model, null, pin1, pin2);
99 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
101 * @author Daniel Kirschten
103 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
105 this(model, null, pin1, pin2);
109 * Creates a new {@link GUIWire} with automatic interpolation and using the default name.
111 * @author Daniel Kirschten
113 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
115 this(model, null, pin1, pin2);
119 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
121 * @author Daniel Kirschten
123 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
125 this(model, null, pin1, pin2, path);
129 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
131 * @author Daniel Kirschten
133 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
135 this(model, null, pin1, pin2, path);
139 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
141 * @author Daniel Kirschten
143 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
145 this(model, null, pin1, pin2, path);
149 * Creates a new {@link GUIWire} without automatic interpolation and using the default name.
151 * @author Daniel Kirschten
153 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
155 this(model, null, pin1, pin2, path);
159 * Creates a new {@link GUIWire} with automatic interpolation.
161 * @author Daniel Kirschten
163 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, WireCrossPoint pin2)
165 this(model, name, pin1, pin2, (Point[]) null);
169 * Creates a new {@link GUIWire} with automatic interpolation.
171 * @author Daniel Kirschten
173 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, Pin pin2)
175 this(model, name, pin1, pin2, (Point[]) null);
179 * Creates a new {@link GUIWire} with automatic interpolation.
181 * @author Daniel Kirschten
183 public GUIWire(ViewModelModifiable model, String name, Pin pin1, WireCrossPoint pin2)
185 this(model, name, pin1, pin2, (Point[]) null);
189 * Creates a new {@link GUIWire} with automatic interpolation.
191 * @author Daniel Kirschten
193 public GUIWire(ViewModelModifiable model, String name, Pin pin1, Pin pin2)
195 this(model, name, pin1, pin2, (Point[]) null);
199 * Creates a new {@link GUIWire} without automatic interpolation.
201 * @author Daniel Kirschten
203 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
205 this(model, name, pin1.getPin(), pin2.getPin(), path);
209 * Creates a new {@link GUIWire} without automatic interpolation.
211 * @author Daniel Kirschten
213 public GUIWire(ViewModelModifiable model, String name, WireCrossPoint pin1, Pin pin2, Point... path)
215 this(model, name, pin1.getPin(), pin2, path);
219 * Creates a new {@link GUIWire} without automatic interpolation.
221 * @author Daniel Kirschten
223 public GUIWire(ViewModelModifiable model, String name, Pin pin1, WireCrossPoint pin2, Point... path)
225 this(model, name, pin1, pin2.getPin(), path);
229 * Creates a new {@link GUIWire} without automatic interpolation.
231 * @author Daniel Kirschten
233 public GUIWire(ViewModelModifiable model, String name, Pin pin1, Pin pin2, Point... path)
236 this.name = name == null ? model.getDefaultWireName() : name;
237 this.logicWidth = pin1.logicWidth;
238 if (pin2.logicWidth != pin1.logicWidth)
239 throw new IllegalArgumentException("Can't connect pins of different logic width");
244 this.path = path == null ? null : Arrays.copyOf(path, path.length);
245 this.bounds = new Rectangle(0, 0, -1, -1);
247 pathChangedListeners = new ArrayList<>();
249 logicObs = (i) -> model.requestRedraw();
251 pin1.addPinMovedListener(p -> pinMoved());
252 pin2.addPinMovedListener(p -> pinMoved());
254 recalculateEffectivePath();
256 model.wireCreated(this, this::destroyed);
260 * Destroys this wire. This method is called from {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} of the model this
263 * @author Daniel Kirschten
265 private void destroyed()
273 * Returns the {@link Pin} on one side of this wire, usually the signal source.
275 * @author Daniel Kirschten
283 * Returns the {@link Pin} on one side of this wire, usually the signal target.
285 * @author Daniel Kirschten
293 * Called when {@link #pin1} or {@link #pin2} were moved.
295 * @author Daniel Kirschten
297 private void pinMoved()
299 recalculateEffectivePath();
300 model.requestRedraw();
303 // "graphical" operations
306 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
308 * @author Daniel Kirschten
310 private void recalculateEffectivePath()
312 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
314 double boundsX1 = Math.min(pos1.x, pos2.x);
315 double boundsY1 = Math.min(pos1.y, pos2.y);
316 double boundsX2 = Math.max(pos1.x, pos2.x);
317 double boundsY2 = Math.max(pos1.y, pos2.y);
320 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 };
323 effectivePath = new double[path.length * 2 + 4];
324 effectivePath[0] = pos1.x;
325 effectivePath[1] = pos1.y;
326 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
328 double pathX = path[srcI].x;
329 double pathY = path[srcI].y;
330 effectivePath[dstI + 0] = pathX;
331 effectivePath[dstI + 1] = pathY;
332 if (pathX < boundsX1)
334 if (pathX > boundsX2)
336 if (pathY < boundsY1)
338 if (pathY > boundsY2)
341 effectivePath[effectivePath.length - 2] = pos2.x;
342 effectivePath[effectivePath.length - 1] = pos2.y;
347 bounds.width = boundsX2 - boundsX1;
348 bounds.height = boundsY2 - boundsY1;
352 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
354 * @author Daniel Kirschten
356 public Rectangle getBounds()
358 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
362 * Render this wire to the given gc, in absoulute coordinates.
364 * @author Daniel Kirschten
366 public void render(GeneralGC gc)
368 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
369 if (wireColor != null)
370 gc.setForeground(ColorManager.current().toColor(wireColor));
372 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.wire.singlebit"));
374 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.wire.multibit"));
375 gc.drawPolyline(effectivePath);
376 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.default"));
379 // operations concerning the path
382 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
383 * in {@link #render(GeneralGC)}.<br>
384 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
387 * @author Daniel Kirschten
389 public Point[] getPath()
391 return deepPathCopy(path);
394 public void setPath(Point... path)
396 this.path = deepPathCopy(path);
397 recalculateEffectivePath();
398 callPathChangedListeners();
399 model.requestRedraw();
402 public Point getPathPoint(int index)
404 return pointCopy(path[index]);
407 public void setPathPoint(Point p, int index)
409 path[index] = pointCopy(p);
410 recalculateEffectivePath();
411 callPathChangedListeners();
412 model.requestRedraw();
415 public void insertPathPoint(Point p, int index)
418 path = new Point[] { pointCopy(p) };
421 Point[] oldPath = path;
422 path = new Point[oldPath.length + 1];
423 System.arraycopy(oldPath, 0, path, 0, index);
424 if (index < oldPath.length)
425 System.arraycopy(oldPath, index, path, index + 1, oldPath.length - index);
426 path[index] = pointCopy(p);
428 recalculateEffectivePath();
429 callPathChangedListeners();
432 public void removePathPoint(int index)
434 if (path.length == 0)
438 Point[] oldPath = path;
439 path = new Point[oldPath.length - 1];
440 System.arraycopy(oldPath, 0, path, 0, index);
441 if (index < oldPath.length - 1)
442 System.arraycopy(oldPath, index + 1, path, index, oldPath.length - index - 1);
444 recalculateEffectivePath();
445 callPathChangedListeners();
448 public double[] getEffectivePath()
450 return Arrays.copyOf(effectivePath, effectivePath.length);
453 private static Point[] deepPathCopy(Point[] path)
457 Point[] copy = new Point[path.length];
458 for (int i = 0; i < path.length; i++)
459 copy[i] = pointCopy(path[i]);
463 private static Point pointCopy(Point p)
465 return new Point(p.x, p.y);
468 // logic model binding
471 * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
472 * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
473 * The argument can be null, in which case the old binding is stopped.
475 * @author Daniel Kirschten
477 public void setLogicModelBinding(ReadEnd end)
479 if (this.end != null)
480 this.end.deregisterObserver(logicObs);
483 end.registerObserver(logicObs);
487 * Returns whether this {@link GUIWire} has a logic model binding or not.
489 * @author Daniel Kirschten
491 public boolean hasLogicModelBinding()
497 * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
498 * corresponding to this {@link GUIWire}.
500 * @author Daniel Kirschten
502 public void forceWireValues(BitVector values)
504 end.getWire().forceValues(values);
508 * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
509 * this {@link GUIWire}.
511 * @author Daniel Kirschten
513 public BitVector getWireValues()
515 return end.getValues();
521 public void addPathChangedListener (Consumer<GUIWire> listener) {pathChangedListeners.add (listener);}
523 public void removePathChangedListener(Consumer<GUIWire> listener) {pathChangedListeners.remove(listener);}
525 private void callPathChangedListeners() {pathChangedListeners.forEach(l -> l.accept(this));}
529 public String toString()
531 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";