1 package net.mograsim.logic.model.model.components.atomic;
3 import java.util.ArrayList;
5 import java.util.function.Consumer;
7 import org.eclipse.swt.graphics.Color;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.core.LogicObserver;
13 import net.mograsim.logic.core.components.CoreManualSwitch;
14 import net.mograsim.logic.core.types.Bit;
15 import net.mograsim.logic.core.types.BitVector;
16 import net.mograsim.logic.core.types.BitVectorFormatter;
17 import net.mograsim.logic.model.model.LogicModelModifiable;
18 import net.mograsim.logic.model.model.components.ModelComponent;
19 import net.mograsim.logic.model.model.wires.Pin;
20 import net.mograsim.logic.model.model.wires.PinUsage;
21 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
22 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
23 import net.mograsim.logic.model.serializing.IdentifyParams;
24 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
25 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
26 import net.mograsim.logic.model.util.TextRenderingHelper;
27 import net.mograsim.preferences.Preferences;
29 public class ModelManualSwitch extends ModelComponent
31 private static final double width = 20;
32 private static final double height = 10;
33 private static final double fontHeight = 5;
34 private static final double heightMiniButtons = 4; // 0 is disabled
35 private static final double textMargin = 0.5;
37 public final int logicWidth;
38 private final Pin outputPin;
40 private final LogicObserver logicObs;
41 private CoreManualSwitch manualSwitch;
43 private final List<Consumer<Object>> hlsListeners;
45 public ModelManualSwitch(LogicModelModifiable model, int logicWidth)
47 this(model, logicWidth, null);
50 public ModelManualSwitch(LogicModelModifiable model, int logicWidth, String name)
52 super(model, name, false);
53 this.logicWidth = logicWidth;
55 setSize(width, height);
56 addPin(this.outputPin = new Pin(model, this, "", logicWidth, PinUsage.OUTPUT, width, height / 2));
58 hlsListeners = new ArrayList<>();
62 model.requestRedraw();
63 BitVector v = getOutValues();
64 hlsListeners.forEach(l -> l.accept(v));
67 setHighLevelStateHandler(new HighLevelStateHandler()
70 public Object get(String stateID)
75 if (manualSwitch != null)
76 return getOutValues();
79 throw new IllegalArgumentException("No high level state with ID " + stateID);
84 public void set(String stateID, Object newState)
89 if (manualSwitch != null)
90 manualSwitch.setState((BitVector) newState);
93 throw new IllegalArgumentException("No high level state with ID " + stateID);
98 public void addListener(String stateID, Consumer<Object> stateChanged)
103 hlsListeners.add(stateChanged);
106 throw new IllegalArgumentException("No high level state with ID " + stateID);
111 public void removeListener(String stateID, java.util.function.Consumer<Object> stateChanged)
116 hlsListeners.remove(stateChanged);
119 throw new IllegalArgumentException("No high level state with ID " + stateID);
124 public String getIDForSerializing(IdentifyParams idParams)
130 public Object getParamsForSerializing(IdentifyParams idParams)
140 public void render(GeneralGC gc, Rectangle visibleRegion)
142 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
143 if (foreground != null)
144 gc.setForeground(foreground);
145 gc.drawRectangle(getBounds());
146 String label = BitVectorFormatter.formatAsString(manualSwitch == null ? null : getOutValues(), false);
147 Font oldFont = gc.getFont();
148 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
149 gc.setFont(labelFont);
150 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
151 if (textColor != null)
152 gc.setForeground(textColor);
153 TextRenderingHelper.drawTextFitting(gc, label, getBounds(), textMargin, true);
156 if (manualSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons)
158 double x = getPosX();
159 double y = getPosY();
160 gc.drawLine(x, y + heightMiniButtons, x + width, y + heightMiniButtons);
161 Color c = gc.getBackground();
162 gc.setBackground(gc.getForeground());
163 BitVector bv = getOutValues();
164 double part = width / bv.length();
165 for (int i = 0; i < bv.length(); i++)
167 double start = x + part * i;
169 gc.drawLine(start, y, start, y + heightMiniButtons);
170 if (bv.getMSBit(i) == Bit.ONE)
172 // gc.fillRectangle(start, y, part, heightMiniButtons); // alternative, but not always visible what Bit is where
173 gc.drawLine(start, y, start + part, y + heightMiniButtons);
174 gc.drawLine(start + part, y, start, y + heightMiniButtons);
181 public void setCoreModelBinding(CoreManualSwitch logicSwitch)
183 if (this.manualSwitch != null)
184 this.manualSwitch.deregisterObserver(logicObs);
185 this.manualSwitch = logicSwitch;
186 if (logicSwitch != null)
187 logicSwitch.registerObserver(logicObs);
190 public boolean hasCoreModelBinding()
192 return manualSwitch != null;
196 public boolean clicked(double x, double y)
198 if (manualSwitch != null)
200 if (heightMiniButtons > 0 && y - getPosY() < heightMiniButtons)
202 int part = (int) ((x - getPosX()) * logicWidth / width);
203 manualSwitch.setState(getOutValues().withBitChanged(part, Bit::not));
206 manualSwitch.toggle();
212 public CoreManualSwitch getManualSwitch()
217 public Pin getOutputPin()
223 public String getIDForSerializing(IdentifyParams idParams)
225 return "ManualSwitch";
229 public Integer getParamsForSerializing(IdentifyParams idParams)
234 private BitVector getOutValues()
236 return manualSwitch.getValues();
241 LogicCoreAdapter.addComponentAdapter(new ManualSwitchAdapter());
242 IndirectModelComponentCreator.setComponentSupplier(ModelManualSwitch.class.getName(),
243 (m, p, n) -> new ModelManualSwitch(m, p.getAsInt(), n));