5d9a174c976bb0ca5cf3f2b134bae55667c5b2a6
[Mograsim.git] / LogicUI / src / era / mi / gui / wires / GUIWire.java
1 package era.mi.gui.wires;\r
2 \r
3 import java.util.Objects;\r
4 \r
5 import org.eclipse.swt.SWT;\r
6 import org.eclipse.swt.graphics.Color;\r
7 \r
8 import era.mi.gui.components.BasicGUIComponent;\r
9 import era.mi.logic.Bit;\r
10 import era.mi.logic.wires.WireArray;\r
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
13 \r
14 public class GUIWire\r
15 {\r
16         private final WireArray wa;\r
17         private final double[]  path;\r
18 \r
19         public GUIWire(Runnable redraw, BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos, BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path)\r
20         {\r
21                 this.wa = component1.getConnectedWireArray(component1ConnectionIndex);\r
22                 if(!Objects.equals(wa, component2.getConnectedWireArray(component2ConnectionIndex)))\r
23                         throw new IllegalArgumentException("Given connection points are not connected!");\r
24                 this.path = new double[path.length * 2 + 4];\r
25                 Point component1ConnectionPoint = component1.getWireArrayConnectionPoint(component1ConnectionIndex);\r
26                 this.path[0] = component1Pos.x + component1ConnectionPoint.x;\r
27                 this.path[1] = component1Pos.y + component1ConnectionPoint.y;\r
28                 for(int srcI = 0, dstI = 2; srcI < path.length; srcI ++, dstI += 2)\r
29                 {\r
30                         this.path[dstI + 0] = path[srcI].x;\r
31                         this.path[dstI + 1] = path[srcI].y;\r
32                 }\r
33                 Point component2ConnectionPoint = component2.getWireArrayConnectionPoint(component2ConnectionIndex);\r
34                 this.path[this.path.length - 2] = component2Pos.x + component2ConnectionPoint.x;\r
35                 this.path[this.path.length - 1] = component2Pos.y + component2ConnectionPoint.y;\r
36 \r
37                 wa.addObserver((initiator, oldValues) -> redraw.run());\r
38         }\r
39 \r
40         public void render(GeneralGC gc)\r
41         {\r
42                 Color oldFG = gc.getForeground();\r
43                 if(wa.length == 1)\r
44                         gc.setForeground(gc.getDevice().getSystemColor(getSWTColorConstantForBit(wa.getValue())));\r
45                 gc.drawPolyline(path);\r
46                 gc.setForeground(oldFG);\r
47         }\r
48 \r
49         public static int getSWTColorConstantForBit(Bit bit)\r
50         {\r
51                 switch(bit)\r
52                 {\r
53                         case ONE:\r
54                                 return SWT.COLOR_GREEN;\r
55                         case ZERO:\r
56                                 return SWT.COLOR_BLUE;\r
57                         case Z:\r
58                                 return SWT.COLOR_BLACK;\r
59                         case U:\r
60                         case X:\r
61                                 return SWT.COLOR_RED;\r
62                         default:\r
63                                 throw new IllegalArgumentException("Unknown enum constant: " + bit);\r
64                 }\r
65         }\r
66 }