1 package net.mograsim.logic.ui.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.ui.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
21 * The {@link GUIComponent} this pin belongs to
23 public final GUIComponent component;
25 * The name identifying this pin. Is unique for a {@link GUIComponent}.
27 public final String name;
29 * The logical width of this pin. Denotes how many bits this pin consists of.
31 public final int logicWidth;
34 * The X position of this pin, relative to its component's location.
36 protected double relX;
38 * The Y position of this pin, relative to its component's location.
40 protected double relY;
42 private final List<Consumer<? super Pin>> pinMovedListeners;
43 private final List<Runnable> redrawListeners;
45 // creation and destruction
48 * Creates a new pin. Usually it is not needed to call this constructor manually, as {@link GUIComponent}s create their pins themselves.
50 * @author Daniel Kirschten
52 public Pin(GUIComponent component, String name, int logicWidth, double relX, double relY)
54 this.component = component;
56 this.logicWidth = logicWidth;
60 this.pinMovedListeners = new ArrayList<>();
61 this.redrawListeners = new ArrayList<>();
63 component.addComponentMovedListener(c -> callPinMovedListeners());
66 // "graphical" operations
69 * Returns the X position of this pin relative to the position of its component.
71 * @author Daniel Kirschten
73 public double getRelX()
79 * Returns the Y position of this pin relative to the position of its component.
81 * @author Daniel Kirschten
83 public double getRelY()
89 * Returns the position of this pin relative to the position of its component.
91 * @author Daniel Kirschten
93 public Point getRelPos()
95 return new Point(relX, relY);
99 * Returns the absolute position of this pin.
101 * @author Daniel Kirschten
103 public Point getPos()
105 return new Point(relX + component.getPosX(), relY + component.getPosY());
109 * Sets the position of this pin relative to the position of its component.
111 * @author Daniel Kirschten
113 protected void setRelPos(double relX, double relY)
117 callPinMovedListeners();
118 callRedrawListeners();
124 public void addPinMovedListener (Consumer<? super Pin> listener){pinMovedListeners.add (listener);}
125 public void addRedrawListener (Runnable listener){redrawListeners .add (listener);}
127 public void removePinMovedListener(Consumer<? super Pin> listener){pinMovedListeners.remove(listener);}
128 public void removeRedrawListener (Runnable listener){redrawListeners .remove(listener);}
130 private void callPinMovedListeners() {pinMovedListeners.forEach(l -> l.accept(this));}
131 private void callRedrawListeners () {redrawListeners .forEach(l -> l.run ( ));}
135 public String toString()
137 return "Pin [" + name + ", point=" + getPos() + "]";