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