fed303ba97891be48bedf0d59f6a775c3a4e9c15
[Mograsim.git] / LogicUI / src / era / mi / gui / components / GUIMerger.java
1 package era.mi.gui.components;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7
8 import era.mi.logic.components.Merger;
9 import era.mi.logic.wires.Wire.WireEnd;
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13
14 public class GUIMerger extends Merger implements BasicGUIComponent
15 {
16         private final int inputCount;
17         private final double height;
18         private final List<WireEnd> connectedWireEnds;
19         private final List<Point> WireEndConnectionPoints;
20
21         public GUIMerger(WireEnd union, WireEnd... inputs)
22         {
23                 super(union, inputs);
24
25                 List<WireEnd> connectedWireEndsModifiable = new ArrayList<>();
26                 List<Point> WireEndConnectionPointsModifiable = new ArrayList<>();
27
28                 this.inputCount = inputs.length;
29                 this.height = (inputCount - 1) * 10;
30
31                 {
32                         connectedWireEndsModifiable.addAll(Arrays.asList(inputs));
33                         double inputHeight = 0;
34                         for (int i = 0; i < inputCount; i++, inputHeight += 10)
35                                 WireEndConnectionPointsModifiable.add(new Point(0, inputHeight));
36                 }
37
38                 connectedWireEndsModifiable.add(union);
39                 WireEndConnectionPointsModifiable.add(new Point(20, height / 2));
40
41                 this.connectedWireEnds = Collections.unmodifiableList(connectedWireEndsModifiable);
42                 this.WireEndConnectionPoints = Collections.unmodifiableList(WireEndConnectionPointsModifiable);
43         }
44
45         @Override
46         public Rectangle getBounds()
47         {
48                 return new Rectangle(0, 0, 20, height);
49         }
50
51         @Override
52         public void render(GeneralGC gc)
53         {
54                 double inputHeight = 0;
55                 for (int i = 0; i < inputCount; i++, inputHeight += 10)
56                         gc.drawLine(0, inputHeight, 10, inputHeight);
57                 gc.drawLine(10, 0, 10, height);
58                 gc.drawLine(10, height / 2, 20, height / 2);
59         }
60
61         @Override
62         public int getConnectedWireEndsCount()
63         {
64                 return connectedWireEnds.size();
65         }
66
67         @Override
68         public WireEnd getConnectedWireEnd(int connectionIndex)
69         {
70                 return connectedWireEnds.get(connectionIndex);
71         }
72
73         @Override
74         public Point getWireEndConnectionPoint(int connectionI)
75         {
76                 return WireEndConnectionPoints.get(connectionI);
77         }
78 }