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.LogicObservable;
13 import net.mograsim.logic.core.LogicObserver;
14 import net.mograsim.logic.core.components.ManualSwitch;
15 import net.mograsim.logic.core.types.BitVector;
16 import net.mograsim.logic.core.types.BitVectorFormatter;
17 import net.mograsim.logic.core.wires.Wire.ReadEnd;
18 import net.mograsim.logic.model.model.ViewModelModifiable;
19 import net.mograsim.logic.model.model.components.GUIComponent;
20 import net.mograsim.logic.model.model.wires.Pin;
21 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
22 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
23 import net.mograsim.logic.model.serializing.IdentifierGetter;
24 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
25 import net.mograsim.preferences.Preferences;
27 public class GUIManualSwitch extends GUIComponent
29 private static final double width = 20;
30 private static final double height = 15;
31 private static final double fontHeight = 5;
33 public final int logicWidth;
34 private final Pin outputPin;
36 private final LogicObserver logicObs;
37 private ManualSwitch logicSwitch;
40 public GUIManualSwitch(ViewModelModifiable model, int logicWidth)
42 this(model, logicWidth, null);
45 public GUIManualSwitch(ViewModelModifiable model, int logicWidth, String name)
48 this.logicWidth = logicWidth;
49 logicObs = (i) -> model.requestRedraw();
51 setSize(width, height);
52 addPin(this.outputPin = new Pin(this, "", logicWidth, width, height / 2));
56 public void render(GeneralGC gc, Rectangle visibleRegion)
58 // TODO maybe draw switch state too?
59 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
60 if (foreground != null)
61 gc.setForeground(foreground);
62 gc.drawRectangle(getBounds());
63 String label = BitVectorFormatter.formatValueAsString(end);
64 Font oldFont = gc.getFont();
65 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
66 gc.setFont(labelFont);
67 Point textExtent = gc.textExtent(label);
68 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
69 if (textColor != null)
70 gc.setForeground(textColor);
71 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
75 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
77 deregisterLogicObs(this.end);
78 deregisterLogicObs(this.logicSwitch);
79 this.logicSwitch = logicSwitch;
81 registerLogicObs(end);
82 registerLogicObs(logicSwitch);
85 public boolean hasLogicModelBinding()
87 return logicSwitch != null;
91 public Object getHighLevelState(String stateID)
96 if (logicSwitch != null)
97 return logicSwitch.getValues();
100 return super.getHighLevelState(stateID);
105 public void setHighLevelState(String stateID, Object newState)
110 if (logicSwitch != null)
111 logicSwitch.setState((BitVector) newState);
114 super.setHighLevelState(stateID, newState);
119 private void registerLogicObs(LogicObservable observable)
121 if (observable != null)
122 observable.registerObserver(logicObs);
125 private void deregisterLogicObs(LogicObservable observable)
127 if (observable != null)
128 observable.deregisterObserver(logicObs);
132 public boolean clicked(double x, double y)
134 if (logicSwitch != null)
135 logicSwitch.toggle();
139 public ManualSwitch getManualSwitch()
144 public Pin getOutputPin()
150 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
152 return new JsonPrimitive(logicWidth);
157 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
158 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(),
159 (m, p, n) -> new GUIManualSwitch(m, p.getAsInt(), n));