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.SplitterAdapter;
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 GUISplitter 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 inputPin;
29 private ReadEnd inputEnd;
30 private final ReadEnd[] outputEnds;
32 public GUISplitter(ViewModelModifiable model, int logicWidth)
34 this(model, logicWidth, null);
37 public GUISplitter(ViewModelModifiable model, int logicWidth, String name)
40 this.logicWidth = logicWidth;
41 setSize(width, (logicWidth - 1) * heightPerPin);
42 addPin(this.inputPin = new Pin(this, "I", logicWidth, PinUsage.TRISTATE, 0, (logicWidth - 1) * heightPerPin / 2));
43 double outputHeight = (logicWidth - 1) * heightPerPin;
44 for (int i = 0; i < logicWidth; i++, outputHeight -= 10)
45 addPin(new Pin(this, "O" + i, 1, PinUsage.TRISTATE, width, outputHeight));
46 outputEnds = new ReadEnd[logicWidth];
50 public void render(GeneralGC gc, Rectangle visibleRegion)
52 double posX = getPosX();
53 double posY = getPosY();
55 ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnd);
57 gc.setForeground(ColorManager.current().toColor(c));
59 Preferences.current().getDouble("net.mograsim.logic.model.linewidth.wire." + (logicWidth == 1 ? "singlebit" : "multibit")));
60 double inLineY = posY + (logicWidth - 1) * heightPerPin / 2;
61 gc.drawLine(posX, inLineY, posX + width / 2, inLineY);
62 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.wire.singlebit"));
63 double outputHeight = posY;
64 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
66 c = BitVectorFormatter.formatAsColor(outputEnds[i]);
68 gc.setForeground(ColorManager.current().toColor(c));
69 gc.drawLine(posX + width / 2, outputHeight, posX + width, outputHeight);
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);
83 public Integer getParamsForSerializing(IdentifierGetter idGetter)
88 public void setLogicModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
90 this.inputEnd = inputEnd;
91 System.arraycopy(outputEnds, 0, this.outputEnds, 0, logicWidth);
94 public Pin getInputPin()
101 ViewLogicModelAdapter.addComponentAdapter(new SplitterAdapter());
102 IndirectGUIComponentCreator.setComponentSupplier(GUISplitter.class.getCanonicalName(),
103 (m, p, n) -> new GUISplitter(m, p.getAsInt(), n));