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.Bit;
10 import era.mi.logic.wires.WireArray;
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
16 private final WireArray wa;
17 private final double[] path;
19 public GUIWire(Runnable redraw, BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos, BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path)
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)
30 this.path[dstI + 0] = path[srcI].x;
31 this.path[dstI + 1] = path[srcI].y;
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;
37 wa.addObserver((initiator, oldValues) -> redraw.run());
40 public void render(GeneralGC gc)
42 Color oldFG = gc.getForeground();
44 gc.setForeground(gc.getDevice().getSystemColor(getSWTColorConstantForBit(wa.getValue())));
45 gc.drawPolyline(path);
46 gc.setForeground(oldFG);
49 public static int getSWTColorConstantForBit(Bit bit)
54 return SWT.COLOR_GREEN;
56 return SWT.COLOR_BLUE;
58 return SWT.COLOR_BLACK;
63 throw new IllegalArgumentException("Unknown enum constant: " + bit);