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.model.wires.PinUsage;
16 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
17 import net.mograsim.logic.model.modeladapter.componentadapters.SplitterAdapter;
18 import net.mograsim.logic.model.serializing.IdentifierGetter;
19 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
20 import net.mograsim.preferences.ColorDefinition;
21 import net.mograsim.preferences.ColorManager;
22 import net.mograsim.preferences.Preferences;
24 public class GUISplitter extends GUIComponent
26 private static final double width = 10;
27 private static final double heightPerPin = 10;
29 public final int logicWidth;
30 private final Pin inputPin;
32 private ReadEnd inputEnd;
33 private final ReadEnd[] outputEnds;
35 public GUISplitter(ViewModelModifiable model, int logicWidth)
37 this(model, logicWidth, null);
40 public GUISplitter(ViewModelModifiable model, int logicWidth, String name)
43 this.logicWidth = logicWidth;
44 setSize(width, (logicWidth - 1) * heightPerPin);
45 addPin(this.inputPin = new Pin(this, "I", logicWidth, PinUsage.TRISTATE, 0, (logicWidth - 1) * heightPerPin / 2));
46 double outputHeight = (logicWidth - 1) * heightPerPin;
47 for (int i = 0; i < logicWidth; i++, outputHeight -= 10)
48 addPin(new Pin(this, "O" + i, 1, PinUsage.TRISTATE, width, outputHeight));
49 outputEnds = new ReadEnd[logicWidth];
53 public void render(GeneralGC gc, Rectangle visibleRegion)
55 double posX = getPosX();
56 double posY = getPosY();
58 ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnd);
60 gc.setForeground(ColorManager.current().toColor(c));
61 double inLineY = posY + (logicWidth - 1) * heightPerPin / 2;
62 gc.drawLine(posX, inLineY, posX + width / 2, inLineY);
63 double outputHeight = posY + (logicWidth - 1) * heightPerPin;
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 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
76 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
77 gc.setLineCap(oldLineCap);
81 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
83 return new JsonPrimitive(logicWidth);
86 public void setLogicModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
88 this.inputEnd = inputEnd;
89 System.arraycopy(outputEnds, 0, this.outputEnds, 0, logicWidth);
92 public Pin getInputPin()
99 ViewLogicModelAdapter.addComponentAdapter(new SplitterAdapter());
100 IndirectGUIComponentCreator.setComponentSupplier(GUISplitter.class.getCanonicalName(),
101 (m, p, n) -> new GUISplitter(m, p.getAsInt(), n));