1 package era.mi.gui.wires;
3 import java.util.Objects;
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.graphics.Color;
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;
16 private final Wire wire;
17 private final double[] path;
19 public GUIWire(Runnable redraw, BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos,
20 BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path)
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)
31 this.path[dstI + 0] = path[srcI].x;
32 this.path[dstI + 1] = path[srcI].y;
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;
38 wire.addObserver((initiator, oldValues) -> redraw.run());
41 public void render(GeneralGC gc)
43 Color oldFG = gc.getForeground();
45 gc.setForeground(gc.getDevice().getSystemColor(getSWTColorConstantForBit(wire.getValue())));
46 gc.drawPolyline(path);
47 gc.setForeground(oldFG);
50 public static int getSWTColorConstantForBit(Bit bit)
55 return SWT.COLOR_GREEN;
57 return SWT.COLOR_BLUE;
59 return SWT.COLOR_BLACK;
64 throw new IllegalArgumentException("Unknown enum constant: " + bit);