LogicUICanvas now uses the new listener system; changes in clicked()
[Mograsim.git] / LogicUI / src / era / mi / gui / model / wires / GUIWire.java
1 package era.mi.gui.model.wires;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.graphics.Color;
5
6 import era.mi.gui.model.ViewModel;
7 import era.mi.logic.types.Bit;
8 import era.mi.logic.wires.Wire;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11
12 public class GUIWire
13 {
14         private Pin pin1;
15         private Pin pin2;
16         private double[] path;
17
18         private Wire wire;
19
20         public GUIWire(ViewModel model, Pin pin1, Pin pin2, Point... path)
21         {
22                 this.path = new double[path.length * 2 + 4];
23                 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
24                 {
25                         this.path[dstI + 0] = path[srcI].x;
26                         this.path[dstI + 1] = path[srcI].y;
27                 }
28                 // TODO support moving pins
29                 Point pos;
30                 pos = pin1.getPos();
31                 this.path[0] = pos.x;
32                 this.path[1] = pos.y;
33                 pos = pin2.getPos();
34                 this.path[this.path.length - 2] = pos.x;
35                 this.path[this.path.length - 1] = pos.y;
36
37                 model.wireCreated(this);
38         }
39
40         public void render(GeneralGC gc)
41         {
42                 Color oldFG = gc.getForeground();
43                 gc.setForeground(gc.getDevice().getSystemColor(getSWTColorConstantForWire(wire)));
44                 gc.drawPolyline(path);
45                 gc.setForeground(oldFG);
46         }
47
48         public void setLogicModelWire(Wire wire)
49         {
50                 this.wire = wire;
51         }
52
53         public static int getSWTColorConstantForWire(Wire wire)
54         {
55                 if (wire != null && wire.length == 1)
56                         return getSWTColorConstantForBit(wire.getValue());
57                 else
58                         return SWT.COLOR_BLACK;
59         }
60
61         public static int getSWTColorConstantForBit(Bit bit)
62         {
63                 switch (bit)
64                 {
65                 case ONE:
66                         return SWT.COLOR_GREEN;
67                 case ZERO:
68                         return SWT.COLOR_BLUE;
69                 case Z:
70                         return SWT.COLOR_BLACK;
71                 case U:
72                 case X:
73                         return SWT.COLOR_RED;
74                 default:
75                         throw new IllegalArgumentException("Unknown enum constant: " + bit);
76                 }
77         }
78 }