33be875fddc4e9c0cb12f524532b6d7b3ddd9eec
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / wires / GUIWire.java
1 package net.mograsim.logic.ui.model.wires;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import org.eclipse.swt.SWT;
8
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.core.LogicObserver;
13 import net.mograsim.logic.core.types.BitVector;
14 import net.mograsim.logic.core.types.BitVectorFormatter;
15 import net.mograsim.logic.core.wires.Wire;
16 import net.mograsim.logic.core.wires.Wire.ReadEnd;
17 import net.mograsim.logic.ui.ColorHelper;
18 import net.mograsim.logic.ui.model.ViewModelModifiable;
19
20 /**
21  * A wire connecting exactly two {@link Pin}s.
22  * 
23  * @author Daniel Kirschten
24  */
25 public class GUIWire
26 {
27         /**
28          * The model this wire is a part of.
29          */
30         private final ViewModelModifiable model;
31         /**
32          * The logical width of this wire. Is equal to the logical with of {@link #pin1} and {@link #pin2}.
33          */
34         public final int logicWidth;
35         /**
36          * The {@link Pin} on one side of this wire, usually the signal source.
37          */
38         private Pin pin1;
39         /**
40          * The {@link Pin} on one side of this wire, usually the signal target.
41          */
42         private Pin pin2;
43         /**
44          * The user-defined path between {@link #pin1} and {@link #pin2}.<br>
45          * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
46          * interpolation".
47          */
48         private Point[] path;
49         /**
50          * The bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
51          */
52         private final Rectangle bounds;
53         /**
54          * The effective path of this wire, including automatic interpolation and the position of both {@link Pin}s. Is never null.
55          */
56         private double[] effectivePath;
57
58         private final List<Runnable> redrawListeners;
59
60         /**
61          * A LogicObserver calling redrawListeners. Used for logic model bindings.
62          */
63         private final LogicObserver logicObs;
64         /**
65          * A ReadEnd of the logic wire this GUI wire currently is bound to.
66          */
67         private ReadEnd end;
68
69         // creation and destruction
70
71         /**
72          * Creates a new {@link GUIWire} with automatic interpolation.
73          * 
74          * @author Daniel Kirschten
75          */
76         public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
77         {
78                 this(model, pin1, pin2, (Point[]) null);
79         }
80
81         /**
82          * Creates a new {@link GUIWire} with automatic interpolation.
83          * 
84          * @author Daniel Kirschten
85          */
86         public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
87         {
88                 this(model, pin1, pin2, (Point[]) null);
89         }
90
91         /**
92          * Creates a new {@link GUIWire} with automatic interpolation.
93          * 
94          * @author Daniel Kirschten
95          */
96         public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
97         {
98                 this(model, pin1, pin2, (Point[]) null);
99         }
100
101         /**
102          * Creates a new {@link GUIWire} with automatic interpolation.
103          * 
104          * @author Daniel Kirschten
105          */
106         public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
107         {
108                 this(model, pin1, pin2, (Point[]) null);
109         }
110
111         /**
112          * Creates a new {@link GUIWire} without automatic interpolation.
113          * 
114          * @author Daniel Kirschten
115          */
116         public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
117         {
118                 this(model, pin1.getPin(), pin2.getPin(), path);
119         }
120
121         /**
122          * Creates a new {@link GUIWire} without automatic interpolation.
123          * 
124          * @author Daniel Kirschten
125          */
126         public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
127         {
128                 this(model, pin1.getPin(), pin2, path);
129         }
130
131         /**
132          * Creates a new {@link GUIWire} without automatic interpolation.
133          * 
134          * @author Daniel Kirschten
135          */
136         public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
137         {
138                 this(model, pin1, pin2.getPin(), path);
139         }
140
141         /**
142          * Creates a new {@link GUIWire} without automatic interpolation.
143          * 
144          * @author Daniel Kirschten
145          */
146         public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
147         {
148                 logicObs = (i) -> callRedrawListeners();
149                 this.model = model;
150                 this.logicWidth = pin1.logicWidth;
151                 if (pin2.logicWidth != pin1.logicWidth)
152                         throw new IllegalArgumentException("Can't connect pins of different logic width");
153
154                 this.pin1 = pin1;
155                 this.pin2 = pin2;
156
157                 this.path = path == null ? null : Arrays.copyOf(path, path.length);
158                 this.bounds = new Rectangle(0, 0, -1, -1);
159
160                 redrawListeners = new ArrayList<>();
161
162                 pin1.addPinMovedListener(p -> pinMoved());
163                 pin2.addPinMovedListener(p -> pinMoved());
164
165                 recalculateEffectivePath();
166
167                 model.wireCreated(this);
168         }
169
170         /**
171          * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
172          * this component is a part of.
173          * 
174          * @author Daniel Kirschten
175          */
176         public void destroy()
177         {
178                 model.wireDestroyed(this);
179         }
180
181         // pins
182
183         /**
184          * Returns the {@link Pin} on one side of this wire, usually the signal source.
185          * 
186          * @author Daniel Kirschten
187          */
188         public Pin getPin1()
189         {
190                 return pin1;
191         }
192
193         /**
194          * Returns the {@link Pin} on one side of this wire, usually the signal target.
195          * 
196          * @author Daniel Kirschten
197          */
198         public Pin getPin2()
199         {
200                 return pin2;
201         }
202
203         /**
204          * Called when {@link #pin1} or {@link #pin2} were moved.
205          * 
206          * @author Daniel Kirschten
207          */
208         private void pinMoved()
209         {
210                 recalculateEffectivePath();
211                 callRedrawListeners();
212         }
213
214         // "graphical" operations
215
216         /**
217          * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
218          * 
219          * @author Daniel Kirschten
220          */
221         private void recalculateEffectivePath()
222         {
223                 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
224
225                 double boundsX1 = Math.min(pos1.x, pos2.x);
226                 double boundsY1 = Math.min(pos1.y, pos2.y);
227                 double boundsX2 = Math.max(pos1.x, pos2.x);
228                 double boundsY2 = Math.max(pos1.y, pos2.y);
229
230                 if (path == null)
231                         effectivePath = new double[] { pos1.x, pos1.y, (pos1.x + pos2.x) / 2, pos1.y, (pos1.x + pos2.x) / 2, pos2.y, pos2.x, pos2.y };
232                 else
233                 {
234                         effectivePath = new double[path.length * 2 + 4];
235                         effectivePath[0] = pos1.x;
236                         effectivePath[1] = pos1.y;
237                         for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
238                         {
239                                 double pathX = path[srcI].x;
240                                 double pathY = path[srcI].y;
241                                 effectivePath[dstI + 0] = pathX;
242                                 effectivePath[dstI + 1] = pathY;
243                                 if (pathX < boundsX1)
244                                         boundsX1 = pathX;
245                                 if (pathX > boundsX2)
246                                         boundsX2 = pathX;
247                                 if (pathY < boundsY1)
248                                         boundsY1 = pathY;
249                                 if (pathY > boundsY2)
250                                         boundsY2 = pathY;
251                         }
252                         effectivePath[effectivePath.length - 2] = pos2.x;
253                         effectivePath[effectivePath.length - 1] = pos2.y;
254                 }
255
256                 bounds.x = boundsX1;
257                 bounds.y = boundsY1;
258                 bounds.width = boundsX2 - boundsX1;
259                 bounds.height = boundsY2 - boundsY1;
260         }
261
262         /**
263          * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
264          * 
265          * @author Daniel Kirschten
266          */
267         public Rectangle getBounds()
268         {
269                 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
270         }
271
272         /**
273          * Render this wire to the given gc, in absoulute coordinates.
274          * 
275          * @author Daniel Kirschten
276          */
277         public void render(GeneralGC gc)
278         {
279                 ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath));
280         }
281
282         /**
283          * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
284          * in {@link #render(GeneralGC)}.<br>
285          * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
286          * interpolation".
287          * 
288          * @author Daniel Kirschten
289          */
290         public Point[] getPath()
291         {
292                 return path == null ? null : path.clone();
293         }
294
295         // logic model binding
296
297         /**
298          * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
299          * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
300          * The argument can be null, in which case the old binding is stopped.
301          * 
302          * @author Daniel Kirschten
303          */
304         public void setLogicModelBinding(ReadEnd end)
305         {
306                 if (this.end != null)
307                         this.end.deregisterObserver(logicObs);
308                 this.end = end;
309                 if (end != null)
310                         end.registerObserver(logicObs);
311         }
312
313         /**
314          * Returns whether this {@link GUIWire} has a logic model binding or not.
315          * 
316          * @author Daniel Kirschten
317          */
318         public boolean hasLogicModelBinding()
319         {
320                 return end != null;
321         }
322
323         /**
324          * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
325          * corresponding to this {@link GUIWire}.
326          * 
327          * @author Daniel Kirschten
328          */
329         public void forceWireValues(BitVector values)
330         {
331                 end.getWire().forceValues(values);
332         }
333
334         /**
335          * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
336          * this {@link GUIWire}.
337          * 
338          * @author Daniel Kirschten
339          */
340         public BitVector getWireValues()
341         {
342                 return end.getValues();
343         }
344
345         // listeners
346
347         // @formatter:off
348         public void addRedrawListener   (Runnable listener) {redrawListeners         .add   (listener);}
349
350         public void removeRedrawListener(Runnable listener) {redrawListeners         .remove(listener);}
351
352         private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
353         // @formatter:on
354
355         @Override
356         public String toString()
357         {
358                 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";
359         }
360 }