1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.SWT;
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonPrimitive;
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
10 import net.mograsim.logic.core.types.BitVectorFormatter;
11 import net.mograsim.logic.core.wires.Wire.ReadEnd;
12 import net.mograsim.logic.model.model.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.GUIComponent;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
16 import net.mograsim.logic.model.modeladapter.componentadapters.MergerAdapter;
17 import net.mograsim.logic.model.serializing.IdentifierGetter;
18 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
19 import net.mograsim.preferences.ColorDefinition;
20 import net.mograsim.preferences.ColorManager;
21 import net.mograsim.preferences.Preferences;
23 public class GUIMerger extends GUIComponent
25 private static final double width = 10;
26 private static final double heightPerPin = 10;
28 public final int logicWidth;
29 private final Pin outputPin;
31 private final ReadEnd[] inputEnds;
32 private ReadEnd outputEnd;
34 public GUIMerger(ViewModelModifiable model, int logicWidth)
36 this(model, logicWidth, null);
39 public GUIMerger(ViewModelModifiable model, int logicWidth, String name)
42 this.logicWidth = logicWidth;
43 setSize(width, logicWidth * heightPerPin);
44 double inputHeight = 0;
45 for (int i = 0; i < logicWidth; i++, inputHeight += 10)
46 addPin(new Pin(this, "I" + i, 1, 0, inputHeight));
47 addPin(this.outputPin = new Pin(this, "O", logicWidth, width, (logicWidth - 1) * heightPerPin / 2));
48 inputEnds = new ReadEnd[logicWidth];
52 public void render(GeneralGC gc, Rectangle visibleRegion)
54 double posX = getPosX();
55 double posY = getPosY();
57 ColorDefinition c = BitVectorFormatter.formatAsColor(outputEnd);
59 gc.setForeground(ColorManager.current().toColor(c));
60 double outLineY = posY + (logicWidth - 1) * heightPerPin / 2;
61 gc.drawLine(posX + width / 2, outLineY, posX + width, outLineY);
62 double inputHeight = posY;
63 for (int i = 0; i < logicWidth; i++, inputHeight += 10)
65 c = BitVectorFormatter.formatAsColor(inputEnds[i]);
67 gc.setForeground(ColorManager.current().toColor(c));
68 gc.drawLine(posX, inputHeight, posX + width / 2, inputHeight);
70 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
71 int oldLineCap = gc.getLineCap();
72 int lineJoin = gc.getLineJoin();
73 // TODO find better "replacement" for JOIN_BEVEL
74 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
75 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
76 gc.setLineCap(oldLineCap);
80 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
82 return new JsonPrimitive(logicWidth);
85 public void setLogicModelBinding(ReadEnd[] inputEnds, ReadEnd outputEnd)
87 System.arraycopy(inputEnds, 0, this.inputEnds, 0, logicWidth);
88 this.outputEnd = outputEnd;
91 public Pin getOutputPin()
98 ViewLogicModelAdapter.addComponentAdapter(new MergerAdapter());
99 IndirectGUIComponentCreator.setComponentSupplier(GUIMerger.class.getCanonicalName(),
100 (m, p, n) -> new GUIMerger(m, p.getAsInt(), n));