X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fwires%2FWireCrossPoint.java;h=6235f343d6a82906397a3ddecd7c6f51b6b5c10c;hb=01c5d7035474a5eb58f216b6831b2c0d8c174efa;hp=1124233bc000ce139e21bf97b1eabfd5676aac2c;hpb=a19dde054ec5cab3c835840d569880ee8ee156dc;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java index 1124233b..6235f343 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java @@ -4,36 +4,68 @@ import java.util.Map; import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; -import net.mograsim.logic.core.LogicObservable; import net.mograsim.logic.core.LogicObserver; import net.mograsim.logic.core.types.BitVectorFormatter; import net.mograsim.logic.core.wires.Wire.ReadEnd; import net.mograsim.logic.ui.ColorHelper; import net.mograsim.logic.ui.model.ViewModelModifiable; import net.mograsim.logic.ui.model.components.GUIComponent; -import net.mograsim.logic.ui.model.components.SimpleRectangularGUIGate; - -public class WireCrossPoint extends GUIComponent +import net.mograsim.logic.ui.model.components.atomic.SimpleRectangularGUIGate; + +/** + * A {@link GUIComponent} with only one pin. Is used to create wires connecting more than two pins.
+ * Example: There are three pins P1, P2, P3 that need to be connected. Solution: Create a + * WireCrossPoint (WCP) and create the GUIWires P1-WCP, P2-WCP, + * P3-WCP.
+ * Cross points are drawn as circles. The pin of cross points is in the center of this circle. + * + * @author Daniel Kirschten + */ +public class WireCrossPoint extends GUIComponent implements ConnectionPoint { private static final int CIRCLE_RADIUS = 1; private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2; + /** + * The (single) pin of this cross point. + */ private final Pin pin; - private final int logicWidth; + /** + * A {@link LogicObserver} calling {@link #requestRedraw()}. + */ private final LogicObserver logicObs; + /** + * The {@link ReadEnd} currently bound to this cross point. + */ private ReadEnd end; + // creation and destruction + public WireCrossPoint(ViewModelModifiable model, int logicWidth) { super(model); logicObs = (i) -> requestRedraw(); - this.logicWidth = logicWidth; setSize(CIRCLE_DIAM, CIRCLE_DIAM); addPin(this.pin = new Pin(this, "", logicWidth, CIRCLE_RADIUS, CIRCLE_RADIUS)); } + // pins + + @Override + public Pin getPin() + { + return pin; + } + + // "graphical" operations + + /** + * Moves the center (and therefore the pin) of this {@link WireCrossPoint} to the given location. + * + * @author Daniel Kirschten + */ public void moveCenterTo(double x, double y) { moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS); @@ -42,46 +74,43 @@ public class WireCrossPoint extends GUIComponent @Override public void render(GeneralGC gc, Rectangle visibleRegion) { - Rectangle bounds = getBounds(); ColorHelper.executeWithDifferentBackground(gc, BitVectorFormatter.formatAsColor(end), - () -> gc.fillOval(bounds.x, bounds.y, CIRCLE_DIAM, CIRCLE_DIAM)); + () -> gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM)); } + // logic model binding + + /** + * Binds this {@link WireCrossPoint} to the given {@link ReadEnd}: The color of this {@link WireCrossPoint} will now depend on the state + * of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.
+ * The argument can be null, in which case the old binding is stopped. + * + * @author Daniel Kirschten + */ public void setLogicModelBinding(ReadEnd end) { - deregisterLogicObs(this.end); + if (this.end != null) + this.end.deregisterObserver(logicObs); this.end = end; - registerLogicObs(end); + if (end != null) + end.registerObserver(logicObs); } - private void registerLogicObs(LogicObservable observable) + /** + * Returns whether this {@link WireCrossPoint} has a logic model binding or not. + */ + public boolean hasLogicModelBinding() { - if (observable != null) - observable.registerObserver(logicObs); + return end != null; } - private void deregisterLogicObs(LogicObservable observable) - { - if (observable != null) - observable.deregisterObserver(logicObs); - } - - public int getLogicWidth() - { - return logicWidth; - } - - public Pin getPin() - { - return pin; - } + // serializing @Override public Map getInstantiationParameters() { Map m = super.getInstantiationParameters(); - m.put(SimpleRectangularGUIGate.kLogicWidth, logicWidth); + m.put(SimpleRectangularGUIGate.kLogicWidth, pin.logicWidth); return m; } - } \ No newline at end of file