X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=LogicUI%2Fsrc%2Fera%2Fmi%2Fgui%2Fwires%2FGUIWire.java;fp=LogicUI%2Fsrc%2Fera%2Fmi%2Fgui%2Fwires%2FGUIWire.java;h=3b04fae8501c6df6a05c5236dd6b6348c3042e09;hb=d3a382377768fdb1434d5baebf73c9413e61e46f;hp=0000000000000000000000000000000000000000;hpb=0cfea6ef89dea8a797708ff685aa2ef9aefd85b9;p=Mograsim.git diff --git a/LogicUI/src/era/mi/gui/wires/GUIWire.java b/LogicUI/src/era/mi/gui/wires/GUIWire.java new file mode 100644 index 00000000..3b04fae8 --- /dev/null +++ b/LogicUI/src/era/mi/gui/wires/GUIWire.java @@ -0,0 +1,66 @@ +package era.mi.gui.wires; + +import java.util.Objects; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Color; + +import era.mi.gui.components.BasicGUIComponent; +import era.mi.logic.wires.WireArray; +import net.haspamelodica.swt.helper.gcs.GeneralGC; +import net.haspamelodica.swt.helper.swtobjectwrappers.Point; + +public class GUIWire +{ + private final WireArray wa; + private final double[] path; + + public GUIWire(Runnable redraw, BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos, BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path) + { + this.wa = component1.getConnectedWireArray(component1ConnectionIndex); + if(!Objects.equals(wa, component2.getConnectedWireArray(component2ConnectionIndex))) + throw new IllegalArgumentException("Given connection points are not connected!"); + this.path = new double[path.length * 2 + 4]; + Point component1ConnectionPoint = component1.getWireArrayConnectionPoint(component1ConnectionIndex); + this.path[0] = component1Pos.x + component1ConnectionPoint.x; + this.path[1] = component1Pos.y + component1ConnectionPoint.y; + for(int srcI = 0, dstI = 2; srcI < path.length; srcI ++, dstI += 2) + { + this.path[dstI + 0] = path[srcI].x; + this.path[dstI + 1] = path[srcI].y; + } + Point component2ConnectionPoint = component2.getWireArrayConnectionPoint(component2ConnectionIndex); + this.path[this.path.length - 2] = component2Pos.x + component2ConnectionPoint.x; + this.path[this.path.length - 1] = component2Pos.y + component2ConnectionPoint.y; + + wa.addObserver((initiator, oldValues) -> redraw.run()); + } + + public void render(GeneralGC gc) + { + Color oldFG = gc.getForeground(); + if(wa.length == 1) + { + int fgColorConstant; + switch(wa.getValue()) + { + case ONE: + fgColorConstant = SWT.COLOR_GREEN; + break; + case ZERO: + fgColorConstant = SWT.COLOR_BLUE; + break; + case U: + case X: + case Z: + fgColorConstant = SWT.COLOR_RED; + break; + default: + throw new IllegalArgumentException("Unknown enum constant: " + wa.getValue()); + } + gc.setForeground(gc.getDevice().getSystemColor(fgColorConstant)); + } + gc.drawPolyline(path); + gc.setForeground(oldFG); + } +} \ No newline at end of file