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
21 // TODO introduce input/output/tristate hints
23 * The {@link GUIComponent} this pin belongs to.
25 public final GUIComponent component;
27 * The name identifying this pin. Is unique for a {@link GUIComponent}.
29 public final String name;
31 * The logical width of this pin. Denotes how many bits this pin consists of.
33 public final int logicWidth;
35 * How this pin is used by the component it belongs to.<br>
36 * Note that this is only a hint.
38 public final PinUsage usage;
41 * The X position of this pin, relative to its component's location.
43 protected double relX;
45 * The Y position of this pin, relative to its component's location.
47 protected double relY;
49 private final List<Consumer<? super Pin>> pinMovedListeners;
50 private final List<Runnable> redrawListeners;
52 // creation and destruction
55 * Creates a new pin. Usually it is not needed to call this constructor manually, as {@link GUIComponent}s create their pins themselves.
57 * @author Daniel Kirschten
59 public Pin(GUIComponent component, String name, int logicWidth, PinUsage usage, double relX, double relY)
61 this.component = component;
63 this.logicWidth = logicWidth;
64 this.usage = Objects.requireNonNull(usage);
68 this.pinMovedListeners = new ArrayList<>();
69 this.redrawListeners = new ArrayList<>();
71 component.addComponentMovedListener(c -> callPinMovedListeners());
74 // "graphical" operations
77 * Returns the X position of this pin relative to the position of its component.
79 * @author Daniel Kirschten
81 public double getRelX()
87 * Returns the Y position of this pin relative to the position of its component.
89 * @author Daniel Kirschten
91 public double getRelY()
97 * Returns the position of this pin relative to the position of its component.
99 * @author Daniel Kirschten
101 public Point getRelPos()
103 return new Point(relX, relY);
107 * Returns the absolute position of this pin.
109 * @author Daniel Kirschten
111 public Point getPos()
113 return new Point(relX + component.getPosX(), relY + component.getPosY());
117 * Sets the position of this pin relative to the position of its component.
119 * @author Daniel Kirschten
121 protected void setRelPos(double relX, double relY)
125 callPinMovedListeners();
126 callRedrawListeners();
132 public void addPinMovedListener (Consumer<? super Pin> listener){pinMovedListeners.add (listener);}
133 public void addRedrawListener (Runnable listener){redrawListeners .add (listener);}
135 public void removePinMovedListener(Consumer<? super Pin> listener){pinMovedListeners.remove(listener);}
136 public void removeRedrawListener (Runnable listener){redrawListeners .remove(listener);}
138 private void callPinMovedListeners() {pinMovedListeners.forEach(l -> l.accept(this));}
139 private void callRedrawListeners () {redrawListeners .forEach(l -> l.run ( ));}
143 public String toString()
145 return "Pin [" + name + ", point=" + getPos() + "]";