1 package era.mi.gui.model.wires;
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.graphics.Color;
6 import era.mi.gui.model.ViewModel;
7 import era.mi.logic.types.Bit;
8 import era.mi.logic.wires.Wire;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
14 private final ViewModel model;
17 private double[] path;
21 public GUIWire(ViewModel model, Pin pin1, Pin pin2, Point... path)
24 this.path = new double[path.length * 2 + 4];
25 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
27 this.path[dstI + 0] = path[srcI].x;
28 this.path[dstI + 1] = path[srcI].y;
34 pin1.addPinMovedListener(p -> pin1Moved());
35 pin2.addPinMovedListener(p -> pin2Moved());
39 model.wireCreated(this);
42 private void pin1Moved()
44 Point pos = pin1.getPos();
49 private void pin2Moved()
51 Point pos = pin2.getPos();
52 this.path[this.path.length - 2] = pos.x;
53 this.path[this.path.length - 1] = pos.y;
58 model.wireDestroyed(this);
61 public void render(GeneralGC gc)
63 Color oldFG = gc.getForeground();
64 gc.setForeground(gc.getDevice().getSystemColor(getSWTColorConstantForWire(wire)));
65 gc.drawPolyline(path);
66 gc.setForeground(oldFG);
69 public void setLogicModelWire(Wire wire)
74 public static int getSWTColorConstantForWire(Wire wire)
76 if (wire != null && wire.length == 1)
77 return getSWTColorConstantForBit(wire.getValue());
79 return SWT.COLOR_BLACK;
82 public static int getSWTColorConstantForBit(Bit bit)
87 return SWT.COLOR_GREEN;
89 return SWT.COLOR_BLUE;
91 return SWT.COLOR_BLACK;
96 throw new IllegalArgumentException("Unknown enum constant: " + bit);