1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonPrimitive;
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.core.LogicObserver;
13 import net.mograsim.logic.core.components.ManualSwitch;
14 import net.mograsim.logic.core.types.BitVector;
15 import net.mograsim.logic.core.types.BitVectorFormatter;
16 import net.mograsim.logic.model.model.ViewModelModifiable;
17 import net.mograsim.logic.model.model.components.GUIComponent;
18 import net.mograsim.logic.model.model.wires.Pin;
19 import net.mograsim.logic.model.model.wires.PinUsage;
20 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
21 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
22 import net.mograsim.logic.model.serializing.IdentifierGetter;
23 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
24 import net.mograsim.preferences.Preferences;
26 public class GUIManualSwitch extends GUIComponent
28 private static final double width = 20;
29 private static final double height = 15;
30 private static final double fontHeight = 5;
32 public final int logicWidth;
33 private final Pin outputPin;
35 private final LogicObserver logicObs;
36 private ManualSwitch logicSwitch;
38 public GUIManualSwitch(ViewModelModifiable model, int logicWidth)
40 this(model, logicWidth, null);
43 public GUIManualSwitch(ViewModelModifiable model, int logicWidth, String name)
46 this.logicWidth = logicWidth;
47 logicObs = (i) -> model.requestRedraw();
49 setSize(width, height);
50 addPin(this.outputPin = new Pin(this, "", logicWidth, PinUsage.OUTPUT, width, height / 2));
54 public void render(GeneralGC gc, Rectangle visibleRegion)
56 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
57 if (foreground != null)
58 gc.setForeground(foreground);
59 gc.drawRectangle(getBounds());
60 String label = BitVectorFormatter.formatAsString(logicSwitch == null ? null : logicSwitch.getValues());
61 Font oldFont = gc.getFont();
62 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
63 gc.setFont(labelFont);
64 Point textExtent = gc.textExtent(label);
65 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
66 if (textColor != null)
67 gc.setForeground(textColor);
68 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
72 public void setLogicModelBinding(ManualSwitch logicSwitch)
74 if (this.logicSwitch != null)
75 this.logicSwitch.deregisterObserver(logicObs);
76 this.logicSwitch = logicSwitch;
77 if (logicSwitch != null)
78 logicSwitch.registerObserver(logicObs);
81 public boolean hasLogicModelBinding()
83 return logicSwitch != null;
87 public Object getHighLevelState(String stateID)
92 if (logicSwitch != null)
93 return logicSwitch.getValues();
96 return super.getHighLevelState(stateID);
101 public void setHighLevelState(String stateID, Object newState)
106 if (logicSwitch != null)
107 logicSwitch.setState((BitVector) newState);
110 super.setHighLevelState(stateID, newState);
116 public boolean clicked(double x, double y)
118 if (logicSwitch != null)
119 logicSwitch.toggle();
123 public ManualSwitch getManualSwitch()
128 public Pin getOutputPin()
134 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
136 return new JsonPrimitive(logicWidth);
141 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
142 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(),
143 (m, p, n) -> new GUIManualSwitch(m, p.getAsInt(), n));