1 package net.mograsim.logic.ui.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.LogicObservable;
10 import net.mograsim.logic.core.LogicObserver;
11 import net.mograsim.logic.core.components.ManualSwitch;
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.types.BitVectorFormatter;
14 import net.mograsim.logic.core.wires.Wire.ReadEnd;
15 import net.mograsim.logic.ui.model.ViewModelModifiable;
16 import net.mograsim.logic.ui.model.components.GUIComponent;
17 import net.mograsim.logic.ui.model.wires.Pin;
18 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
19 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
20 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
21 import net.mograsim.preferences.Preferences;
23 public class GUIManualSwitch extends GUIComponent
25 private static final double width = 20;
26 private static final double height = 15;
27 private static final double fontHeight = 5;
29 private final Pin outputPin;
31 private final LogicObserver logicObs;
32 private ManualSwitch logicSwitch;
35 public GUIManualSwitch(ViewModelModifiable model)
38 logicObs = (i) -> requestRedraw();
40 setSize(width, height);
41 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
45 public void render(GeneralGC gc, Rectangle visibleRegion)
47 // TODO maybe draw switch state too?
48 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
49 if (foreground != null)
50 gc.setForeground(foreground);
51 gc.drawRectangle(getBounds());
52 String label = BitVectorFormatter.formatValueAsString(end);
53 Font oldFont = gc.getFont();
54 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
55 gc.setFont(labelFont);
56 Point textExtent = gc.textExtent(label);
57 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
58 if (textColor != null)
59 gc.setForeground(textColor);
60 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
64 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
66 deregisterLogicObs(this.end);
67 deregisterLogicObs(this.logicSwitch);
68 this.logicSwitch = logicSwitch;
70 registerLogicObs(end);
71 registerLogicObs(logicSwitch);
74 public boolean hasLogicModelBinding()
76 return logicSwitch != null;
80 public void setHighLevelState(String stateID, Object newState)
85 if (logicSwitch != null)
86 logicSwitch.setToValueOf((Bit) newState);
89 super.setHighLevelState(stateID, newState);
95 public Object getHighLevelState(String stateID)
100 if (logicSwitch != null)
101 return logicSwitch.getValue();
104 return super.getHighLevelState(stateID);
108 private void registerLogicObs(LogicObservable observable)
110 if (observable != null)
111 observable.registerObserver(logicObs);
114 private void deregisterLogicObs(LogicObservable observable)
116 if (observable != null)
117 observable.deregisterObserver(logicObs);
121 public boolean clicked(double x, double y)
123 if (logicSwitch != null)
124 logicSwitch.toggle();
128 public ManualSwitch getManualSwitch()
133 public Pin getOutputPin()
140 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
141 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(), (m, p) -> new GUIManualSwitch(m));