Implemented GUIMerger + GUISplitter
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUIMerger.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import net.haspamelodica.swt.helper.gcs.GeneralGC;
4 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
5 import net.mograsim.logic.core.types.BitVectorFormatter;
6 import net.mograsim.logic.core.wires.Wire.ReadEnd;
7 import net.mograsim.logic.model.model.ViewModelModifiable;
8 import net.mograsim.logic.model.model.components.GUIComponent;
9 import net.mograsim.logic.model.model.wires.Pin;
10 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
11 import net.mograsim.logic.model.modeladapter.componentadapters.MergerAdapter;
12 import net.mograsim.preferences.ColorDefinition;
13 import net.mograsim.preferences.ColorManager;
14 import net.mograsim.preferences.Preferences;
15
16 public class GUIMerger extends GUIComponent
17 {
18         private static final double width = 20;
19         private static final double heightPerPin = 10;
20
21         public final int logicWidth;
22
23         private ReadEnd[] inputEnds;
24         private ReadEnd outputEnd;
25
26         public GUIMerger(ViewModelModifiable model, int logicWidth, String name)
27         {
28                 super(model, name);
29                 this.logicWidth = logicWidth;
30                 setSize(width, logicWidth * heightPerPin);
31                 double inputHeight = 0;
32                 for (int i = 0; i < logicWidth; i++, inputHeight += 10)
33                         addPin(new Pin(this, "I" + i, 1, 0, inputHeight));
34                 addPin(new Pin(this, "O", logicWidth, width, logicWidth * heightPerPin / 2));
35         }
36
37         @Override
38         public void render(GeneralGC gc, Rectangle visibleRegion)
39         {
40                 double posX = getPosX();
41                 double posY = getPosY();
42
43                 double inputHeight = posY;
44                 for (int i = 0; i < logicWidth; i++, inputHeight += 10)
45                 {
46                         ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnds[i]);
47                         if (c != null)
48                                 gc.setForeground(ColorManager.current().toColor(c));
49                         gc.drawLine(posX, inputHeight, posX + width / 2, inputHeight);
50                 }
51                 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
52                 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
53                 ColorDefinition c = BitVectorFormatter.formatAsColor(outputEnd);
54                 if (c != null)
55                         gc.setForeground(ColorManager.current().toColor(c));
56                 gc.drawLine(posX + width / 2, posY + heightPerPin * logicWidth / 2, posX + width, posY + heightPerPin * logicWidth / 2);
57         }
58
59         public void setLogicModelBinding(ReadEnd[] inputEnds, ReadEnd outputEnd)
60         {
61                 this.inputEnds = inputEnds;
62                 this.outputEnd = outputEnd;
63         }
64
65         static
66         {
67                 ViewLogicModelAdapter.addComponentAdapter(new MergerAdapter());
68         }
69 }