Updated LogicUI to Java 10
[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.types.Bit;
10 import era.mi.logic.wires.Wire;
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 Wire wire;
17         private final double[] path;
18
19         public GUIWire(Runnable redraw, BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos,
20                         BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path)
21         {
22                 this.wire = component1.getConnectedWireEnd(component1ConnectionIndex).getWire();
23                 if (!Objects.equals(wire, component2.getConnectedWireEnd(component2ConnectionIndex).getWire()))
24                         throw new IllegalArgumentException("Given connection points are not connected!");
25                 this.path = new double[path.length * 2 + 4];
26                 Point component1ConnectionPoint = component1.getWireEndConnectionPoint(component1ConnectionIndex);
27                 this.path[0] = component1Pos.x + component1ConnectionPoint.x;
28                 this.path[1] = component1Pos.y + component1ConnectionPoint.y;
29                 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
30                 {
31                         this.path[dstI + 0] = path[srcI].x;
32                         this.path[dstI + 1] = path[srcI].y;
33                 }
34                 Point component2ConnectionPoint = component2.getWireEndConnectionPoint(component2ConnectionIndex);
35                 this.path[this.path.length - 2] = component2Pos.x + component2ConnectionPoint.x;
36                 this.path[this.path.length - 1] = component2Pos.y + component2ConnectionPoint.y;
37
38                 wire.createReadOnlyEnd().addObserver((initiator, oldValues) -> redraw.run());
39         }
40
41         public void render(GeneralGC gc)
42         {
43                 Color oldFG = gc.getForeground();
44                 if (wire.length == 1)
45                         gc.setForeground(gc.getDevice().getSystemColor(getSWTColorConstantForBit(wire.getValue())));
46                 gc.drawPolyline(path);
47                 gc.setForeground(oldFG);
48         }
49
50         public static int getSWTColorConstantForBit(Bit bit)
51         {
52                 switch (bit)
53                 {
54                 case ONE:
55                         return SWT.COLOR_GREEN;
56                 case ZERO:
57                         return SWT.COLOR_BLUE;
58                 case Z:
59                         return SWT.COLOR_BLACK;
60                 case U:
61                 case X:
62                         return SWT.COLOR_RED;
63                 default:
64                         throw new IllegalArgumentException("Unknown enum constant: " + bit);
65                 }
66         }
67 }