1 package net.mograsim.logic.model.model.wires;
3 import static net.mograsim.logic.model.preferences.RenderPreferences.DEFAULT_LINE_WIDTH;
4 import static net.mograsim.logic.model.preferences.RenderPreferences.WIRE_WIDTH_MULTIBIT;
5 import static net.mograsim.logic.model.preferences.RenderPreferences.WIRE_WIDTH_SINGLEBIT;
7 import java.util.ArrayList;
8 import java.util.Arrays;
10 import java.util.function.Consumer;
12 import org.eclipse.swt.SWT;
14 import net.haspamelodica.swt.helper.gcs.GeneralGC;
15 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
16 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
17 import net.mograsim.logic.core.LogicObserver;
18 import net.mograsim.logic.core.types.BitVector;
19 import net.mograsim.logic.core.wires.CoreWire;
20 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
21 import net.mograsim.logic.model.BitVectorFormatter;
22 import net.mograsim.logic.model.model.LogicModelModifiable;
23 import net.mograsim.logic.model.preferences.RenderPreferences;
24 import net.mograsim.preferences.ColorDefinition;
25 import net.mograsim.preferences.ColorManager;
28 * A wire connecting exactly two {@link Pin}s.
30 * @author Daniel Kirschten
32 public class ModelWire
35 * The model this wire is a part of.
37 private final LogicModelModifiable model;
39 * The name of this wire. Is unique for all wires in its model.
41 public final String name;
43 * The logical width of this wire. Is equal to the logical width of {@link #pin1} and {@link #pin2}.
45 public final int logicWidth;
47 * The {@link Pin} on one side of this wire, usually the signal source.
51 * The {@link Pin} on one side of this wire, usually the signal target.
55 * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
56 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
61 * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
63 private final Rectangle bounds;
65 * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
67 private double[] effectivePath;
69 private final List<Consumer<ModelWire>> pathChangedListeners;
72 * A LogicObserver calling redrawListeners. Used for core model bindings.
74 private final LogicObserver logicObs;
76 * A ReadEnd of the core wire this model wire currently is bound to.
80 // creation and destruction
83 * Creates a new {@link ModelWire} with automatic interpolation and using the default name.
85 * @author Daniel Kirschten
87 public ModelWire(LogicModelModifiable model, ModelWireCrossPoint pin1, ModelWireCrossPoint pin2)
89 this(model, null, pin1, pin2);
93 * Creates a new {@link ModelWire} with automatic interpolation and using the default name.
95 * @author Daniel Kirschten
97 public ModelWire(LogicModelModifiable model, ModelWireCrossPoint pin1, Pin pin2)
99 this(model, null, pin1, pin2);
103 * Creates a new {@link ModelWire} with automatic interpolation and using the default name.
105 * @author Daniel Kirschten
107 public ModelWire(LogicModelModifiable model, Pin pin1, ModelWireCrossPoint pin2)
109 this(model, null, pin1, pin2);
113 * Creates a new {@link ModelWire} with automatic interpolation and using the default name.
115 * @author Daniel Kirschten
117 public ModelWire(LogicModelModifiable model, Pin pin1, Pin pin2)
119 this(model, null, pin1, pin2);
123 * Creates a new {@link ModelWire} without automatic interpolation and using the default name.
125 * @author Daniel Kirschten
127 public ModelWire(LogicModelModifiable model, ModelWireCrossPoint pin1, ModelWireCrossPoint pin2, Point... path)
129 this(model, null, pin1, pin2, path);
133 * Creates a new {@link ModelWire} without automatic interpolation and using the default name.
135 * @author Daniel Kirschten
137 public ModelWire(LogicModelModifiable model, ModelWireCrossPoint pin1, Pin pin2, Point... path)
139 this(model, null, pin1, pin2, path);
143 * Creates a new {@link ModelWire} without automatic interpolation and using the default name.
145 * @author Daniel Kirschten
147 public ModelWire(LogicModelModifiable model, Pin pin1, ModelWireCrossPoint pin2, Point... path)
149 this(model, null, pin1, pin2, path);
153 * Creates a new {@link ModelWire} without automatic interpolation and using the default name.
155 * @author Daniel Kirschten
157 public ModelWire(LogicModelModifiable model, Pin pin1, Pin pin2, Point... path)
159 this(model, null, pin1, pin2, path);
163 * Creates a new {@link ModelWire} with automatic interpolation.
165 * @author Daniel Kirschten
167 public ModelWire(LogicModelModifiable model, String name, ModelWireCrossPoint pin1, ModelWireCrossPoint pin2)
169 this(model, name, pin1, pin2, (Point[]) null);
173 * Creates a new {@link ModelWire} with automatic interpolation.
175 * @author Daniel Kirschten
177 public ModelWire(LogicModelModifiable model, String name, ModelWireCrossPoint pin1, Pin pin2)
179 this(model, name, pin1, pin2, (Point[]) null);
183 * Creates a new {@link ModelWire} with automatic interpolation.
185 * @author Daniel Kirschten
187 public ModelWire(LogicModelModifiable model, String name, Pin pin1, ModelWireCrossPoint pin2)
189 this(model, name, pin1, pin2, (Point[]) null);
193 * Creates a new {@link ModelWire} with automatic interpolation.
195 * @author Daniel Kirschten
197 public ModelWire(LogicModelModifiable model, String name, Pin pin1, Pin pin2)
199 this(model, name, pin1, pin2, (Point[]) null);
203 * Creates a new {@link ModelWire} without automatic interpolation.
205 * @author Daniel Kirschten
207 public ModelWire(LogicModelModifiable model, String name, ModelWireCrossPoint pin1, ModelWireCrossPoint pin2, Point... path)
209 this(model, name, pin1.getPin(), pin2.getPin(), path);
213 * Creates a new {@link ModelWire} without automatic interpolation.
215 * @author Daniel Kirschten
217 public ModelWire(LogicModelModifiable model, String name, ModelWireCrossPoint pin1, Pin pin2, Point... path)
219 this(model, name, pin1.getPin(), pin2, path);
223 * Creates a new {@link ModelWire} without automatic interpolation.
225 * @author Daniel Kirschten
227 public ModelWire(LogicModelModifiable model, String name, Pin pin1, ModelWireCrossPoint pin2, Point... path)
229 this(model, name, pin1, pin2.getPin(), path);
233 * Creates a new {@link ModelWire} without automatic interpolation.
235 * @author Daniel Kirschten
237 public ModelWire(LogicModelModifiable model, String name, Pin pin1, Pin pin2, Point... path)
240 this.name = name == null ? model.getDefaultWireName() : name;
241 this.logicWidth = pin1.logicWidth;
242 if (pin2.logicWidth != pin1.logicWidth)
243 throw new IllegalArgumentException("Can't connect pins of different logic width");
248 this.path = path == null ? null : Arrays.copyOf(path, path.length);
249 this.bounds = new Rectangle(0, 0, -1, -1);
251 pathChangedListeners = new ArrayList<>();
253 logicObs = (i) -> model.requestRedraw();
255 pin1.addPinMovedListener(p -> pinMoved());
256 pin2.addPinMovedListener(p -> pinMoved());
258 recalculateEffectivePath();
260 model.wireCreated(this, this::destroyed);
264 * Destroys this wire. This method is called from {@link LogicModelModifiable#wireDestroyed(ModelWire) wireDestroyed()} of the model
265 * this wire is a part of.
267 * @author Daniel Kirschten
269 private void destroyed()
277 * Returns the {@link Pin} on one side of this wire, usually the signal source.
279 * @author Daniel Kirschten
287 * Returns the {@link Pin} on one side of this wire, usually the signal target.
289 * @author Daniel Kirschten
297 * Called when {@link #pin1} or {@link #pin2} were moved.
299 * @author Daniel Kirschten
301 private void pinMoved()
303 recalculateEffectivePath();
304 model.requestRedraw();
307 // "graphical" operations
310 * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
312 * @author Daniel Kirschten
314 private void recalculateEffectivePath()
316 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
318 double boundsX1 = Math.min(pos1.x, pos2.x);
319 double boundsY1 = Math.min(pos1.y, pos2.y);
320 double boundsX2 = Math.max(pos1.x, pos2.x);
321 double boundsY2 = Math.max(pos1.y, pos2.y);
324 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 };
327 effectivePath = new double[path.length * 2 + 4];
328 effectivePath[0] = pos1.x;
329 effectivePath[1] = pos1.y;
330 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
332 double pathX = path[srcI].x;
333 double pathY = path[srcI].y;
334 effectivePath[dstI + 0] = pathX;
335 effectivePath[dstI + 1] = pathY;
336 if (pathX < boundsX1)
338 if (pathX > boundsX2)
340 if (pathY < boundsY1)
342 if (pathY > boundsY2)
345 effectivePath[effectivePath.length - 2] = pos2.x;
346 effectivePath[effectivePath.length - 1] = pos2.y;
351 bounds.width = boundsX2 - boundsX1;
352 bounds.height = boundsY2 - boundsY1;
356 * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
358 * @author Daniel Kirschten
360 public Rectangle getBounds()
362 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
366 * Render this wire to the given gc, in absoulute coordinates.
368 * @author Daniel Kirschten
370 public void render(GeneralGC gc, RenderPreferences renderPrefs)
372 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(renderPrefs, end);
373 if (wireColor != null)
374 gc.setForeground(ColorManager.current().toColor(wireColor));
375 gc.setLineWidth(renderPrefs.getDouble(logicWidth == 1 ? WIRE_WIDTH_SINGLEBIT : WIRE_WIDTH_MULTIBIT));
376 gc.setLineDash(BitVectorFormatter.formatAsLineDash(renderPrefs, end));
377 gc.drawPolyline(effectivePath);
378 gc.setLineDash(null);
379 gc.setLineWidth(renderPrefs.getDouble(DEFAULT_LINE_WIDTH));
382 // operations concerning the path
385 * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
386 * in {@link #render(GeneralGC)}.<br>
387 * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
390 * @author Daniel Kirschten
392 public Point[] getPath()
394 return deepPathCopy(path);
397 public void setPath(Point... path)
399 this.path = deepPathCopy(path);
400 recalculateEffectivePath();
401 callPathChangedListeners();
402 model.requestRedraw();
405 public Point getPathPoint(int index)
407 return pointCopy(path[index]);
410 public void setPathPoint(Point p, int index)
412 path[index] = pointCopy(p);
413 recalculateEffectivePath();
414 callPathChangedListeners();
415 model.requestRedraw();
418 public void insertPathPoint(Point p, int index)
421 path = new Point[] { pointCopy(p) };
424 Point[] oldPath = path;
425 path = new Point[oldPath.length + 1];
426 System.arraycopy(oldPath, 0, path, 0, index);
427 if (index < oldPath.length)
428 System.arraycopy(oldPath, index, path, index + 1, oldPath.length - index);
429 path[index] = pointCopy(p);
431 recalculateEffectivePath();
432 callPathChangedListeners();
435 public void removePathPoint(int index)
437 if (path.length == 0)
441 Point[] oldPath = path;
442 path = new Point[oldPath.length - 1];
443 System.arraycopy(oldPath, 0, path, 0, index);
444 if (index < oldPath.length - 1)
445 System.arraycopy(oldPath, index + 1, path, index, oldPath.length - index - 1);
447 recalculateEffectivePath();
448 callPathChangedListeners();
451 public double[] getEffectivePath()
453 return Arrays.copyOf(effectivePath, effectivePath.length);
456 private static Point[] deepPathCopy(Point[] path)
460 Point[] copy = new Point[path.length];
461 for (int i = 0; i < path.length; i++)
462 copy[i] = pointCopy(path[i]);
466 private static Point pointCopy(Point p)
468 return new Point(p.x, p.y);
471 // core model binding
474 * Binds this {@link ModelWire} to the given {@link ReadEnd}: The color of this {@link ModelWire} will now depend on the state of the
475 * given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
476 * The argument can be null, in which case the old binding is stopped.
478 * @author Daniel Kirschten
480 public void setCoreModelBinding(ReadEnd end)
482 if (this.end != null)
483 this.end.deregisterObserver(logicObs);
486 end.registerObserver(logicObs);
490 * Returns whether this {@link ModelWire} has a core model binding or not.
492 * @author Daniel Kirschten
494 public boolean hasCoreModelBinding()
500 * If this {@link ModelWire} has a core model binding, delegates to {@link CoreWire#forceValues(BitVector)} for the {@link CoreWire}
501 * corresponding to this {@link ModelWire}.
503 * @author Daniel Kirschten
505 public void forceWireValues(BitVector values)
507 end.getWire().forceValues(values);
511 * If this {@link ModelWire} has a core model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
512 * this {@link ModelWire}.
514 * @author Daniel Kirschten
516 public BitVector getWireValues()
518 return end.getValues();
522 * Registers the given {@link LogicObserver} for the {@link ReadEnd} this {@link ModelWire} is bound to.
524 * @see ReadEnd#registerObserver(LogicObserver)
525 * @author Daniel Kirschten
527 public void addObserver(LogicObserver obs)
529 end.registerObserver(obs);
533 * Deregisters the given {@link LogicObserver} for the {@link ReadEnd} this {@link ModelWire} is bound to.
535 * @see ReadEnd#deregisterObserver(LogicObserver)
536 * @author Daniel Kirschten
538 public void removeObserver(LogicObserver obs)
540 end.deregisterObserver(obs);
546 public void addPathChangedListener (Consumer<ModelWire> listener) {pathChangedListeners.add (listener);}
548 public void removePathChangedListener(Consumer<ModelWire> listener) {pathChangedListeners.remove(listener);}
550 private void callPathChangedListeners() {pathChangedListeners.forEach(l -> l.accept(this));}
554 public String toString()
556 return "ModelWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";