Added ModelSplitter.getOutputPin(int)
[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         private final Pin[] outputPins;
40
41         private ReadEnd inputEnd;
42         private final ReadEnd[] outputEnds;
43
44         public ModelSplitter(LogicModelModifiable model, SplitterParams params)
45         {
46                 this(model, params, null);
47         }
48
49         public ModelSplitter(LogicModelModifiable model, SplitterParams params, String name)
50         {
51                 super(model, name, false);
52                 this.logicWidth = params.logicWidth;
53                 this.oc = new OrientationCalculator(toggleLeftDownAlt(params.orientation), width,
54                                 this.heightWithoutOC = (logicWidth - 1) * heightPerPin);
55                 setSize(oc.width(), oc.height());
56                 double inLineY = (logicWidth - 1) * heightPerPin / 2;
57                 addPin(this.inputPin = new Pin(model, this, "I", logicWidth, PinUsage.TRISTATE, oc.newX(0, inLineY), oc.newY(0, inLineY)));
58                 double outputHeight = (logicWidth - 1) * heightPerPin;
59                 this.outputPins = new Pin[logicWidth];
60                 for (int i = 0; i < logicWidth; i++, outputHeight -= 10)
61                         addPin(outputPins[i] = new Pin(model, this, "O" + i, 1, PinUsage.TRISTATE, oc.newX(width, outputHeight),
62                                         oc.newY(width, outputHeight)));
63                 outputEnds = new ReadEnd[logicWidth];
64
65                 init();
66         }
67
68         @Override
69         public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
70         {
71                 double posX = getPosX();
72                 double posY = getPosY();
73
74                 ColorDefinition c = BitVectorFormatter.formatAsColor(renderPrefs, inputEnd);
75                 if (c != null)
76                         gc.setForeground(ColorManager.current().toColor(c));
77                 gc.setLineWidth(renderPrefs.getDouble(logicWidth == 1 ? WIRE_WIDTH_SINGLEBIT : WIRE_WIDTH_MULTIBIT));
78                 double inLineY = heightWithoutOC / 2;
79                 gc.drawLine(posX + oc.newX(0, inLineY), posY + oc.newY(0, inLineY), posX + oc.newX(width / 2, inLineY),
80                                 posY + oc.newY(width / 2, inLineY));
81                 gc.setLineWidth(renderPrefs.getDouble(WIRE_WIDTH_SINGLEBIT));
82                 double outputHeight = 0;
83                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
84                 {
85                         c = BitVectorFormatter.formatAsColor(renderPrefs, outputEnds[i]);
86                         if (c != null)
87                                 gc.setForeground(ColorManager.current().toColor(c));
88                         gc.drawLine(posX + oc.newX(width / 2, outputHeight), posY + oc.newY(width / 2, outputHeight),
89                                         posX + oc.newX(width, outputHeight), posY + oc.newY(width, outputHeight));
90                 }
91                 gc.setForeground(renderPrefs.getColor(FOREGROUND_COLOR));
92                 int oldLineCap = gc.getLineCap();
93                 int lineJoin = gc.getLineJoin();
94                 // TODO find better "replacement" for JOIN_BEVEL
95                 // TODO it looks weird that the vertical line is thinner than the single multibit wire.
96                 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
97                 gc.drawLine(posX + oc.newX(width / 2, 0), posY + oc.newY(width / 2, 0), posX + oc.newX(width / 2, heightWithoutOC),
98                                 posY + oc.newY(width / 2, heightWithoutOC));
99                 gc.setLineWidth(renderPrefs.getDouble(DEFAULT_LINE_WIDTH));
100                 gc.setLineCap(oldLineCap);
101         }
102
103         @Override
104         public String getIDForSerializing(IdentifyParams idParams)
105         {
106                 return "Splitter";
107         }
108
109         @Override
110         public SplitterParams getParamsForSerializing(IdentifyParams idParams)
111         {
112                 SplitterParams splitterParams = new SplitterParams();
113                 splitterParams.logicWidth = logicWidth;
114                 splitterParams.orientation = toggleLeftDownAlt(oc.getOrientation());
115                 return splitterParams;
116         }
117
118         public void setCoreModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
119         {
120                 this.inputEnd = inputEnd;
121                 System.arraycopy(outputEnds, 0, this.outputEnds, 0, logicWidth);
122         }
123
124         public Pin getInputPin()
125         {
126                 return inputPin;
127         }
128
129         public Pin getOutputPin(int i)
130         {
131                 return outputPins[i];
132         }
133
134         /**
135          * Used to leave bit order intuitive (MSB left or on top)
136          */
137         private static Orientation toggleLeftDownAlt(Orientation orientation)
138         {
139                 // TODO if we upgrade to Java 12, replace with switch-expression
140                 switch (orientation)
141                 {
142                 case LEFT:
143                         return Orientation.LEFT_ALT;
144                 case LEFT_ALT:
145                         return Orientation.LEFT;
146                 case DOWN:
147                         return Orientation.DOWN_ALT;
148                 case DOWN_ALT:
149                         return Orientation.DOWN;
150                 default:
151                         return orientation;
152                 }
153         }
154
155         public static class SplitterParams
156         {
157                 public int logicWidth;
158                 public Orientation orientation;
159
160                 public SplitterParams()
161                 {
162                 }
163
164                 public SplitterParams(int logicWidth, Orientation orientation)
165                 {
166                         this.logicWidth = logicWidth;
167                         this.orientation = orientation;
168                 }
169         }
170
171         static
172         {
173                 LogicCoreAdapter.addComponentAdapter(new SplitterAdapter());
174                 IndirectModelComponentCreator.setComponentSupplier(ModelSplitter.class.getCanonicalName(),
175                                 (m, p, n) -> new ModelSplitter(m, JsonHandler.fromJsonTree(p, SplitterParams.class), n));
176         }
177 }