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