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.Wire.ReadEnd;
9 import net.mograsim.logic.model.model.ViewModelModifiable;
10 import net.mograsim.logic.model.model.components.GUIComponent;
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.IdentifierGetter;
16 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
17 import net.mograsim.preferences.ColorDefinition;
18 import net.mograsim.preferences.ColorManager;
19 import net.mograsim.preferences.Preferences;
21 public class GUIMerger extends GUIComponent
23 private static final double width = 10;
24 private static final double heightPerPin = 10;
26 public final int logicWidth;
27 private final Pin outputPin;
29 private final ReadEnd[] inputEnds;
30 private ReadEnd outputEnd;
32 public GUIMerger(ViewModelModifiable model, int logicWidth)
34 this(model, logicWidth, null);
37 public GUIMerger(ViewModelModifiable model, int logicWidth, String 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];
50 public void render(GeneralGC gc, Rectangle visibleRegion)
52 double posX = getPosX();
53 double posY = getPosY();
55 ColorDefinition c = BitVectorFormatter.formatAsColor(outputEnd);
57 gc.setForeground(ColorManager.current().toColor(c));
58 double outLineY = posY + (logicWidth - 1) * heightPerPin / 2;
59 gc.drawLine(posX + width / 2, outLineY, posX + width, outLineY);
60 double inputHeight = posY;
61 for (int i = 0; i < logicWidth; i++, inputHeight += 10)
63 c = BitVectorFormatter.formatAsColor(inputEnds[i]);
65 gc.setForeground(ColorManager.current().toColor(c));
66 gc.drawLine(posX, inputHeight, posX + width / 2, inputHeight);
68 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
69 int oldLineCap = gc.getLineCap();
70 int lineJoin = gc.getLineJoin();
71 // TODO find better "replacement" for JOIN_BEVEL
72 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
73 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
74 gc.setLineCap(oldLineCap);
78 public Integer getParamsForSerializing(IdentifierGetter idGetter)
83 public void setLogicModelBinding(ReadEnd[] inputEnds, ReadEnd outputEnd)
85 System.arraycopy(inputEnds, 0, this.inputEnds, 0, logicWidth);
86 this.outputEnd = outputEnd;
89 public Pin getOutputPin()
96 ViewLogicModelAdapter.addComponentAdapter(new MergerAdapter());
97 IndirectGUIComponentCreator.setComponentSupplier(GUIMerger.class.getCanonicalName(),
98 (m, p, n) -> new GUIMerger(m, p.getAsInt(), n));