3f5c2542a44b3c42dc4d321ae55ced75c5079c1e
[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 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.MergerAdapter;
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 GUIMerger 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 final ReadEnd[] inputEnds;
29         private ReadEnd outputEnd;
30
31         public GUIMerger(ViewModelModifiable model, int logicWidth, String name)
32         {
33                 super(model, name);
34                 this.logicWidth = logicWidth;
35                 setSize(width, logicWidth * heightPerPin);
36                 double inputHeight = 0;
37                 for (int i = 0; i < logicWidth; i++, inputHeight += 10)
38                         addPin(new Pin(this, "I" + i, 1, 0, inputHeight));
39                 addPin(new Pin(this, "O", logicWidth, width, logicWidth * heightPerPin / 2));
40                 inputEnds = 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                 double inputHeight = posY;
50                 for (int i = 0; i < logicWidth; i++, inputHeight += 10)
51                 {
52                         ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnds[i]);
53                         if (c != null)
54                                 gc.setForeground(ColorManager.current().toColor(c));
55                         gc.drawLine(posX, inputHeight, posX + width / 2, inputHeight);
56                 }
57                 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
58                 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
59                 ColorDefinition c = BitVectorFormatter.formatAsColor(outputEnd);
60                 if (c != null)
61                         gc.setForeground(ColorManager.current().toColor(c));
62                 gc.drawLine(posX + width / 2, posY + heightPerPin * logicWidth / 2, posX + width, posY + heightPerPin * logicWidth / 2);
63         }
64
65         @Override
66         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
67         {
68                 return new JsonPrimitive(logicWidth);
69         }
70
71         public void setLogicModelBinding(ReadEnd[] inputEnds, ReadEnd outputEnd)
72         {
73                 System.arraycopy(inputEnds, 0, this.inputEnds, 0, logicWidth);
74                 this.outputEnd = outputEnd;
75         }
76
77         static
78         {
79                 ViewLogicModelAdapter.addComponentAdapter(new MergerAdapter());
80                 IndirectGUIComponentCreator.setComponentSupplier(GUIMerger.class.getCanonicalName(),
81                                 (m, p, n) -> new GUIMerger(m, p.getAsInt(), n));
82         }
83 }