1 package net.mograsim.logic.ui.model.wires;
3 import java.util.ArrayList;
4 import java.util.Arrays;
7 import net.haspamelodica.swt.helper.gcs.GeneralGC;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
9 import net.mograsim.logic.core.LogicObservable;
10 import net.mograsim.logic.core.LogicObserver;
11 import net.mograsim.logic.core.types.BitVectorFormatter;
12 import net.mograsim.logic.core.wires.Wire.ReadEnd;
13 import net.mograsim.logic.ui.ColorHelper;
14 import net.mograsim.logic.ui.model.ViewModelModifiable;
18 private final ViewModelModifiable model;
19 public final int logicWidth;
23 private double[] effectivePath;
25 private final List<Runnable> redrawListeners;
27 private final LogicObserver logicObs;
30 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
32 this(model, pin1, pin2, (Point[]) null);
35 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
37 this(model, pin1, pin2, (Point[]) null);
40 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
42 this(model, pin1, pin2, (Point[]) null);
45 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
47 this(model, pin1, pin2, (Point[]) null);
50 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
52 this(model, pin1.getPin(), pin2.getPin(), path);
55 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
57 this(model, pin1.getPin(), pin2, path);
60 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
62 this(model, pin1, pin2.getPin(), path);
65 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
67 logicObs = (i) -> callRedrawListeners();
69 this.logicWidth = pin1.logicWidth;
70 if (pin2.logicWidth != pin1.logicWidth)
71 throw new IllegalArgumentException("Can't connect pins of different logic width");
76 this.path = path == null ? null : Arrays.copyOf(path, path.length);
78 redrawListeners = new ArrayList<>();
80 pin1.addPinMovedListener(p -> pin1Moved());
81 pin2.addPinMovedListener(p -> pin2Moved());
83 recalculateEffectivePath();
85 model.wireCreated(this);
88 private void recalculateEffectivePath()
90 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
92 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 };
95 effectivePath = new double[path.length * 2 + 4];
96 effectivePath[0] = pos1.x;
97 effectivePath[1] = pos1.y;
98 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
100 effectivePath[dstI + 0] = path[srcI].x;
101 effectivePath[dstI + 1] = path[srcI].y;
103 effectivePath[effectivePath.length - 2] = pos2.x;
104 effectivePath[effectivePath.length - 1] = pos2.y;
108 private void pin1Moved()
110 recalculateEffectivePath();
111 callRedrawListeners();
114 private void pin2Moved()
116 recalculateEffectivePath();
117 callRedrawListeners();
120 public void destroy()
122 model.wireDestroyed(this);
125 public void render(GeneralGC gc)
127 ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath));
130 public void setLogicModelBinding(ReadEnd end)
132 deregisterLogicObs(this.end);
134 registerLogicObs(end);
137 private void registerLogicObs(LogicObservable observable)
139 if (observable != null)
140 observable.registerObserver(logicObs);
143 private void deregisterLogicObs(LogicObservable observable)
145 if (observable != null)
146 observable.deregisterObserver(logicObs);
160 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
162 public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);}
164 private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}