Renamed ViewModel to LogicModel
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / wires / Pin.java
1 package net.mograsim.logic.model.model.wires;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Objects;
6 import java.util.function.Consumer;
7
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
9 import net.mograsim.logic.model.model.components.ModelComponent;
10
11 /**
12  * A connection interface between a ModelComponent and the rest of a LogicModel. Pins usually are created by {@link ModelComponent}s
13  * themselves. <br>
14  * A pin has a name identifying it. Pin names are unique for a {@link ModelComponent}: Every pin of a {@link ModelComponent} has a different
15  * name, but different {@link ModelComponent}s can have pins with the same name.
16  * 
17  * @author Daniel Kirschten
18  */
19 public class Pin
20 {
21         /**
22          * The {@link ModelComponent} this pin belongs to.
23          */
24         public final ModelComponent component;
25         /**
26          * The name identifying this pin. Is unique for a {@link ModelComponent}.
27          */
28         public final String name;
29         /**
30          * The logical width of this pin. Denotes how many bits this pin consists of.
31          */
32         public final int logicWidth;
33         /**
34          * How this pin is used by the component it belongs to.<br>
35          * Note that this is only a hint.
36          */
37         public final PinUsage usage;
38
39         /**
40          * The X position of this pin, relative to its component's location.
41          */
42         protected double relX;
43         /**
44          * The Y position of this pin, relative to its component's location.
45          */
46         protected double relY;
47
48         private final List<Consumer<? super Pin>> pinMovedListeners;
49         private final List<Runnable> redrawListeners;
50
51         // creation and destruction
52
53         /**
54          * Creates a new pin. Usually it is not needed to call this constructor manually, as {@link ModelComponent}s create their pins
55          * themselves.
56          * 
57          * @author Daniel Kirschten
58          */
59         public Pin(ModelComponent component, String name, int logicWidth, PinUsage usage, double relX, double relY)
60         {
61                 this.component = component;
62                 this.name = name;
63                 this.logicWidth = logicWidth;
64                 this.usage = Objects.requireNonNull(usage);
65                 this.relX = relX;
66                 this.relY = relY;
67
68                 this.pinMovedListeners = new ArrayList<>();
69                 this.redrawListeners = new ArrayList<>();
70
71                 component.addComponentMovedListener(c -> callPinMovedListeners());
72         }
73
74         // "graphical" operations
75
76         /**
77          * Returns the X position of this pin relative to the position of its component.
78          * 
79          * @author Daniel Kirschten
80          */
81         public double getRelX()
82         {
83                 return relX;
84         }
85
86         /**
87          * Returns the Y position of this pin relative to the position of its component.
88          * 
89          * @author Daniel Kirschten
90          */
91         public double getRelY()
92         {
93                 return relY;
94         }
95
96         /**
97          * Returns the position of this pin relative to the position of its component.
98          * 
99          * @author Daniel Kirschten
100          */
101         public Point getRelPos()
102         {
103                 return new Point(relX, relY);
104         }
105
106         /**
107          * Returns the absolute position of this pin.
108          * 
109          * @author Daniel Kirschten
110          */
111         public Point getPos()
112         {
113                 return new Point(relX + component.getPosX(), relY + component.getPosY());
114         }
115
116         /**
117          * Sets the position of this pin relative to the position of its component.
118          * 
119          * @author Daniel Kirschten
120          */
121         protected void setRelPos(double relX, double relY)
122         {
123                 this.relX = relX;
124                 this.relY = relY;
125                 callPinMovedListeners();
126                 callRedrawListeners();
127         }
128
129         // listeners
130
131         // @formatter:off
132         public void addPinMovedListener   (Consumer<? super Pin> listener){pinMovedListeners.add   (listener);}
133         public void addRedrawListener     (Runnable              listener){redrawListeners  .add   (listener);}
134
135         public void removePinMovedListener(Consumer<? super Pin> listener){pinMovedListeners.remove(listener);}
136         public void removeRedrawListener  (Runnable              listener){redrawListeners  .remove(listener);}
137
138         private void callPinMovedListeners() {pinMovedListeners.forEach(l -> l.accept(this));}
139         private void callRedrawListeners  () {redrawListeners  .forEach(l -> l.run   (    ));}
140         // @formatter:on
141
142         @Override
143         public String toString()
144         {
145                 return "Pin [" + name + ", point=" + getPos() + "]";
146         }
147 }