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.CoreWire;
17 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
18 import net.mograsim.logic.model.model.LogicModelModifiable;
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
28 public class ModelWire
31 * The model this wire is a part of.
33 private final LogicModelModifiable 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 width 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<ModelWire>> pathChangedListeners;
68 * A LogicObserver calling redrawListeners. Used for core model bindings.
70 private final LogicObserver logicObs;
72 * A ReadEnd of the core wire this model wire currently is bound to.
76 // creation and destruction
79 * Creates a new {@link ModelWire} with automatic interpolation and using the default name.
81 * @author Daniel Kirschten
83 public ModelWire(LogicModelModifiable model, ModelWireCrossPoint pin1, ModelWireCrossPoint pin2)
85 this(model, null, pin1, pin2);
89 * Creates a new {@link ModelWire} with automatic interpolation and using the default name.
91 * @author Daniel Kirschten
93 public ModelWire(LogicModelModifiable model, ModelWireCrossPoint pin1, Pin pin2)
95 this(model, null, pin1, pin2);
99 * Creates a new {@link ModelWire} with automatic interpolation and using the default name.
101 * @author Daniel Kirschten
103 public ModelWire(LogicModelModifiable model, Pin pin1, ModelWireCrossPoint pin2)
105 this(model, null, pin1, pin2);
109 * Creates a new {@link ModelWire} with automatic interpolation and using the default name.
111 * @author Daniel Kirschten
113 public ModelWire(LogicModelModifiable model, Pin pin1, Pin pin2)
115 this(model, null, pin1, pin2);
119 * Creates a new {@link ModelWire} without automatic interpolation and using the default name.
121 * @author Daniel Kirschten
123 public ModelWire(LogicModelModifiable model, ModelWireCrossPoint pin1, ModelWireCrossPoint pin2, Point... path)
125 this(model, null, pin1, pin2, path);
129 * Creates a new {@link ModelWire} without automatic interpolation and using the default name.
131 * @author Daniel Kirschten
133 public ModelWire(LogicModelModifiable model, ModelWireCrossPoint pin1, Pin pin2, Point... path)
135 this(model, null, pin1, pin2, path);
139 * Creates a new {@link ModelWire} without automatic interpolation and using the default name.
141 * @author Daniel Kirschten
143 public ModelWire(LogicModelModifiable model, Pin pin1, ModelWireCrossPoint pin2, Point... path)
145 this(model, null, pin1, pin2, path);
149 * Creates a new {@link ModelWire} without automatic interpolation and using the default name.
151 * @author Daniel Kirschten
153 public ModelWire(LogicModelModifiable model, Pin pin1, Pin pin2, Point... path)
155 this(model, null, pin1, pin2, path);
159 * Creates a new {@link ModelWire} with automatic interpolation.
161 * @author Daniel Kirschten
163 public ModelWire(LogicModelModifiable model, String name, ModelWireCrossPoint pin1, ModelWireCrossPoint pin2)
165 this(model, name, pin1, pin2, (Point[]) null);
169 * Creates a new {@link ModelWire} with automatic interpolation.
171 * @author Daniel Kirschten
173 public ModelWire(LogicModelModifiable model, String name, ModelWireCrossPoint pin1, Pin pin2)
175 this(model, name, pin1, pin2, (Point[]) null);
179 * Creates a new {@link ModelWire} with automatic interpolation.
181 * @author Daniel Kirschten
183 public ModelWire(LogicModelModifiable model, String name, Pin pin1, ModelWireCrossPoint pin2)
185 this(model, name, pin1, pin2, (Point[]) null);
189 * Creates a new {@link ModelWire} with automatic interpolation.
191 * @author Daniel Kirschten
193 public ModelWire(LogicModelModifiable model, String name, Pin pin1, Pin pin2)
195 this(model, name, pin1, pin2, (Point[]) null);
199 * Creates a new {@link ModelWire} without automatic interpolation.
201 * @author Daniel Kirschten
203 public ModelWire(LogicModelModifiable model, String name, ModelWireCrossPoint pin1, ModelWireCrossPoint pin2, Point... path)
205 this(model, name, pin1.getPin(), pin2.getPin(), path);
209 * Creates a new {@link ModelWire} without automatic interpolation.
211 * @author Daniel Kirschten
213 public ModelWire(LogicModelModifiable model, String name, ModelWireCrossPoint pin1, Pin pin2, Point... path)
215 this(model, name, pin1.getPin(), pin2, path);
219 * Creates a new {@link ModelWire} without automatic interpolation.
221 * @author Daniel Kirschten
223 public ModelWire(LogicModelModifiable model, String name, Pin pin1, ModelWireCrossPoint pin2, Point... path)
225 this(model, name, pin1, pin2.getPin(), path);
229 * Creates a new {@link ModelWire} without automatic interpolation.
231 * @author Daniel Kirschten
233 public ModelWire(LogicModelModifiable 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 LogicModelModifiable#wireDestroyed(ModelWire) wireDestroyed()} of the model
261 * this wire is a part of.
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 Preferences.current().getDouble("net.mograsim.logic.model.linewidth.wire." + (logicWidth == 1 ? "singlebit" : "multibit")));
373 gc.drawPolyline(effectivePath);
374 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.default"));
377 // operations concerning the path
380 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
381 * in {@link #render(GeneralGC)}.<br>
382 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
385 * @author Daniel Kirschten
387 public Point[] getPath()
389 return deepPathCopy(path);
392 public void setPath(Point... path)
394 this.path = deepPathCopy(path);
395 recalculateEffectivePath();
396 callPathChangedListeners();
397 model.requestRedraw();
400 public Point getPathPoint(int index)
402 return pointCopy(path[index]);
405 public void setPathPoint(Point p, int index)
407 path[index] = pointCopy(p);
408 recalculateEffectivePath();
409 callPathChangedListeners();
410 model.requestRedraw();
413 public void insertPathPoint(Point p, int index)
416 path = new Point[] { pointCopy(p) };
419 Point[] oldPath = path;
420 path = new Point[oldPath.length + 1];
421 System.arraycopy(oldPath, 0, path, 0, index);
422 if (index < oldPath.length)
423 System.arraycopy(oldPath, index, path, index + 1, oldPath.length - index);
424 path[index] = pointCopy(p);
426 recalculateEffectivePath();
427 callPathChangedListeners();
430 public void removePathPoint(int index)
432 if (path.length == 0)
436 Point[] oldPath = path;
437 path = new Point[oldPath.length - 1];
438 System.arraycopy(oldPath, 0, path, 0, index);
439 if (index < oldPath.length - 1)
440 System.arraycopy(oldPath, index + 1, path, index, oldPath.length - index - 1);
442 recalculateEffectivePath();
443 callPathChangedListeners();
446 public double[] getEffectivePath()
448 return Arrays.copyOf(effectivePath, effectivePath.length);
451 private static Point[] deepPathCopy(Point[] path)
455 Point[] copy = new Point[path.length];
456 for (int i = 0; i < path.length; i++)
457 copy[i] = pointCopy(path[i]);
461 private static Point pointCopy(Point p)
463 return new Point(p.x, p.y);
466 // core model binding
469 * Binds this {@link ModelWire} to the given {@link ReadEnd}: The color of this {@link ModelWire} will now depend on the state of the
470 * given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
471 * The argument can be null, in which case the old binding is stopped.
473 * @author Daniel Kirschten
475 public void setCoreModelBinding(ReadEnd end)
477 if (this.end != null)
478 this.end.deregisterObserver(logicObs);
481 end.registerObserver(logicObs);
485 * Returns whether this {@link ModelWire} has a core model binding or not.
487 * @author Daniel Kirschten
489 public boolean hasCoreModelBinding()
495 * If this {@link ModelWire} has a core model binding, delegates to {@link CoreWire#forceValues(BitVector)} for the {@link CoreWire}
496 * corresponding to this {@link ModelWire}.
498 * @author Daniel Kirschten
500 public void forceWireValues(BitVector values)
502 end.getWire().forceValues(values);
506 * If this {@link ModelWire} has a core model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
507 * this {@link ModelWire}.
509 * @author Daniel Kirschten
511 public BitVector getWireValues()
513 return end.getValues();
517 * Registers the given {@link LogicObserver} for the {@link ReadEnd} this {@link ModelWire} is bound to.
519 * @see ReadEnd#registerObserver(LogicObserver)
520 * @author Daniel Kirschten
522 public void addObserver(LogicObserver obs)
524 end.registerObserver(obs);
528 * Deregisters the given {@link LogicObserver} for the {@link ReadEnd} this {@link ModelWire} is bound to.
530 * @see ReadEnd#deregisterObserver(LogicObserver)
531 * @author Daniel Kirschten
533 public void removeObserver(LogicObserver obs)
535 end.deregisterObserver(obs);
541 public void addPathChangedListener (Consumer<ModelWire> listener) {pathChangedListeners.add (listener);}
543 public void removePathChangedListener(Consumer<ModelWire> listener) {pathChangedListeners.remove(listener);}
545 private void callPathChangedListeners() {pathChangedListeners.forEach(l -> l.accept(this));}
549 public String toString()
551 return "ModelWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";