ca3c9e4df6ff0a66e7ce7f15a0d2ebc5e85bdf5b
[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 com.google.gson.JsonElement;
4 import com.google.gson.JsonPrimitive;
5
6 import net.haspamelodica.swt.helper.gcs.GeneralGC;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
8 import net.mograsim.logic.core.types.BitVectorFormatter;
9 import net.mograsim.logic.core.wires.Wire.ReadEnd;
10 import net.mograsim.logic.model.model.ViewModelModifiable;
11 import net.mograsim.logic.model.model.components.GUIComponent;
12 import net.mograsim.logic.model.model.wires.Pin;
13 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
14 import net.mograsim.logic.model.modeladapter.componentadapters.SplitterAdapter;
15 import net.mograsim.logic.model.serializing.IdentifierGetter;
16 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
17 import net.mograsim.preferences.ColorDefinition;
18 import net.mograsim.preferences.ColorManager;
19 import net.mograsim.preferences.Preferences;
20
21 public class GUISplitter extends GUIComponent
22 {
23         private static final double width = 10;
24         private static final double heightPerPin = 10;
25
26         public final int logicWidth;
27
28         private ReadEnd inputEnd;
29         private final ReadEnd[] outputEnds;
30
31         public GUISplitter(ViewModelModifiable model, int logicWidth, String name)
32         {
33                 super(model, name);
34                 this.logicWidth = logicWidth;
35                 setSize(width, logicWidth * heightPerPin);
36                 addPin(new Pin(this, "I", logicWidth, 0, logicWidth * heightPerPin / 2));
37                 double outputHeight = 0;
38                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
39                         addPin(new Pin(this, "O" + i, 1, width, outputHeight));
40                 outputEnds = new ReadEnd[logicWidth];
41         }
42
43         @Override
44         public void render(GeneralGC gc, Rectangle visibleRegion)
45         {
46                 double posX = getPosX();
47                 double posY = getPosY();
48
49                 ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnd);
50                 if (c != null)
51                         gc.setForeground(ColorManager.current().toColor(c));
52                 gc.drawLine(posX, posY + heightPerPin * logicWidth / 2, posX + width / 2, posY + heightPerPin * logicWidth / 2);
53                 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
54                 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
55                 double outputHeight = posY;
56                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
57                 {
58                         c = BitVectorFormatter.formatAsColor(outputEnds[i]);
59                         if (c != null)
60                                 gc.setForeground(ColorManager.current().toColor(c));
61                         gc.drawLine(posX + width / 2, outputHeight, posX + width, outputHeight);
62                 }
63         }
64
65         @Override
66         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
67         {
68                 return new JsonPrimitive(logicWidth);
69         }
70
71         public void setLogicModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
72         {
73                 this.inputEnd = inputEnd;
74                 System.arraycopy(outputEnds, 0, this.outputEnds, 0, logicWidth);
75         }
76
77         static
78         {
79                 ViewLogicModelAdapter.addComponentAdapter(new SplitterAdapter());
80                 IndirectGUIComponentCreator.setComponentSupplier(GUISplitter.class.getCanonicalName(),
81                                 (m, p, n) -> new GUISplitter(m, p.getAsInt(), n));
82         }
83 }