1 package net.mograsim.logic.model.model.wires;
3 import java.util.ArrayList;
5 import java.util.Objects;
6 import java.util.function.Consumer;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
9 import net.mograsim.logic.model.model.components.GUIComponent;
12 * A connection interface between a GUIComponent and the rest of a ViewModel. Pins usually are created by {@link GUIComponent}s themselves.
14 * A pin has a name identifying it. Pin names are unique for a {@link GUIComponent}: Every pin of a {@link GUIComponent} has a different
15 * name, but different {@link GUIComponent}s can have pins with the same name.
17 * @author Daniel Kirschten
22 * The {@link GUIComponent} this pin belongs to.
24 public final GUIComponent component;
26 * The name identifying this pin. Is unique for a {@link GUIComponent}.
28 public final String name;
30 * The logical width of this pin. Denotes how many bits this pin consists of.
32 public final int logicWidth;
34 * How this pin is used by the component it belongs to.<br>
35 * Note that this is only a hint.
37 public final PinUsage usage;
40 * The X position of this pin, relative to its component's location.
42 protected double relX;
44 * The Y position of this pin, relative to its component's location.
46 protected double relY;
48 private final List<Consumer<? super Pin>> pinMovedListeners;
49 private final List<Runnable> redrawListeners;
51 // creation and destruction
54 * Creates a new pin. Usually it is not needed to call this constructor manually, as {@link GUIComponent}s create their pins themselves.
56 * @author Daniel Kirschten
58 public Pin(GUIComponent component, String name, int logicWidth, PinUsage usage, double relX, double relY)
60 this.component = component;
62 this.logicWidth = logicWidth;
63 this.usage = Objects.requireNonNull(usage);
67 this.pinMovedListeners = new ArrayList<>();
68 this.redrawListeners = new ArrayList<>();
70 component.addComponentMovedListener(c -> callPinMovedListeners());
73 // "graphical" operations
76 * Returns the X position of this pin relative to the position of its component.
78 * @author Daniel Kirschten
80 public double getRelX()
86 * Returns the Y position of this pin relative to the position of its component.
88 * @author Daniel Kirschten
90 public double getRelY()
96 * Returns the position of this pin relative to the position of its component.
98 * @author Daniel Kirschten
100 public Point getRelPos()
102 return new Point(relX, relY);
106 * Returns the absolute position of this pin.
108 * @author Daniel Kirschten
110 public Point getPos()
112 return new Point(relX + component.getPosX(), relY + component.getPosY());
116 * Sets the position of this pin relative to the position of its component.
118 * @author Daniel Kirschten
120 protected void setRelPos(double relX, double relY)
124 callPinMovedListeners();
125 callRedrawListeners();
131 public void addPinMovedListener (Consumer<? super Pin> listener){pinMovedListeners.add (listener);}
132 public void addRedrawListener (Runnable listener){redrawListeners .add (listener);}
134 public void removePinMovedListener(Consumer<? super Pin> listener){pinMovedListeners.remove(listener);}
135 public void removeRedrawListener (Runnable listener){redrawListeners .remove(listener);}
137 private void callPinMovedListeners() {pinMovedListeners.forEach(l -> l.accept(this));}
138 private void callRedrawListeners () {redrawListeners .forEach(l -> l.run ( ));}
142 public String toString()
144 return "Pin [" + name + ", point=" + getPos() + "]";