1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.SWT;
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;
23 //TODO delete this legacy class
24 public class ModelMerger extends ModelComponent
26 private static final double width = 10;
27 private static final double heightPerPin = 10;
29 public final int logicWidth;
30 private final Pin outputPin;
32 private final ReadEnd[] inputEnds;
33 private ReadEnd outputEnd;
35 public ModelMerger(LogicModelModifiable model, int logicWidth)
37 this(model, logicWidth, null);
40 public ModelMerger(LogicModelModifiable model, int logicWidth, String name)
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];
55 public void render(GeneralGC gc, Rectangle visibleRegion)
57 double posX = getPosX();
58 double posY = getPosY();
60 ColorDefinition c = BitVectorFormatter.formatAsColor(outputEnd);
62 gc.setForeground(ColorManager.current().toColor(c));
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)
71 c = BitVectorFormatter.formatAsColor(inputEnds[i]);
73 gc.setForeground(ColorManager.current().toColor(c));
74 gc.drawLine(posX, inputHeight, posX + width / 2, inputHeight);
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);
88 public Pin getPin(String name)
90 Pin pin = getPinOrNull(name);
91 return pin == null ? getPin(name.replace('O', 'i').replace('I', 'O').replace('i', 'I')) : pin;
95 public String getIDForSerializing(IdentifyParams idParams)
101 public SplitterParams getParamsForSerializing(IdentifyParams idParams)
103 SplitterParams splitterParams = new SplitterParams();
104 splitterParams.logicWidth = logicWidth;
105 splitterParams.orientation = Orientation.LEFT;
106 return splitterParams;
109 public void setCoreModelBinding(ReadEnd[] inputEnds, ReadEnd outputEnd)
111 System.arraycopy(inputEnds, 0, this.inputEnds, 0, logicWidth);
112 this.outputEnd = outputEnd;
115 public Pin getOutputPin()
122 LogicCoreAdapter.addComponentAdapter(new MergerAdapter());
123 IndirectModelComponentCreator.setComponentSupplier(ModelMerger.class.getCanonicalName(),
124 (m, p, n) -> new ModelMerger(m, p.getAsInt(), n));