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