1 package net.mograsim.logic.model.model.wires;
3 import java.util.ArrayList;
5 import java.util.function.Consumer;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.mograsim.logic.model.model.components.GUIComponent;
11 * A connection interface between a GUIComponent and the rest of a ViewModel. Pins usually are created by {@link GUIComponent}s themselves.
13 * A pin has a name identifying it. Pin names are unique for a {@link GUIComponent}: Every pin of a {@link GUIComponent} has a different
14 * name, but different {@link GUIComponent}s can have pins with the same name.
16 * @author Daniel Kirschten
20 // TODO introduce input/output/tristate hints
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;
35 * The X position of this pin, relative to its component's location.
37 protected double relX;
39 * The Y position of this pin, relative to its component's location.
41 protected double relY;
43 private final List<Consumer<? super Pin>> pinMovedListeners;
44 private final List<Runnable> redrawListeners;
46 // creation and destruction
49 * Creates a new pin. Usually it is not needed to call this constructor manually, as {@link GUIComponent}s create their pins themselves.
51 * @author Daniel Kirschten
53 public Pin(GUIComponent component, String name, int logicWidth, double relX, double relY)
55 this.component = component;
57 this.logicWidth = logicWidth;
61 this.pinMovedListeners = new ArrayList<>();
62 this.redrawListeners = new ArrayList<>();
64 component.addComponentMovedListener(c -> callPinMovedListeners());
67 // "graphical" operations
70 * Returns the X position of this pin relative to the position of its component.
72 * @author Daniel Kirschten
74 public double getRelX()
80 * Returns the Y position of this pin relative to the position of its component.
82 * @author Daniel Kirschten
84 public double getRelY()
90 * Returns the position of this pin relative to the position of its component.
92 * @author Daniel Kirschten
94 public Point getRelPos()
96 return new Point(relX, relY);
100 * Returns the absolute position of this pin.
102 * @author Daniel Kirschten
104 public Point getPos()
106 return new Point(relX + component.getPosX(), relY + component.getPosY());
110 * Sets the position of this pin relative to the position of its component.
112 * @author Daniel Kirschten
114 protected void setRelPos(double relX, double relY)
118 callPinMovedListeners();
119 callRedrawListeners();
125 public void addPinMovedListener (Consumer<? super Pin> listener){pinMovedListeners.add (listener);}
126 public void addRedrawListener (Runnable listener){redrawListeners .add (listener);}
128 public void removePinMovedListener(Consumer<? super Pin> listener){pinMovedListeners.remove(listener);}
129 public void removeRedrawListener (Runnable listener){redrawListeners .remove(listener);}
131 private void callPinMovedListeners() {pinMovedListeners.forEach(l -> l.accept(this));}
132 private void callRedrawListeners () {redrawListeners .forEach(l -> l.run ( ));}
136 public String toString()
138 return "Pin [" + name + ", point=" + getPos() + "]";