Added a constructor for ModelSplitterParams; added a TODO
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / ModelSplitter.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import static net.mograsim.logic.model.preferences.RenderPreferences.DEFAULT_LINE_WIDTH;
4 import static net.mograsim.logic.model.preferences.RenderPreferences.FOREGROUND_COLOR;
5 import static net.mograsim.logic.model.preferences.RenderPreferences.WIRE_WIDTH_MULTIBIT;
6 import static net.mograsim.logic.model.preferences.RenderPreferences.WIRE_WIDTH_SINGLEBIT;
7
8 import org.eclipse.swt.SWT;
9
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
13 import net.mograsim.logic.model.BitVectorFormatter;
14 import net.mograsim.logic.model.model.LogicModelModifiable;
15 import net.mograsim.logic.model.model.components.ModelComponent;
16 import net.mograsim.logic.model.model.components.Orientation;
17 import net.mograsim.logic.model.model.components.OrientationCalculator;
18 import net.mograsim.logic.model.model.wires.Pin;
19 import net.mograsim.logic.model.model.wires.PinUsage;
20 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
21 import net.mograsim.logic.model.modeladapter.componentadapters.SplitterAdapter;
22 import net.mograsim.logic.model.preferences.RenderPreferences;
23 import net.mograsim.logic.model.serializing.IdentifyParams;
24 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
25 import net.mograsim.logic.model.util.JsonHandler;
26 import net.mograsim.preferences.ColorDefinition;
27 import net.mograsim.preferences.ColorManager;
28
29 //TODO introduce a direction
30 public class ModelSplitter extends ModelComponent
31 {
32         private static final double width = 10;
33         private static final double heightPerPin = 10;
34
35         private final double heightWithoutOC;
36         public final int logicWidth;
37         private final OrientationCalculator oc;
38         private final Pin inputPin;
39
40         private ReadEnd inputEnd;
41         private final ReadEnd[] outputEnds;
42
43         public ModelSplitter(LogicModelModifiable model, SplitterParams params)
44         {
45                 this(model, params, null);
46         }
47
48         public ModelSplitter(LogicModelModifiable model, SplitterParams params, String name)
49         {
50                 super(model, name, false);
51                 this.logicWidth = params.logicWidth;
52                 this.oc = new OrientationCalculator(toggleLeftDownAlt(params.orientation), width,
53                                 this.heightWithoutOC = (logicWidth - 1) * heightPerPin);
54                 setSize(oc.width(), oc.height());
55                 double inLineY = (logicWidth - 1) * heightPerPin / 2;
56                 addPin(this.inputPin = new Pin(model, this, "I", logicWidth, PinUsage.TRISTATE, oc.newX(0, inLineY), oc.newY(0, inLineY)));
57                 double outputHeight = (logicWidth - 1) * heightPerPin;
58                 for (int i = 0; i < logicWidth; i++, outputHeight -= 10)
59                         addPin(new Pin(model, this, "O" + i, 1, PinUsage.TRISTATE, oc.newX(width, outputHeight), oc.newY(width, outputHeight)));
60                 outputEnds = new ReadEnd[logicWidth];
61
62                 init();
63         }
64
65         @Override
66         public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
67         {
68                 double posX = getPosX();
69                 double posY = getPosY();
70
71                 ColorDefinition c = BitVectorFormatter.formatAsColor(renderPrefs, inputEnd);
72                 if (c != null)
73                         gc.setForeground(ColorManager.current().toColor(c));
74                 gc.setLineWidth(renderPrefs.getDouble(logicWidth == 1 ? WIRE_WIDTH_SINGLEBIT : WIRE_WIDTH_MULTIBIT));
75                 double inLineY = heightWithoutOC / 2;
76                 gc.drawLine(posX + oc.newX(0, inLineY), posY + oc.newY(0, inLineY), posX + oc.newX(width / 2, inLineY),
77                                 posY + oc.newY(width / 2, inLineY));
78                 gc.setLineWidth(renderPrefs.getDouble(WIRE_WIDTH_SINGLEBIT));
79                 double outputHeight = 0;
80                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
81                 {
82                         c = BitVectorFormatter.formatAsColor(renderPrefs, outputEnds[i]);
83                         if (c != null)
84                                 gc.setForeground(ColorManager.current().toColor(c));
85                         gc.drawLine(posX + oc.newX(width / 2, outputHeight), posY + oc.newY(width / 2, outputHeight),
86                                         posX + oc.newX(width, outputHeight), posY + oc.newY(width, outputHeight));
87                 }
88                 gc.setForeground(renderPrefs.getColor(FOREGROUND_COLOR));
89                 int oldLineCap = gc.getLineCap();
90                 int lineJoin = gc.getLineJoin();
91                 // TODO find better "replacement" for JOIN_BEVEL
92                 // TODO it looks weird that the vertical line is thinner than the single multibit wire.
93                 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
94                 gc.drawLine(posX + oc.newX(width / 2, 0), posY + oc.newY(width / 2, 0), posX + oc.newX(width / 2, heightWithoutOC),
95                                 posY + oc.newY(width / 2, heightWithoutOC));
96                 gc.setLineWidth(renderPrefs.getDouble(DEFAULT_LINE_WIDTH));
97                 gc.setLineCap(oldLineCap);
98         }
99
100         @Override
101         public String getIDForSerializing(IdentifyParams idParams)
102         {
103                 return "Splitter";
104         }
105
106         @Override
107         public SplitterParams getParamsForSerializing(IdentifyParams idParams)
108         {
109                 SplitterParams splitterParams = new SplitterParams();
110                 splitterParams.logicWidth = logicWidth;
111                 splitterParams.orientation = toggleLeftDownAlt(oc.getOrientation());
112                 return splitterParams;
113         }
114
115         public void setCoreModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
116         {
117                 this.inputEnd = inputEnd;
118                 System.arraycopy(outputEnds, 0, this.outputEnds, 0, logicWidth);
119         }
120
121         public Pin getInputPin()
122         {
123                 return inputPin;
124         }
125
126         /**
127          * Used to leave bit order intuitive (MSB left or on top)
128          */
129         private static Orientation toggleLeftDownAlt(Orientation orientation)
130         {
131                 // TODO if we upgrade to Java 12, replace with switch-expression
132                 switch (orientation)
133                 {
134                 case LEFT:
135                         return Orientation.LEFT_ALT;
136                 case LEFT_ALT:
137                         return Orientation.LEFT;
138                 case DOWN:
139                         return Orientation.DOWN_ALT;
140                 case DOWN_ALT:
141                         return Orientation.DOWN;
142                 default:
143                         return orientation;
144                 }
145         }
146
147         public static class SplitterParams
148         {
149                 public int logicWidth;
150                 public Orientation orientation;
151
152                 public SplitterParams()
153                 {
154                 }
155
156                 public SplitterParams(int logicWidth, Orientation orientation)
157                 {
158                         this.logicWidth = logicWidth;
159                         this.orientation = orientation;
160                 }
161         }
162
163         static
164         {
165                 LogicCoreAdapter.addComponentAdapter(new SplitterAdapter());
166                 IndirectModelComponentCreator.setComponentSupplier(ModelSplitter.class.getCanonicalName(),
167                                 (m, p, n) -> new ModelSplitter(m, JsonHandler.fromJsonTree(p, SplitterParams.class), n));
168         }
169 }