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=de5ba4ac5026bb1eeedfec536434acbd5ddf64da;hb=67c7e16eac6ef555f7ebe0cbe6048598d7f1187e;hp=46262808fb76821df537e638b26e90c65e5d2a2c;hpb=e7193d1fb16edc79e9cc3d8adcfb71caecd8463b;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 46262808..de5ba4ac 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 @@ -1,67 +1,115 @@ package net.mograsim.logic.ui.model.wires; +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.ViewModel; +import net.mograsim.logic.ui.model.ViewModelModifiable; import net.mograsim.logic.ui.model.components.GUIComponent; +import net.mograsim.logic.ui.model.components.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 { + 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; - public WireCrossPoint(ViewModel model, int logicWidth) + // creation and destruction + + public WireCrossPoint(ViewModelModifiable model, int logicWidth) { super(model); logicObs = (i) -> requestRedraw(); - this.logicWidth = logicWidth; - setSize(0, 0); - addPin(this.pin = new Pin(this, logicWidth, 0, 0)); + setSize(CIRCLE_DIAM, CIRCLE_DIAM); + addPin(this.pin = new Pin(this, "", logicWidth, CIRCLE_RADIUS, CIRCLE_RADIUS)); } - @Override - public void render(GeneralGC gc, Rectangle visibleRegion) + // pins + + public Pin getPin() { - Rectangle bounds = getBounds(); - ColorHelper.executeWithDifferentBackground(gc, BitVectorFormatter.formatAsColor(end), - () -> gc.fillOval(bounds.x - 1, bounds.y - 1, 2, 2)); + return pin; } - public void setLogicModelBinding(ReadEnd end) + // "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) { - deregisterLogicObs(this.end); - this.end = end; - registerLogicObs(end); + moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS); } - private void registerLogicObs(LogicObservable observable) + @Override + public void render(GeneralGC gc, Rectangle visibleRegion) { - if (observable != null) - observable.registerObserver(logicObs); + ColorHelper.executeWithDifferentBackground(gc, BitVectorFormatter.formatAsColor(end), + () -> gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM)); } - private void deregisterLogicObs(LogicObservable observable) + // 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) { - if (observable != null) - observable.deregisterObserver(logicObs); + if (this.end != null) + this.end.deregisterObserver(logicObs); + this.end = end; + if (end != null) + end.registerObserver(logicObs); } - public int getLogicWidth() + /** + * Returns whether this {@link WireCrossPoint} has a logic model binding or not. + */ + public boolean hasLogicModelBinding() { - return logicWidth; + return end != null; } - public Pin getPin() + // serializing + + @Override + public Map getInstantiationParameters() { - return pin; + Map m = super.getInstantiationParameters(); + m.put(SimpleRectangularGUIGate.kLogicWidth, pin.logicWidth); + return m; } } \ No newline at end of file