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