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)
44 super(model, name, false);
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));
55 public void render(GeneralGC gc, Rectangle visibleRegion)
57 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
58 if (foreground != null)
59 gc.setForeground(foreground);
60 gc.drawRectangle(getBounds());
61 String label = BitVectorFormatter.formatAsString(manualSwitch == null ? null : manualSwitch.getValues());
62 Font oldFont = gc.getFont();
63 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
64 gc.setFont(labelFont);
65 Point textExtent = gc.textExtent(label);
66 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
67 if (textColor != null)
68 gc.setForeground(textColor);
69 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
72 if (manualSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons)
76 gc.drawLine(x, y + heightMiniButtons, x + width, y + heightMiniButtons);
77 Color c = gc.getBackground();
78 gc.setBackground(gc.getForeground());
79 BitVector bv = manualSwitch.getValues();
80 double part = width / bv.length();
81 for (int i = 0; i < bv.length(); i++)
83 double start = x + part * i;
85 gc.drawLine(start, y, start, y + heightMiniButtons);
86 if (bv.getMSBit(i) == Bit.ONE)
88 // gc.fillRectangle(start, y, part, heightMiniButtons); // alternative, but not always visible what Bit is where
89 gc.drawLine(start, y, start + part, y + heightMiniButtons);
90 gc.drawLine(start + part, y, start, y + heightMiniButtons);
97 public void setCoreModelBinding(CoreManualSwitch logicSwitch)
99 if (this.manualSwitch != null)
100 this.manualSwitch.deregisterObserver(logicObs);
101 this.manualSwitch = logicSwitch;
102 if (logicSwitch != null)
103 logicSwitch.registerObserver(logicObs);
106 public boolean hasCoreModelBinding()
108 return manualSwitch != null;
112 public Object getHighLevelState(String stateID)
117 if (manualSwitch != null)
118 return manualSwitch.getValues();
121 return super.getHighLevelState(stateID);
126 public void setHighLevelState(String stateID, Object newState)
131 if (manualSwitch != null)
132 manualSwitch.setState((BitVector) newState);
135 super.setHighLevelState(stateID, newState);
141 public boolean clicked(double x, double y)
143 if (manualSwitch != null)
145 if (heightMiniButtons > 0 && y - getPosY() < heightMiniButtons)
147 int part = (int) ((x - getPosX()) * logicWidth / width);
148 manualSwitch.setState(manualSwitch.getValues().withBitChanged(part, Bit::not));
151 manualSwitch.toggle();
157 public CoreManualSwitch getManualSwitch()
162 public Pin getOutputPin()
168 public String getIDForSerializing(IdentifyParams idParams)
170 return "ManualSwitch";
174 public Integer getParamsForSerializing(IdentifyParams idParams)
181 LogicCoreAdapter.addComponentAdapter(new ManualSwitchAdapter());
182 IndirectModelComponentCreator.setComponentSupplier(ModelManualSwitch.class.getName(),
183 (m, p, n) -> new ModelManualSwitch(m, p.getAsInt(), n));