Implemented the RS-Latch-Example
[Mograsim.git] / LogicUI / src / era / mi / wires / gui / GUIWire.java
1 package era.mi.wires.gui;
2
3 import java.util.Objects;
4
5 import era.mi.components.gui.BasicGUIComponent;
6 import era.mi.logic.wires.WireArray;
7 import net.haspamelodica.swt.helper.gcs.GeneralGC;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
9
10 public class GUIWire
11 {
12         private final WireArray wa;
13         private final double[]  path;
14
15         public GUIWire(BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos, BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path)
16         {
17                 this.wa = component1.getConnectedWireArray(component1ConnectionIndex);
18                 if(!Objects.equals(wa, component2.getConnectedWireArray(component2ConnectionIndex)))
19                         throw new IllegalArgumentException("Given connection points are not connected!");
20                 this.path = new double[path.length * 2 + 4];
21                 Point component1ConnectionPoint = component1.getWireArrayConnectionPoint(component1ConnectionIndex);
22                 this.path[0] = component1Pos.x + component1ConnectionPoint.x;
23                 this.path[1] = component1Pos.y + component1ConnectionPoint.y;
24                 for(int srcI = 0, dstI = 2; srcI < path.length; srcI ++, dstI += 2)
25                 {
26                         this.path[dstI + 0] = path[srcI].x;
27                         this.path[dstI + 1] = path[srcI].y;
28                 }
29                 Point component2ConnectionPoint = component2.getWireArrayConnectionPoint(component2ConnectionIndex);
30                 this.path[this.path.length - 2] = component2Pos.x + component2ConnectionPoint.x;
31                 this.path[this.path.length - 1] = component2Pos.y + component2ConnectionPoint.y;
32         }
33
34         public void render(GeneralGC gc)
35         {
36                 gc.drawPolyline(path);
37         }
38 }