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