1 package net.mograsim.logic.ui.model.components;
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.wires.Pin;
17 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
18 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
19 import net.mograsim.preferences.Preferences;
21 public class GUIManualSwitch extends GUIComponent
23 private static final double width = 20;
24 private static final double height = 15;
25 private static final double fontHeight = 5;
27 private final Pin outputPin;
29 private final LogicObserver logicObs;
30 private ManualSwitch logicSwitch;
33 public GUIManualSwitch(ViewModelModifiable model)
36 logicObs = (i) -> requestRedraw();
38 setSize(width, height);
39 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
43 public void render(GeneralGC gc, Rectangle visibleRegion)
45 // TODO maybe draw switch state too?
46 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
47 if (foreground != null)
48 gc.setForeground(foreground);
49 gc.drawRectangle(getBounds());
50 String label = BitVectorFormatter.formatValueAsString(end);
51 Font oldFont = gc.getFont();
52 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
53 gc.setFont(labelFont);
54 Point textExtent = gc.textExtent(label);
55 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
56 if (textColor != null)
57 gc.setForeground(textColor);
58 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
62 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
64 deregisterLogicObs(this.end);
65 deregisterLogicObs(this.logicSwitch);
66 this.logicSwitch = logicSwitch;
68 registerLogicObs(end);
69 registerLogicObs(logicSwitch);
72 public boolean hasLogicModelBinding()
74 return logicSwitch != null;
78 public void setHighLevelState(String stateID, Object newState)
83 if (logicSwitch != null)
84 logicSwitch.setToValueOf((Bit) newState);
87 super.setHighLevelState(stateID, newState);
93 public Object getHighLevelState(String stateID)
98 if (logicSwitch != null)
99 return logicSwitch.getValue();
102 return super.getHighLevelState(stateID);
106 private void registerLogicObs(LogicObservable observable)
108 if (observable != null)
109 observable.registerObserver(logicObs);
112 private void deregisterLogicObs(LogicObservable observable)
114 if (observable != null)
115 observable.deregisterObserver(logicObs);
119 public boolean clicked(double x, double y)
121 if (logicSwitch != null)
122 logicSwitch.toggle();
126 public ManualSwitch getManualSwitch()
131 public Pin getOutputPin()
138 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());