Restructured packages
[Mograsim.git] / LogicUI / src / era / mi / gui / wires / GUIWire.java
1 package era.mi.gui.wires;
2
3 import java.util.Objects;
4
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.graphics.Color;
7
8 import era.mi.gui.components.BasicGUIComponent;
9 import era.mi.logic.wires.WireArray;
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12
13 public class GUIWire
14 {
15         private final WireArray wa;
16         private final double[]  path;
17
18         public GUIWire(Runnable redraw, BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos, BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path)
19         {
20                 this.wa = component1.getConnectedWireArray(component1ConnectionIndex);
21                 if(!Objects.equals(wa, component2.getConnectedWireArray(component2ConnectionIndex)))
22                         throw new IllegalArgumentException("Given connection points are not connected!");
23                 this.path = new double[path.length * 2 + 4];
24                 Point component1ConnectionPoint = component1.getWireArrayConnectionPoint(component1ConnectionIndex);
25                 this.path[0] = component1Pos.x + component1ConnectionPoint.x;
26                 this.path[1] = component1Pos.y + component1ConnectionPoint.y;
27                 for(int srcI = 0, dstI = 2; srcI < path.length; srcI ++, dstI += 2)
28                 {
29                         this.path[dstI + 0] = path[srcI].x;
30                         this.path[dstI + 1] = path[srcI].y;
31                 }
32                 Point component2ConnectionPoint = component2.getWireArrayConnectionPoint(component2ConnectionIndex);
33                 this.path[this.path.length - 2] = component2Pos.x + component2ConnectionPoint.x;
34                 this.path[this.path.length - 1] = component2Pos.y + component2ConnectionPoint.y;
35
36                 wa.addObserver((initiator, oldValues) -> redraw.run());
37         }
38
39         public void render(GeneralGC gc)
40         {
41                 Color oldFG = gc.getForeground();
42                 if(wa.length == 1)
43                 {
44                         int fgColorConstant;
45                         switch(wa.getValue())
46                         {
47                                 case ONE:
48                                         fgColorConstant = SWT.COLOR_GREEN;
49                                         break;
50                                 case ZERO:
51                                         fgColorConstant = SWT.COLOR_BLUE;
52                                         break;
53                                 case U:
54                                 case X:
55                                 case Z:
56                                         fgColorConstant = SWT.COLOR_RED;
57                                         break;
58                                 default:
59                                         throw new IllegalArgumentException("Unknown enum constant: " + wa.getValue());
60                         }
61                         gc.setForeground(gc.getDevice().getSystemColor(fgColorConstant));
62                 }
63                 gc.drawPolyline(path);
64                 gc.setForeground(oldFG);
65         }
66 }