1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
9 import net.mograsim.logic.core.LogicObserver;
10 import net.mograsim.logic.core.components.CoreManualSwitch;
11 import net.mograsim.logic.core.types.Bit;
12 import net.mograsim.logic.core.types.BitVector;
13 import net.mograsim.logic.core.types.BitVectorFormatter;
14 import net.mograsim.logic.model.model.LogicModelModifiable;
15 import net.mograsim.logic.model.model.components.ModelComponent;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.model.wires.PinUsage;
18 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
19 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
20 import net.mograsim.logic.model.serializing.IdentifyParams;
21 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
22 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
23 import net.mograsim.preferences.Preferences;
25 public class ModelManualSwitch extends ModelComponent
27 private static final double width = 20;
28 private static final double height = 15;
29 private static final double fontHeight = 5;
30 private static final double heightMiniButtons = 4; // 0 is disabled
32 public final int logicWidth;
33 private final Pin outputPin;
35 private final LogicObserver logicObs;
36 private CoreManualSwitch manualSwitch;
38 public ModelManualSwitch(LogicModelModifiable model, int logicWidth)
40 this(model, logicWidth, null);
43 public ModelManualSwitch(LogicModelModifiable model, int logicWidth, String name)
45 super(model, name, false);
46 this.logicWidth = logicWidth;
47 logicObs = (i) -> model.requestRedraw();
49 setSize(width, height);
50 addPin(this.outputPin = new Pin(model, this, "", logicWidth, PinUsage.OUTPUT, width, height / 2));
52 setHighLevelStateHandler(new HighLevelStateHandler()
55 public Object getHighLevelState(String stateID)
60 if (manualSwitch != null)
61 return manualSwitch.getValues();
64 throw new IllegalArgumentException("No high level state with ID " + stateID);
69 public void setHighLevelState(String stateID, Object newState)
74 if (manualSwitch != null)
75 manualSwitch.setState((BitVector) newState);
78 throw new IllegalArgumentException("No high level state with ID " + stateID);
83 public String getIDForSerializing(IdentifyParams idParams)
89 public Object getParamsForSerializing(IdentifyParams idParams)
99 public void render(GeneralGC gc, Rectangle visibleRegion)
101 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
102 if (foreground != null)
103 gc.setForeground(foreground);
104 gc.drawRectangle(getBounds());
105 String label = BitVectorFormatter.formatAsString(manualSwitch == null ? null : manualSwitch.getValues());
106 Font oldFont = gc.getFont();
107 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
108 gc.setFont(labelFont);
109 Point textExtent = gc.textExtent(label);
110 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
111 if (textColor != null)
112 gc.setForeground(textColor);
113 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
116 if (manualSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons)
118 double x = getPosX();
119 double y = getPosY();
120 gc.drawLine(x, y + heightMiniButtons, x + width, y + heightMiniButtons);
121 Color c = gc.getBackground();
122 gc.setBackground(gc.getForeground());
123 BitVector bv = manualSwitch.getValues();
124 double part = width / bv.length();
125 for (int i = 0; i < bv.length(); i++)
127 double start = x + part * i;
129 gc.drawLine(start, y, start, y + heightMiniButtons);
130 if (bv.getMSBit(i) == Bit.ONE)
132 // gc.fillRectangle(start, y, part, heightMiniButtons); // alternative, but not always visible what Bit is where
133 gc.drawLine(start, y, start + part, y + heightMiniButtons);
134 gc.drawLine(start + part, y, start, y + heightMiniButtons);
141 public void setCoreModelBinding(CoreManualSwitch logicSwitch)
143 if (this.manualSwitch != null)
144 this.manualSwitch.deregisterObserver(logicObs);
145 this.manualSwitch = logicSwitch;
146 if (logicSwitch != null)
147 logicSwitch.registerObserver(logicObs);
150 public boolean hasCoreModelBinding()
152 return manualSwitch != null;
156 public boolean clicked(double x, double y)
158 if (manualSwitch != null)
160 if (heightMiniButtons > 0 && y - getPosY() < heightMiniButtons)
162 int part = (int) ((x - getPosX()) * logicWidth / width);
163 manualSwitch.setState(manualSwitch.getValues().withBitChanged(part, Bit::not));
166 manualSwitch.toggle();
172 public CoreManualSwitch getManualSwitch()
177 public Pin getOutputPin()
183 public String getIDForSerializing(IdentifyParams idParams)
185 return "ManualSwitch";
189 public Integer getParamsForSerializing(IdentifyParams idParams)
196 LogicCoreAdapter.addComponentAdapter(new ManualSwitchAdapter());
197 IndirectModelComponentCreator.setComponentSupplier(ModelManualSwitch.class.getName(),
198 (m, p, n) -> new ModelManualSwitch(m, p.getAsInt(), n));