Renamed GUI to Model:
[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.ViewModelModifiable;
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.ViewLogicModelAdapter;
14 import net.mograsim.logic.model.modeladapter.componentadapters.MergerAdapter;
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 ModelMerger 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 outputPin;
28
29         private final ReadEnd[] inputEnds;
30         private ReadEnd outputEnd;
31
32         public ModelMerger(ViewModelModifiable model, int logicWidth)
33         {
34                 this(model, logicWidth, null);
35         }
36
37         public ModelMerger(ViewModelModifiable model, int logicWidth, String name)
38         {
39                 super(model, name);
40                 this.logicWidth = logicWidth;
41                 setSize(width, (logicWidth - 1) * heightPerPin);
42                 double inputHeight = (logicWidth - 1) * heightPerPin;
43                 for (int i = 0; i < logicWidth; i++, inputHeight -= 10)
44                         addPin(new Pin(this, "I" + i, 1, PinUsage.TRISTATE, 0, inputHeight));
45                 addPin(this.outputPin = new Pin(this, "O", logicWidth, PinUsage.TRISTATE, width, (logicWidth - 1) * heightPerPin / 2));
46                 inputEnds = new ReadEnd[logicWidth];
47         }
48
49         @Override
50         public void render(GeneralGC gc, Rectangle visibleRegion)
51         {
52                 double posX = getPosX();
53                 double posY = getPosY();
54
55                 ColorDefinition c = BitVectorFormatter.formatAsColor(outputEnd);
56                 if (c != null)
57                         gc.setForeground(ColorManager.current().toColor(c));
58                 gc.setLineWidth(
59                                 Preferences.current().getDouble("net.mograsim.logic.model.linewidth.wire." + (logicWidth == 1 ? "singlebit" : "multibit")));
60                 double outLineY = posY + (logicWidth - 1) * heightPerPin / 2;
61                 gc.drawLine(posX + width / 2, outLineY, posX + width, outLineY);
62                 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.wire.singlebit"));
63                 double inputHeight = posY;
64                 for (int i = 0; i < logicWidth; i++, inputHeight += 10)
65                 {
66                         c = BitVectorFormatter.formatAsColor(inputEnds[i]);
67                         if (c != null)
68                                 gc.setForeground(ColorManager.current().toColor(c));
69                         gc.drawLine(posX, inputHeight, posX + width / 2, inputHeight);
70                 }
71                 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
72                 int oldLineCap = gc.getLineCap();
73                 int lineJoin = gc.getLineJoin();
74                 // TODO find better "replacement" for JOIN_BEVEL
75                 // TODO it looks weird that the vertical line is thinner than the single multibit wire.
76                 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
77                 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
78                 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.default"));
79                 gc.setLineCap(oldLineCap);
80         }
81
82         @Override
83         public String getIDForSerializing(IdentifyParams idParams)
84         {
85                 return "Merger";
86         }
87
88         @Override
89         public Integer getParamsForSerializing(IdentifyParams idParams)
90         {
91                 return logicWidth;
92         }
93
94         public void setLogicModelBinding(ReadEnd[] inputEnds, ReadEnd outputEnd)
95         {
96                 System.arraycopy(inputEnds, 0, this.inputEnds, 0, logicWidth);
97                 this.outputEnd = outputEnd;
98         }
99
100         public Pin getOutputPin()
101         {
102                 return outputPin;
103         }
104
105         static
106         {
107                 ViewLogicModelAdapter.addComponentAdapter(new MergerAdapter());
108                 IndirectModelComponentCreator.setComponentSupplier(ModelMerger.class.getCanonicalName(),
109                                 (m, p, n) -> new ModelMerger(m, p.getAsInt(), n));
110         }
111 }