X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=LogicUI%2Fsrc%2Fera%2Fmi%2Fgui%2Fmodel%2Fwires%2FGUIWire.java;h=c2f03ee56d3e3430ede18856d0eb952ad687448b;hb=e618fa725540dd5f92ccf0ed7d65acdc8eb83f93;hp=649ddb688f586660ed2209c8aafd765cd039c4b1;hpb=f2886cbd57dd08b797921fc2421b41bd92915799;p=Mograsim.git diff --git a/LogicUI/src/era/mi/gui/model/wires/GUIWire.java b/LogicUI/src/era/mi/gui/model/wires/GUIWire.java index 649ddb68..c2f03ee5 100644 --- a/LogicUI/src/era/mi/gui/model/wires/GUIWire.java +++ b/LogicUI/src/era/mi/gui/model/wires/GUIWire.java @@ -1,78 +1,86 @@ package era.mi.gui.model.wires; -import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Color; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import era.mi.gui.ColorHelper; import era.mi.gui.model.ViewModel; -import era.mi.logic.types.Bit; -import era.mi.logic.wires.Wire; +import era.mi.logic.types.BitVectorFormatter; +import era.mi.logic.wires.Wire.ReadEnd; import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.swtobjectwrappers.Point; public class GUIWire { + private final ViewModel model; private Pin pin1; private Pin pin2; private double[] path; - private Wire wire; + private final List> wireChangedListeners; + + private ReadEnd end; public GUIWire(ViewModel model, Pin pin1, Pin pin2, Point... path) { + this.model = model; this.path = new double[path.length * 2 + 4]; for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2) { this.path[dstI + 0] = path[srcI].x; this.path[dstI + 1] = path[srcI].y; } - // TODO support moving pins - Point pos; - pos = pin1.getPos(); - this.path[0] = pos.x; - this.path[1] = pos.y; - pos = pin2.getPos(); - this.path[this.path.length - 2] = pos.x; - this.path[this.path.length - 1] = pos.y; + + this.pin1 = pin1; + this.pin2 = pin2; + + wireChangedListeners = new ArrayList<>(); + + pin1.addPinMovedListener(p -> pin1Moved()); + pin2.addPinMovedListener(p -> pin2Moved()); + pin1Moved(); + pin2Moved(); model.wireCreated(this); } - public void render(GeneralGC gc) + private void pin1Moved() { - Color oldFG = gc.getForeground(); - gc.setForeground(gc.getDevice().getSystemColor(getSWTColorConstantForWire(wire))); - gc.drawPolyline(path); - gc.setForeground(oldFG); + Point pos = pin1.getPos(); + this.path[0] = pos.x; + this.path[1] = pos.y; } - public void setLogicModelWire(Wire wire) + private void pin2Moved() { - this.wire = wire; + Point pos = pin2.getPos(); + this.path[this.path.length - 2] = pos.x; + this.path[this.path.length - 1] = pos.y; } - public static int getSWTColorConstantForWire(Wire wire) + public void destroy() { - if (wire != null && wire.length == 1) - return getSWTColorConstantForBit(wire.getValue()); - else - return SWT.COLOR_BLACK; + model.wireDestroyed(this); } - public static int getSWTColorConstantForBit(Bit bit) + public void render(GeneralGC gc) { - switch (bit) - { - case ONE: - return SWT.COLOR_GREEN; - case ZERO: - return SWT.COLOR_BLUE; - case Z: - return SWT.COLOR_BLACK; - case U: - case X: - return SWT.COLOR_RED; - default: - throw new IllegalArgumentException("Unknown enum constant: " + bit); - } + ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(path)); } + + public void setLogicModelBinding(ReadEnd end) + { + this.end = end; + end.addObserver((i, o) -> callWireChangedListeners()); + } + + // @formatter:off + public void addWireChangedListener (Consumer listener) {wireChangedListeners.add (listener);} + + public void removeWireChangedListener(Consumer listener) {wireChangedListeners.remove(listener);} + + private void callWireChangedListeners() {wireChangedListeners.forEach(l -> l.accept(this));} + // @formatter:on + } \ No newline at end of file