1dc11d19f023764824aea54065b2fe91ff28ce83
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUISplitter.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.SplitterAdapter;
12 import net.mograsim.preferences.ColorDefinition;
13 import net.mograsim.preferences.ColorManager;
14 import net.mograsim.preferences.Preferences;
15
16 public class GUISplitter 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 inputEnd;
24         private ReadEnd[] outputEnds;
25
26         public GUISplitter(ViewModelModifiable model, int logicWidth, String name)
27         {
28                 super(model, name);
29                 this.logicWidth = logicWidth;
30                 setSize(width, logicWidth * heightPerPin);
31                 addPin(new Pin(this, "I", logicWidth, 0, logicWidth * heightPerPin / 2));
32                 double outputHeight = 0;
33                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
34                         addPin(new Pin(this, "O" + i, 1, width, outputHeight));
35         }
36
37         @Override
38         public void render(GeneralGC gc, Rectangle visibleRegion)
39         {
40                 double posX = getPosX();
41                 double posY = getPosY();
42
43                 ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnd);
44                 if (c != null)
45                         gc.setForeground(ColorManager.current().toColor(c));
46                 gc.drawLine(posX, posY + heightPerPin * logicWidth / 2, posX + width / 2, posY + heightPerPin * logicWidth / 2);
47                 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
48                 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
49                 double outputHeight = posY;
50                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
51                 {
52                         c = BitVectorFormatter.formatAsColor(outputEnds[i]);
53                         if (c != null)
54                                 gc.setForeground(ColorManager.current().toColor(c));
55                         gc.drawLine(posX + width / 2, outputHeight, posX + width, outputHeight);
56                 }
57         }
58
59         public void setLogicModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
60         {
61                 this.inputEnd = inputEnd;
62                 this.outputEnds = outputEnds;
63         }
64
65         static
66         {
67                 ViewLogicModelAdapter.addComponentAdapter(new SplitterAdapter());
68         }
69 }