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.Point;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.core.LogicObserver;
14 import net.mograsim.logic.core.components.CoreManualSwitch;
15 import net.mograsim.logic.core.types.Bit;
16 import net.mograsim.logic.core.types.BitVector;
17 import net.mograsim.logic.core.types.BitVectorFormatter;
18 import net.mograsim.logic.model.model.LogicModelModifiable;
19 import net.mograsim.logic.model.model.components.ModelComponent;
20 import net.mograsim.logic.model.model.wires.Pin;
21 import net.mograsim.logic.model.model.wires.PinUsage;
22 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
23 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
24 import net.mograsim.logic.model.serializing.IdentifyParams;
25 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
26 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
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
36 public final int logicWidth;
37 private final Pin outputPin;
39 private final LogicObserver logicObs;
40 private CoreManualSwitch manualSwitch;
42 private final List<Consumer<Object>> hlsListeners;
44 public ModelManualSwitch(LogicModelModifiable model, int logicWidth)
46 this(model, logicWidth, null);
49 public ModelManualSwitch(LogicModelModifiable model, int logicWidth, String name)
51 super(model, name, false);
52 this.logicWidth = logicWidth;
54 setSize(width, height);
55 addPin(this.outputPin = new Pin(model, this, "", logicWidth, PinUsage.OUTPUT, width, height / 2));
57 hlsListeners = new ArrayList<>();
61 model.requestRedraw();
62 BitVector v = getOutValues();
63 hlsListeners.forEach(l -> l.accept(v));
66 setHighLevelStateHandler(new HighLevelStateHandler()
69 public Object get(String stateID)
74 if (manualSwitch != null)
75 return getOutValues();
78 throw new IllegalArgumentException("No high level state with ID " + stateID);
83 public void set(String stateID, Object newState)
88 if (manualSwitch != null)
89 manualSwitch.setState((BitVector) newState);
92 throw new IllegalArgumentException("No high level state with ID " + stateID);
97 public void addListener(String stateID, Consumer<Object> stateChanged)
102 hlsListeners.add(stateChanged);
105 throw new IllegalArgumentException("No high level state with ID " + stateID);
110 public void removeListener(String stateID, java.util.function.Consumer<Object> stateChanged)
115 hlsListeners.remove(stateChanged);
118 throw new IllegalArgumentException("No high level state with ID " + stateID);
123 public String getIDForSerializing(IdentifyParams idParams)
129 public Object getParamsForSerializing(IdentifyParams idParams)
139 public void render(GeneralGC gc, Rectangle visibleRegion)
141 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
142 if (foreground != null)
143 gc.setForeground(foreground);
144 gc.drawRectangle(getBounds());
145 String label = BitVectorFormatter.formatAsString(manualSwitch == null ? null : getOutValues(), false);
146 Font oldFont = gc.getFont();
147 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
148 gc.setFont(labelFont);
149 Point textExtent = gc.textExtent(label);
150 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
151 if (textColor != null)
152 gc.setForeground(textColor);
153 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, 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));