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