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