Fixed GUIMerger/GUISplitter rendering
[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 org.eclipse.swt.SWT;
4
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonPrimitive;
7
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
10 import net.mograsim.logic.core.types.BitVectorFormatter;
11 import net.mograsim.logic.core.wires.Wire.ReadEnd;
12 import net.mograsim.logic.model.model.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.GUIComponent;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.model.wires.PinUsage;
16 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
17 import net.mograsim.logic.model.modeladapter.componentadapters.MergerAdapter;
18 import net.mograsim.logic.model.serializing.IdentifierGetter;
19 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
20 import net.mograsim.preferences.ColorDefinition;
21 import net.mograsim.preferences.ColorManager;
22 import net.mograsim.preferences.Preferences;
23
24 public class GUIMerger extends GUIComponent
25 {
26         private static final double width = 10;
27         private static final double heightPerPin = 10;
28
29         public final int logicWidth;
30         private final Pin outputPin;
31
32         private final ReadEnd[] inputEnds;
33         private ReadEnd outputEnd;
34
35         public GUIMerger(ViewModelModifiable model, int logicWidth)
36         {
37                 this(model, logicWidth, null);
38         }
39
40         public GUIMerger(ViewModelModifiable model, int logicWidth, String name)
41         {
42                 super(model, name);
43                 this.logicWidth = logicWidth;
44                 setSize(width, (logicWidth - 1) * heightPerPin);
45                 double inputHeight = (logicWidth - 1) * heightPerPin;
46                 for (int i = 0; i < logicWidth; i++, inputHeight -= 10)
47                         addPin(new Pin(this, "I" + i, 1, PinUsage.TRISTATE, 0, inputHeight));
48                 addPin(this.outputPin = new Pin(this, "O", logicWidth, PinUsage.TRISTATE, width, (logicWidth - 1) * heightPerPin / 2));
49                 inputEnds = new ReadEnd[logicWidth];
50         }
51
52         @Override
53         public void render(GeneralGC gc, Rectangle visibleRegion)
54         {
55                 double posX = getPosX();
56                 double posY = getPosY();
57
58                 ColorDefinition c = BitVectorFormatter.formatAsColor(outputEnd);
59                 if (c != null)
60                         gc.setForeground(ColorManager.current().toColor(c));
61                 double outLineY = posY + (logicWidth - 1) * heightPerPin / 2;
62                 gc.drawLine(posX + width / 2, outLineY, posX + width, outLineY);
63                 double inputHeight = posY + (logicWidth - 1) * heightPerPin;
64                 for (int i = 0; i < logicWidth; i++, inputHeight -= 10)
65                 {
66                         c = BitVectorFormatter.formatAsColor(inputEnds[i]);
67                         if (c != null)
68                                 gc.setForeground(ColorManager.current().toColor(c));
69                         gc.drawLine(posX, inputHeight, posX + width / 2, inputHeight);
70                 }
71                 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
72                 int oldLineCap = gc.getLineCap();
73                 int lineJoin = gc.getLineJoin();
74                 // TODO find better "replacement" for JOIN_BEVEL
75                 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
76                 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
77                 gc.setLineCap(oldLineCap);
78         }
79
80         @Override
81         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
82         {
83                 return new JsonPrimitive(logicWidth);
84         }
85
86         public void setLogicModelBinding(ReadEnd[] inputEnds, ReadEnd outputEnd)
87         {
88                 System.arraycopy(inputEnds, 0, this.inputEnds, 0, logicWidth);
89                 this.outputEnd = outputEnd;
90         }
91
92         public Pin getOutputPin()
93         {
94                 return outputPin;
95         }
96
97         static
98         {
99                 ViewLogicModelAdapter.addComponentAdapter(new MergerAdapter());
100                 IndirectGUIComponentCreator.setComponentSupplier(GUIMerger.class.getCanonicalName(),
101                                 (m, p, n) -> new GUIMerger(m, p.getAsInt(), n));
102         }
103 }