47f851710a672a84c87ee50ff639fa4c938bb54f
[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 net.haspamelodica.swt.helper.gcs.GeneralGC;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
10 import net.mograsim.logic.core.LogicObservable;
11 import net.mograsim.logic.core.LogicObserver;
12 import net.mograsim.logic.core.types.BitVectorFormatter;
13 import net.mograsim.logic.core.wires.Wire.ReadEnd;
14 import net.mograsim.logic.ui.ColorHelper;
15 import net.mograsim.logic.ui.model.ViewModelModifiable;
16
17 public class GUIWire
18 {
19         private final ViewModelModifiable model;
20         public final int logicWidth;
21         private Pin pin1;
22         private Pin pin2;
23         private Point[] path;
24         private final Rectangle bounds;
25         private double[] effectivePath;
26
27         private final List<Runnable> redrawListeners;
28
29         private final LogicObserver logicObs;
30         private ReadEnd end;
31
32         public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2)
33         {
34                 this(model, pin1, pin2, (Point[]) null);
35         }
36
37         public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2)
38         {
39                 this(model, pin1, pin2, (Point[]) null);
40         }
41
42         public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2)
43         {
44                 this(model, pin1, pin2, (Point[]) null);
45         }
46
47         public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2)
48         {
49                 this(model, pin1, pin2, (Point[]) null);
50         }
51
52         public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, WireCrossPoint pin2, Point... path)
53         {
54                 this(model, pin1.getPin(), pin2.getPin(), path);
55         }
56
57         public GUIWire(ViewModelModifiable model, WireCrossPoint pin1, Pin pin2, Point... path)
58         {
59                 this(model, pin1.getPin(), pin2, path);
60         }
61
62         public GUIWire(ViewModelModifiable model, Pin pin1, WireCrossPoint pin2, Point... path)
63         {
64                 this(model, pin1, pin2.getPin(), path);
65         }
66
67         public GUIWire(ViewModelModifiable model, Pin pin1, Pin pin2, Point... path)
68         {
69                 logicObs = (i) -> callRedrawListeners();
70                 this.model = model;
71                 this.logicWidth = pin1.logicWidth;
72                 if (pin2.logicWidth != pin1.logicWidth)
73                         throw new IllegalArgumentException("Can't connect pins of different logic width");
74
75                 this.pin1 = pin1;
76                 this.pin2 = pin2;
77
78                 this.path = path == null ? null : Arrays.copyOf(path, path.length);
79                 this.bounds = new Rectangle(0, 0, -1, -1);
80
81                 redrawListeners = new ArrayList<>();
82
83                 pin1.addPinMovedListener(p -> pin1Moved());
84                 pin2.addPinMovedListener(p -> pin2Moved());
85
86                 recalculateEffectivePath();
87
88                 model.wireCreated(this);
89         }
90
91         private void recalculateEffectivePath()
92         {
93                 Point pos1 = pin1.getPos(), pos2 = pin2.getPos();
94
95                 double boundsX1 = Math.min(pos1.x, pos2.x);
96                 double boundsY1 = Math.min(pos1.y, pos2.y);
97                 double boundsX2 = Math.max(pos1.x, pos2.x);
98                 double boundsY2 = Math.max(pos1.y, pos2.y);
99
100                 if (path == null)
101                         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 };
102                 else
103                 {
104                         effectivePath = new double[path.length * 2 + 4];
105                         effectivePath[0] = pos1.x;
106                         effectivePath[1] = pos1.y;
107                         for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
108                         {
109                                 double pathX = path[srcI].x;
110                                 double pathY = path[srcI].y;
111                                 effectivePath[dstI + 0] = pathX;
112                                 effectivePath[dstI + 1] = pathY;
113                                 if (pathX < boundsX1)
114                                         boundsX1 = pathX;
115                                 if (pathX > boundsX2)
116                                         boundsX2 = pathX;
117                                 if (pathY < boundsY1)
118                                         boundsY1 = pathY;
119                                 if (pathY > boundsY2)
120                                         boundsY2 = pathY;
121                         }
122                         effectivePath[effectivePath.length - 2] = pos2.x;
123                         effectivePath[effectivePath.length - 1] = pos2.y;
124                 }
125
126                 bounds.x = boundsX1;
127                 bounds.y = boundsY1;
128                 bounds.width = boundsX2 - boundsX1;
129                 bounds.height = boundsY2 - boundsY1;
130         }
131
132         private void pin1Moved()
133         {
134                 recalculateEffectivePath();
135                 callRedrawListeners();
136         }
137
138         private void pin2Moved()
139         {
140                 recalculateEffectivePath();
141                 callRedrawListeners();
142         }
143
144         public void destroy()
145         {
146                 model.wireDestroyed(this);
147         }
148
149         public Rectangle getBounds()
150         {
151                 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
152         }
153
154         public void render(GeneralGC gc)
155         {
156                 ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath));
157         }
158
159         public void setLogicModelBinding(ReadEnd end)
160         {
161                 deregisterLogicObs(this.end);
162                 this.end = end;
163                 registerLogicObs(end);
164         }
165
166         private void registerLogicObs(LogicObservable observable)
167         {
168                 if (observable != null)
169                         observable.registerObserver(logicObs);
170         }
171
172         private void deregisterLogicObs(LogicObservable observable)
173         {
174                 if (observable != null)
175                         observable.deregisterObserver(logicObs);
176         }
177
178         public Pin getPin1()
179         {
180                 return pin1;
181         }
182
183         public Pin getPin2()
184         {
185                 return pin2;
186         }
187
188         // @formatter:off
189         public void addRedrawListener   (Runnable listener) {redrawListeners         .add   (listener);}
190
191         public void removeRedrawListener(Runnable listener) {redrawListeners         .remove(listener);}
192
193         private void callRedrawListeners() {redrawListeners.forEach(l -> l.run());}
194         // @formatter:on
195
196 }