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 private final Pin outputPin;
35 private final LogicObserver logicObs;
36 private ManualSwitch logicSwitch;
39 public GUIManualSwitch(ViewModelModifiable model, int logicWidth)
41 this(model, logicWidth, null);
44 public GUIManualSwitch(ViewModelModifiable model, int logicWidth, String name)
47 logicObs = (i) -> model.requestRedraw();
49 setSize(width, height);
50 addPin(this.outputPin = new Pin(this, "", logicWidth, width, height / 2));
54 public void render(GeneralGC gc, Rectangle visibleRegion)
56 // TODO maybe draw switch state too?
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.formatValueAsString(end);
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);
73 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
75 deregisterLogicObs(this.end);
76 deregisterLogicObs(this.logicSwitch);
77 this.logicSwitch = logicSwitch;
79 registerLogicObs(end);
80 registerLogicObs(logicSwitch);
83 public boolean hasLogicModelBinding()
85 return logicSwitch != null;
89 public Object getHighLevelState(String stateID)
94 if (logicSwitch != null)
95 return logicSwitch.getValues();
98 return super.getHighLevelState(stateID);
103 public void setHighLevelState(String stateID, Object newState)
108 if (logicSwitch != null)
109 logicSwitch.setState((BitVector) newState);
112 super.setHighLevelState(stateID, newState);
117 private void registerLogicObs(LogicObservable observable)
119 if (observable != null)
120 observable.registerObserver(logicObs);
123 private void deregisterLogicObs(LogicObservable observable)
125 if (observable != null)
126 observable.deregisterObserver(logicObs);
130 public boolean clicked(double x, double y)
132 if (logicSwitch != null)
133 logicSwitch.toggle();
137 public ManualSwitch getManualSwitch()
142 public Pin getOutputPin()
148 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
150 return new JsonPrimitive(outputPin.logicWidth);
155 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
156 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(),
157 (m, p, n) -> new GUIManualSwitch(m, p.getAsInt(), n));