1 package net.mograsim.logic.ui.model.wires;
3 import java.util.ArrayList;
6 import net.haspamelodica.swt.helper.gcs.GeneralGC;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.mograsim.logic.core.LogicObservable;
9 import net.mograsim.logic.core.LogicObserver;
10 import net.mograsim.logic.core.types.BitVectorFormatter;
11 import net.mograsim.logic.core.wires.Wire.ReadEnd;
12 import net.mograsim.logic.ui.ColorHelper;
13 import net.mograsim.logic.ui.model.ViewModelModifiable;
17 private final ViewModelModifiable model;
18 public final int logicWidth;
21 private double[] path;
23 private final List<Runnable> redrawListeners;
25 private final LogicObserver logicObs;
28 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
30 this(model, pin1.getPin(), pin2.getPin(), path);
33 public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
35 this(model, pin1.getPin(), pin2, path);
38 public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
40 this(model, pin1, pin2.getPin(), path);
43 public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
45 logicObs = (i) -> callRedrawListeners();
47 this.logicWidth = pin1.logicWidth;
48 if (pin2.logicWidth != pin1.logicWidth)
49 throw new IllegalArgumentException("Can't connect pins of different logic width");
53 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
54 path = new Point[] { new Point((pos1.x + pos2.x) / 2, pos1.y), new Point((pos1.x + pos2.x) / 2, pos2.y) };
62 redrawListeners = new ArrayList<>();
64 pin1.addPinMovedListener(p -> pin1Moved());
65 pin2.addPinMovedListener(p -> pin2Moved());
69 model.wireCreated(this);
72 private void applyPath(Point... path)
74 this.path = new double[path.length * 2 + 4];
76 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
78 this.path[dstI + 0] = path[srcI].x;
79 this.path[dstI + 1] = path[srcI].y;
83 private void pin1Moved()
85 Point pos = pin1.getPos();
88 callRedrawListeners();
91 private void pin2Moved()
93 Point pos = pin2.getPos();
94 this.path[this.path.length - 2] = pos.x;
95 this.path[this.path.length - 1] = pos.y;
96 callRedrawListeners();
101 model.wireDestroyed(this);
104 public void render(GeneralGC gc)
106 ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(path));
109 public void setLogicModelBinding(ReadEnd end)
111 deregisterLogicObs(this.end);
113 registerLogicObs(end);
116 private void registerLogicObs(LogicObservable observable)
118 if (observable != null)
119 observable.registerObserver(logicObs);
122 private void deregisterLogicObs(LogicObservable observable)
124 if (observable != null)
125 observable.deregisterObserver(logicObs);
139 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
141 public void removeRedrawListener(Runnable listener) {redrawListeners .remove(listener);}
143 private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}