X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fwires%2FPin.java;h=287a6237da3e6aa842e9da8e6171a7ed6c15eca4;hb=553d67a03d7d1688a8467d40ea0c8cff3cbbc1fb;hp=f3a78023ffd5c8f2674ae0009243df0a630f401a;hpb=07faf07e3acb8b2afdc2bf65a46bc868faaed0f8;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/Pin.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/Pin.java index f3a78023..287a6237 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/Pin.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/Pin.java @@ -4,65 +4,136 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import net.mograsim.logic.ui.model.components.GUIComponent; import net.haspamelodica.swt.helper.swtobjectwrappers.Point; -import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; +import net.mograsim.logic.ui.model.components.GUIComponent; +/** + * A connection interface between a GUIComponent and the rest of a ViewModel. Pins usually are created by {@link GUIComponent}s themselves. + *
+ * A pin has a name identifying it. Pin names are unique for a {@link GUIComponent}: Every pin of a {@link GUIComponent} has a different + * name, but different {@link GUIComponent}s can have pins with the same name. + * + * @author Daniel Kirschten + */ public class Pin { + /** + * The {@link GUIComponent} this pin belongs to + */ public final GUIComponent component; + /** + * The name identifying this pin. Is unique for a {@link GUIComponent}. + */ + public final String name; + /** + * The logical width of this pin. Denotes how many bits this pin consists of. + */ public final int logicWidth; + /** + * The X position of this pin, relative to its component's location. + */ protected double relX; + /** + * The Y position of this pin, relative to its component's location. + */ protected double relY; private final List> pinMovedListeners; + private final List redrawListeners; - public Pin(GUIComponent component, int logicWidth, double relX, double relY) + // creation and destruction + + /** + * Creates a new pin. Usually it is not needed to call this constructor manually, as {@link GUIComponent}s create their pins themselves. + * + * @author Daniel Kirschten + */ + public Pin(GUIComponent component, String name, int logicWidth, double relX, double relY) { this.component = component; + this.name = name; this.logicWidth = logicWidth; this.relX = relX; this.relY = relY; this.pinMovedListeners = new ArrayList<>(); + this.redrawListeners = new ArrayList<>(); component.addComponentMovedListener(c -> callPinMovedListeners()); } + // "graphical" operations + + /** + * Returns the X position of this pin relative to the position of its component. + * + * @author Daniel Kirschten + */ public double getRelX() { return relX; } + /** + * Returns the Y position of this pin relative to the position of its component. + * + * @author Daniel Kirschten + */ public double getRelY() { return relY; } + /** + * Returns the position of this pin relative to the position of its component. + * + * @author Daniel Kirschten + */ public Point getRelPos() { return new Point(relX, relY); } + /** + * Returns the absolute position of this pin. + * + * @author Daniel Kirschten + */ public Point getPos() { - Rectangle componentBounds = component.getBounds(); - return new Point(relX + componentBounds.x, relY + componentBounds.y); + return new Point(relX + component.getPosX(), relY + component.getPosY()); + } + + /** + * Sets the position of this pin relative to the position of its component. + * + * @author Daniel Kirschten + */ + protected void setRelPos(double relX, double relY) + { + this.relX = relX; + this.relY = relY; + callPinMovedListeners(); + callRedrawListeners(); } + // listeners + // @formatter:off public void addPinMovedListener (Consumer listener){pinMovedListeners.add (listener);} + public void addRedrawListener (Runnable listener){redrawListeners .add (listener);} public void removePinMovedListener(Consumer listener){pinMovedListeners.remove(listener);} + public void removeRedrawListener (Runnable listener){redrawListeners .remove(listener);} private void callPinMovedListeners() {pinMovedListeners.forEach(l -> l.accept(this));} + private void callRedrawListeners () {redrawListeners .forEach(l -> l.run ( ));} // @formatter:on - protected void setRelPos(double relX, double relY) + @Override + public String toString() { - this.relX = relX; - this.relY = relY; - callPinMovedListeners(); + return "Pin [" + name + ", point=" + getPos() + "]"; } } \ No newline at end of file