c16aa31630462ee45f0874c91cd136a8fdc19fbd
[Mograsim.git] / LogicUI / src / era / mi / gui / components / GUIMux.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.Mux;
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 GUIMux extends Mux implements BasicGUIComponent
15 {
16         private final double height;
17         private final List<WireEnd> connectedWireEnds;
18         private final List<Point> WireEndConnectionPoints;
19
20         public GUIMux(int processTime, WireEnd out, WireEnd select, WireEnd... inputs)
21         {
22                 super(processTime, out, select, inputs);
23
24                 double height = inputs.length * 5;
25                 if (height < 10)
26                         height = 10;
27                 this.height = height;
28
29                 List<WireEnd> connectedWireEndsModifiable = new ArrayList<>();
30                 List<Point> WireEndConnectionPointsModifiable = new ArrayList<>();
31
32                 connectedWireEndsModifiable.add(out);
33                 WireEndConnectionPointsModifiable.add(new Point(20, 10 + height / 2));
34
35                 connectedWireEndsModifiable.add(select);
36                 WireEndConnectionPointsModifiable.add(new Point(10, 5));
37
38                 {
39                         connectedWireEndsModifiable.addAll(Arrays.asList(inputs));
40                         double inputHeightIncrement = (height + 20) / inputs.length;
41                         double inputHeight = inputHeightIncrement / 2;
42                         for (int i = 0; i < inputs.length; i++, inputHeight += inputHeightIncrement)
43                                 WireEndConnectionPointsModifiable.add(new Point(0, inputHeight));
44                 }
45
46                 this.connectedWireEnds = Collections.unmodifiableList(connectedWireEndsModifiable);
47                 this.WireEndConnectionPoints = Collections.unmodifiableList(WireEndConnectionPointsModifiable);
48         }
49
50         @Override
51         public Rectangle getBounds()
52         {
53                 return new Rectangle(0, 0, 20, height + 20);
54         }
55
56         @Override
57         public void render(GeneralGC gc)
58         {
59                 gc.drawPolygon(new double[] { 0, 0, 20, 10, 20, height + 10, 0, height + 20 });
60         }
61
62         @Override
63         public int getConnectedWireEndsCount()
64         {
65                 return connectedWireEnds.size();
66         }
67
68         @Override
69         public WireEnd getConnectedWireEnd(int connectionIndex)
70         {
71                 return connectedWireEnds.get(connectionIndex);
72         }
73
74         @Override
75         public Point getWireEndConnectionPoint(int connectionI)
76         {
77                 return WireEndConnectionPoints.get(connectionI);
78         }
79 }