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.preferences.Preferences;
24 public class ModelManualSwitch extends ModelComponent
26 private static final double width = 20;
27 private static final double height = 15;
28 private static final double fontHeight = 5;
29 private static final double heightMiniButtons = 4; // 0 is disabled
31 public final int logicWidth;
32 private final Pin outputPin;
34 private final LogicObserver logicObs;
35 private CoreManualSwitch manualSwitch;
37 public ModelManualSwitch(LogicModelModifiable model, int logicWidth)
39 this(model, logicWidth, null);
42 public ModelManualSwitch(LogicModelModifiable model, int logicWidth, String name)
45 this.logicWidth = logicWidth;
46 logicObs = (i) -> model.requestRedraw();
48 setSize(width, height);
49 addPin(this.outputPin = new Pin(model, this, "", logicWidth, PinUsage.OUTPUT, width, height / 2));
53 public void render(GeneralGC gc, Rectangle visibleRegion)
55 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
56 if (foreground != null)
57 gc.setForeground(foreground);
58 gc.drawRectangle(getBounds());
59 String label = BitVectorFormatter.formatAsString(manualSwitch == null ? null : manualSwitch.getValues());
60 Font oldFont = gc.getFont();
61 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
62 gc.setFont(labelFont);
63 Point textExtent = gc.textExtent(label);
64 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
65 if (textColor != null)
66 gc.setForeground(textColor);
67 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
70 if (manualSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons)
74 gc.drawLine(x, y + heightMiniButtons, x + width, y + heightMiniButtons);
75 Color c = gc.getBackground();
76 gc.setBackground(gc.getForeground());
77 BitVector bv = manualSwitch.getValues();
78 double part = width / bv.length();
79 for (int i = 0; i < bv.length(); i++)
81 double start = x + part * i;
83 gc.drawLine(start, y, start, y + heightMiniButtons);
84 if (bv.getMSBit(i) == Bit.ONE)
86 // gc.fillRectangle(start, y, part, heightMiniButtons); // alternative, but not always visible what Bit is where
87 gc.drawLine(start, y, start + part, y + heightMiniButtons);
88 gc.drawLine(start + part, y, start, y + heightMiniButtons);
95 public void setCoreModelBinding(CoreManualSwitch logicSwitch)
97 if (this.manualSwitch != null)
98 this.manualSwitch.deregisterObserver(logicObs);
99 this.manualSwitch = logicSwitch;
100 if (logicSwitch != null)
101 logicSwitch.registerObserver(logicObs);
104 public boolean hasCoreModelBinding()
106 return manualSwitch != null;
110 public Object getHighLevelState(String stateID)
115 if (manualSwitch != null)
116 return manualSwitch.getValues();
119 return super.getHighLevelState(stateID);
124 public void setHighLevelState(String stateID, Object newState)
129 if (manualSwitch != null)
130 manualSwitch.setState((BitVector) newState);
133 super.setHighLevelState(stateID, newState);
139 public boolean clicked(double x, double y)
141 if (manualSwitch != null)
143 if (heightMiniButtons > 0 && y - getPosY() < heightMiniButtons)
145 int part = (int) ((x - getPosX()) * logicWidth / width);
146 manualSwitch.setState(manualSwitch.getValues().withBitChanged(part, Bit::not));
149 manualSwitch.toggle();
155 public CoreManualSwitch getManualSwitch()
160 public Pin getOutputPin()
166 public String getIDForSerializing(IdentifyParams idParams)
168 return "ManualSwitch";
172 public Integer getParamsForSerializing(IdentifyParams idParams)
179 LogicCoreAdapter.addComponentAdapter(new ManualSwitchAdapter());
180 IndirectModelComponentCreator.setComponentSupplier(ModelManualSwitch.class.getName(),
181 (m, p, n) -> new ModelManualSwitch(m, p.getAsInt(), n));