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