Merge branch 'development' of
[Mograsim.git] / 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.wires.Pin;
12 import net.mograsim.logic.model.model.wires.PinUsage;
13 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
14 import net.mograsim.logic.model.modeladapter.componentadapters.SplitterAdapter;
15 import net.mograsim.logic.model.serializing.IdentifyParams;
16 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
17 import net.mograsim.preferences.ColorDefinition;
18 import net.mograsim.preferences.ColorManager;
19 import net.mograsim.preferences.Preferences;
20
21 public class ModelSplitter extends ModelComponent
22 {
23         private static final double width = 10;
24         private static final double heightPerPin = 10;
25
26         public final int logicWidth;
27         private final Pin inputPin;
28
29         private ReadEnd inputEnd;
30         private final ReadEnd[] outputEnds;
31
32         public ModelSplitter(LogicModelModifiable model, int logicWidth)
33         {
34                 this(model, logicWidth, null);
35         }
36
37         public ModelSplitter(LogicModelModifiable model, int logicWidth, String name)
38         {
39                 super(model, name, false);
40                 this.logicWidth = logicWidth;
41                 setSize(width, (logicWidth - 1) * heightPerPin);
42                 addPin(this.inputPin = new Pin(model, this, "I", logicWidth, PinUsage.TRISTATE, 0, (logicWidth - 1) * heightPerPin / 2));
43                 double outputHeight = (logicWidth - 1) * heightPerPin;
44                 for (int i = 0; i < logicWidth; i++, outputHeight -= 10)
45                         addPin(new Pin(model, this, "O" + i, 1, PinUsage.TRISTATE, width, outputHeight));
46                 outputEnds = new ReadEnd[logicWidth];
47
48                 init();
49         }
50
51         @Override
52         public void render(GeneralGC gc, Rectangle visibleRegion)
53         {
54                 double posX = getPosX();
55                 double posY = getPosY();
56
57                 ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnd);
58                 if (c != null)
59                         gc.setForeground(ColorManager.current().toColor(c));
60                 gc.setLineWidth(
61                                 Preferences.current().getDouble("net.mograsim.logic.model.linewidth.wire." + (logicWidth == 1 ? "singlebit" : "multibit")));
62                 double inLineY = posY + (logicWidth - 1) * heightPerPin / 2;
63                 gc.drawLine(posX, inLineY, posX + width / 2, inLineY);
64                 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.wire.singlebit"));
65                 double outputHeight = posY;
66                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
67                 {
68                         c = BitVectorFormatter.formatAsColor(outputEnds[i]);
69                         if (c != null)
70                                 gc.setForeground(ColorManager.current().toColor(c));
71                         gc.drawLine(posX + width / 2, outputHeight, posX + width, outputHeight);
72                 }
73                 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
74                 int oldLineCap = gc.getLineCap();
75                 int lineJoin = gc.getLineJoin();
76                 // TODO find better "replacement" for JOIN_BEVEL
77                 // TODO it looks weird that the vertical line is thinner than the single multibit wire.
78                 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
79                 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
80                 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.default"));
81                 gc.setLineCap(oldLineCap);
82         }
83
84         @Override
85         public String getIDForSerializing(IdentifyParams idParams)
86         {
87                 return "Splitter";
88         }
89
90         @Override
91         public Integer getParamsForSerializing(IdentifyParams idParams)
92         {
93                 return logicWidth;
94         }
95
96         public void setCoreModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
97         {
98                 this.inputEnd = inputEnd;
99                 System.arraycopy(outputEnds, 0, this.outputEnds, 0, logicWidth);
100         }
101
102         public Pin getInputPin()
103         {
104                 return inputPin;
105         }
106
107         static
108         {
109                 LogicCoreAdapter.addComponentAdapter(new SplitterAdapter());
110                 IndirectModelComponentCreator.setComponentSupplier(ModelSplitter.class.getCanonicalName(),
111                                 (m, p, n) -> new ModelSplitter(m, p.getAsInt(), n));
112         }
113 }