X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fwires%2FGUIWire.java;h=86a545ddfa191951ec1408a106d48d0ce7a61a68;hb=da27eee93b1893199dde2b0935cafdb1c6301279;hp=736c0ea67d3ef2253695e45ff9e4466d55b8a1d2;hpb=218ed3e44595ad3534c33e05b43a55cc1a67e851;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/GUIWire.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/GUIWire.java index 736c0ea6..86a545dd 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/GUIWire.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/GUIWire.java @@ -1,100 +1,100 @@ -package net.mograsim.logic.ui.model.wires; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.Consumer; - -import net.mograsim.logic.ui.ColorHelper; -import net.mograsim.logic.ui.model.ViewModel; -import net.haspamelodica.swt.helper.gcs.GeneralGC; -import net.haspamelodica.swt.helper.swtobjectwrappers.Point; -import net.mograsim.logic.core.types.BitVectorFormatter; -import net.mograsim.logic.core.wires.Wire.ReadEnd; - -public class GUIWire -{ - private final ViewModel model; - public final int logicWidth; - private Pin pin1; - private Pin pin2; - private double[] path; - - private final List> wireLookChangedListeners; - - private ReadEnd end; - - public GUIWire(ViewModel model, Pin pin1, Pin pin2, Point... path) - { - this.model = model; - this.logicWidth = pin1.logicWidth; - if (pin2.logicWidth != pin1.logicWidth) - throw new IllegalArgumentException("Can't connect pins of different logic width"); - 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; - } - - this.pin1 = pin1; - this.pin2 = pin2; - - wireLookChangedListeners = new ArrayList<>(); - - pin1.addPinMovedListener(p -> pin1Moved()); - pin2.addPinMovedListener(p -> pin2Moved()); - pin1Moved(); - pin2Moved(); - - model.wireCreated(this); - } - - private void pin1Moved() - { - Point pos = pin1.getPos(); - this.path[0] = pos.x; - this.path[1] = pos.y; - } - - private void pin2Moved() - { - Point pos = pin2.getPos(); - this.path[this.path.length - 2] = pos.x; - this.path[this.path.length - 1] = pos.y; - } - - public void destroy() - { - model.wireDestroyed(this); - } - - public void render(GeneralGC gc) - { - ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(path)); - } - - public void setLogicModelBinding(ReadEnd end) - { - this.end = end; - end.addObserver((i, o) -> callWireLookChangedListeners()); - } - - public Pin getPin1() - { - return pin1; - } - - public Pin getPin2() - { - return pin2; - } - - // @formatter:off - public void addWireLookChangedListener (Consumer listener) {wireLookChangedListeners.add (listener);} - - public void removeWireLookChangedListener(Consumer listener) {wireLookChangedListeners.remove(listener);} - - private void callWireLookChangedListeners() {wireLookChangedListeners.forEach(l -> l.accept(this));} - // @formatter:on - +package net.mograsim.logic.ui.model.wires; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import net.mograsim.logic.ui.ColorHelper; +import net.mograsim.logic.ui.model.ViewModel; +import net.haspamelodica.swt.helper.gcs.GeneralGC; +import net.haspamelodica.swt.helper.swtobjectwrappers.Point; +import net.mograsim.logic.core.types.BitVectorFormatter; +import net.mograsim.logic.core.wires.Wire.ReadEnd; + +public class GUIWire +{ + private final ViewModel model; + public final int logicWidth; + private Pin pin1; + private Pin pin2; + private double[] path; + + private final List> wireLookChangedListeners; + + private ReadEnd end; + + public GUIWire(ViewModel model, Pin pin1, Pin pin2, Point... path) + { + this.model = model; + this.logicWidth = pin1.logicWidth; + if (pin2.logicWidth != pin1.logicWidth) + throw new IllegalArgumentException("Can't connect pins of different logic width"); + 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; + } + + this.pin1 = pin1; + this.pin2 = pin2; + + wireLookChangedListeners = new ArrayList<>(); + + pin1.addPinMovedListener(p -> pin1Moved()); + pin2.addPinMovedListener(p -> pin2Moved()); + pin1Moved(); + pin2Moved(); + + model.wireCreated(this); + } + + private void pin1Moved() + { + Point pos = pin1.getPos(); + this.path[0] = pos.x; + this.path[1] = pos.y; + } + + private void pin2Moved() + { + Point pos = pin2.getPos(); + this.path[this.path.length - 2] = pos.x; + this.path[this.path.length - 1] = pos.y; + } + + public void destroy() + { + model.wireDestroyed(this); + } + + public void render(GeneralGC gc) + { + ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(path)); + } + + public void setLogicModelBinding(ReadEnd end) + { + this.end = end; + end.registerObserver((i) -> callWireLookChangedListeners()); + } + + public Pin getPin1() + { + return pin1; + } + + public Pin getPin2() + { + return pin2; + } + + // @formatter:off + public void addWireLookChangedListener (Consumer listener) {wireLookChangedListeners.add (listener);} + + public void removeWireLookChangedListener(Consumer listener) {wireLookChangedListeners.remove(listener);} + + private void callWireLookChangedListeners() {wireLookChangedListeners.forEach(l -> l.accept(this));} + // @formatter:on + } \ No newline at end of file