1 package era.mi.gui.wires;
\r
3 import java.util.Objects;
\r
5 import org.eclipse.swt.SWT;
\r
6 import org.eclipse.swt.graphics.Color;
\r
8 import era.mi.gui.components.BasicGUIComponent;
\r
9 import era.mi.logic.types.Bit;
\r
10 import era.mi.logic.wires.Wire;
\r
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
\r
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
\r
14 public class GUIWire
\r
16 private final Wire wire;
\r
17 private final double[] path;
\r
19 public GUIWire(Runnable redraw, BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos,
\r
20 BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path)
\r
22 this.wire = component1.getConnectedWireEnd(component1ConnectionIndex).getWire();
\r
23 if (!Objects.equals(wire, component2.getConnectedWireEnd(component2ConnectionIndex).getWire()))
\r
24 throw new IllegalArgumentException("Given connection points are not connected!");
\r
25 this.path = new double[path.length * 2 + 4];
\r
26 Point component1ConnectionPoint = component1.getWireEndConnectionPoint(component1ConnectionIndex);
\r
27 this.path[0] = component1Pos.x + component1ConnectionPoint.x;
\r
28 this.path[1] = component1Pos.y + component1ConnectionPoint.y;
\r
29 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
\r
31 this.path[dstI + 0] = path[srcI].x;
\r
32 this.path[dstI + 1] = path[srcI].y;
\r
34 Point component2ConnectionPoint = component2.getWireEndConnectionPoint(component2ConnectionIndex);
\r
35 this.path[this.path.length - 2] = component2Pos.x + component2ConnectionPoint.x;
\r
36 this.path[this.path.length - 1] = component2Pos.y + component2ConnectionPoint.y;
\r
38 wire.addObserver((initiator, oldValues) -> redraw.run());
\r
41 public void render(GeneralGC gc)
\r
43 Color oldFG = gc.getForeground();
\r
44 if (wire.length == 1)
\r
45 gc.setForeground(gc.getDevice().getSystemColor(getSWTColorConstantForBit(wire.getValue())));
\r
46 gc.drawPolyline(path);
\r
47 gc.setForeground(oldFG);
\r
50 public static int getSWTColorConstantForBit(Bit bit)
\r
55 return SWT.COLOR_GREEN;
\r
57 return SWT.COLOR_BLUE;
\r
59 return SWT.COLOR_BLACK;
\r
62 return SWT.COLOR_RED;
\r
64 throw new IllegalArgumentException("Unknown enum constant: " + bit);
\r