Moved code serializing a symbol renderer to where it belongs
[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.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, ConnectionPoint pin1, ConnectionPoint pin2)
78         {
79                 this(model, pin1, pin2, (Point[]) null);
80         }
81
82         /**
83          * Creates a new {@link GUIWire} without automatic interpolation.
84          * 
85          * @author Daniel Kirschten
86          */
87         public GUIWire(ViewModelModifiable model, ConnectionPoint pin1, ConnectionPoint pin2, Point... path)
88         {
89                 this(model, pin1.getPin(), pin2.getPin(), path);
90         }
91
92         /**
93          * Creates a new {@link GUIWire} without automatic interpolation.
94          * 
95          * @author Daniel Kirschten
96          */
97         GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
98         {
99                 logicObs = (i) -> callRedrawListeners();
100                 this.model = model;
101                 this.logicWidth = pin1.logicWidth;
102                 if (pin2.logicWidth != pin1.logicWidth)
103                         throw new IllegalArgumentException("Can't connect pins of different logic width");
104
105                 this.pin1 = pin1;
106                 this.pin2 = pin2;
107
108                 this.path = path == null ? null : Arrays.copyOf(path, path.length);
109                 this.bounds = new Rectangle(0, 0, -1, -1);
110
111                 redrawListeners = new ArrayList<>();
112
113                 pin1.addPinMovedListener(p -> pinMoved());
114                 pin2.addPinMovedListener(p -> pinMoved());
115
116                 recalculateEffectivePath();
117
118                 model.wireCreated(this);
119         }
120
121         /**
122          * Destroys this wire. This method implicitly calls {@link ViewModelModifiable#wireDestroyed(GUIWire) wireDestroyed()} for the model
123          * this component is a part of.
124          * 
125          * @author Daniel Kirschten
126          */
127         public void destroy()
128         {
129                 model.wireDestroyed(this);
130         }
131
132         // pins
133
134         /**
135          * Returns the {@link Pin} on one side of this wire, usually the signal source.
136          * 
137          * @author Daniel Kirschten
138          */
139         public Pin getPin1()
140         {
141                 return pin1;
142         }
143
144         /**
145          * Returns the {@link Pin} on one side of this wire, usually the signal target.
146          * 
147          * @author Daniel Kirschten
148          */
149         public Pin getPin2()
150         {
151                 return pin2;
152         }
153
154         /**
155          * Called when {@link #pin1} or {@link #pin2} were moved.
156          * 
157          * @author Daniel Kirschten
158          */
159         private void pinMoved()
160         {
161                 recalculateEffectivePath();
162                 callRedrawListeners();
163         }
164
165         // "graphical" operations
166
167         /**
168          * Recalculates {@link #effectivePath} "from scratch". Also updates {@link #bounds}.
169          * 
170          * @author Daniel Kirschten
171          */
172         private void recalculateEffectivePath()
173         {
174                 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
175
176                 double boundsX1 = Math.min(pos1.x, pos2.x);
177                 double boundsY1 = Math.min(pos1.y, pos2.y);
178                 double boundsX2 = Math.max(pos1.x, pos2.x);
179                 double boundsY2 = Math.max(pos1.y, pos2.y);
180
181                 if (path == null)
182                         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 };
183                 else
184                 {
185                         effectivePath = new double[path.length * 2 + 4];
186                         effectivePath[0] = pos1.x;
187                         effectivePath[1] = pos1.y;
188                         for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
189                         {
190                                 double pathX = path[srcI].x;
191                                 double pathY = path[srcI].y;
192                                 effectivePath[dstI + 0] = pathX;
193                                 effectivePath[dstI + 1] = pathY;
194                                 if (pathX < boundsX1)
195                                         boundsX1 = pathX;
196                                 if (pathX > boundsX2)
197                                         boundsX2 = pathX;
198                                 if (pathY < boundsY1)
199                                         boundsY1 = pathY;
200                                 if (pathY > boundsY2)
201                                         boundsY2 = pathY;
202                         }
203                         effectivePath[effectivePath.length - 2] = pos2.x;
204                         effectivePath[effectivePath.length - 1] = pos2.y;
205                 }
206
207                 bounds.x = boundsX1;
208                 bounds.y = boundsY1;
209                 bounds.width = boundsX2 - boundsX1;
210                 bounds.height = boundsY2 - boundsY1;
211         }
212
213         /**
214          * Returns the bounds of this wire, excluding line width (and line joins, if the line join is {@link SWT#JOIN_MITER})
215          * 
216          * @author Daniel Kirschten
217          */
218         public Rectangle getBounds()
219         {
220                 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
221         }
222
223         /**
224          * Render this wire to the given gc, in absoulute coordinates.
225          * 
226          * @author Daniel Kirschten
227          */
228         public void render(GeneralGC gc)
229         {
230                 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
231                 if (wireColor != null)
232                         gc.setForeground(ColorManager.current().toColor(wireColor));
233                 gc.drawPolyline(effectivePath);
234         }
235
236         /**
237          * The user-defined path between {@link #pin1} and {@link #pin2}. Note that this is not neccessarily equal to the effective path drawn
238          * in {@link #render(GeneralGC)}.<br>
239          * Special cases: <code>null</code> means "choose an interpolation as fits", and an empty array means "direct connection without any
240          * interpolation".
241          * 
242          * @author Daniel Kirschten
243          */
244         public Point[] getPath()
245         {
246                 return path == null ? null : path.clone();
247         }
248
249         // logic model binding
250
251         /**
252          * Binds this {@link GUIWire} to the given {@link ReadEnd}: The color of this {@link GUIWire} will now depend on the state of the given
253          * {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
254          * The argument can be null, in which case the old binding is stopped.
255          * 
256          * @author Daniel Kirschten
257          */
258         public void setLogicModelBinding(ReadEnd end)
259         {
260                 if (this.end != null)
261                         this.end.deregisterObserver(logicObs);
262                 this.end = end;
263                 if (end != null)
264                         end.registerObserver(logicObs);
265         }
266
267         /**
268          * Returns whether this {@link GUIWire} has a logic model binding or not.
269          * 
270          * @author Daniel Kirschten
271          */
272         public boolean hasLogicModelBinding()
273         {
274                 return end != null;
275         }
276
277         /**
278          * If this {@link GUIWire} has a logic model binding, delegates to {@link Wire#forceValues(BitVector)} for the {@link Wire}
279          * corresponding to this {@link GUIWire}.
280          * 
281          * @author Daniel Kirschten
282          */
283         public void forceWireValues(BitVector values)
284         {
285                 end.getWire().forceValues(values);
286         }
287
288         /**
289          * If this {@link GUIWire} has a logic model binding, delegates to {@link ReadEnd#getValues()} for the {@link ReadEnd} corresponding to
290          * this {@link GUIWire}.
291          * 
292          * @author Daniel Kirschten
293          */
294         public BitVector getWireValues()
295         {
296                 return end.getValues();
297         }
298
299         // listeners
300
301         // @formatter:off
302         public void addRedrawListener   (Runnable listener) {redrawListeners         .add   (listener);}
303
304         public void removeRedrawListener(Runnable listener) {redrawListeners         .remove(listener);}
305
306         private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
307         // @formatter:on
308
309         @Override
310         public String toString()
311         {
312                 return "GUIWire [" + pin1 + "---" + pin2 + ", value=" + (end == null ? "null" : end.getValues()) + "]";
313         }
314 }