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.preferences.Preferences;
22 public class GUIManualSwitch extends GUIComponent
24 private static final double width = 20;
25 private static final double height = 15;
26 private static final double fontHeight = 5;
28 private final Pin outputPin;
30 private final LogicObserver logicObs;
31 private ManualSwitch logicSwitch;
34 public GUIManualSwitch(ViewModelModifiable model)
37 logicObs = (i) -> requestRedraw();
39 setSize(width, height);
40 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
44 public void render(GeneralGC gc, Rectangle visibleRegion)
46 // TODO maybe draw switch state too?
47 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
48 if (foreground != null)
49 gc.setForeground(foreground);
50 gc.drawRectangle(getBounds());
51 String label = BitVectorFormatter.formatValueAsString(end);
52 Font oldFont = gc.getFont();
53 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
54 gc.setFont(labelFont);
55 Point textExtent = gc.textExtent(label);
56 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
57 if (textColor != null)
58 gc.setForeground(textColor);
59 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
63 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
65 deregisterLogicObs(this.end);
66 deregisterLogicObs(this.logicSwitch);
67 this.logicSwitch = logicSwitch;
69 registerLogicObs(end);
70 registerLogicObs(logicSwitch);
73 public boolean hasLogicModelBinding()
75 return logicSwitch != null;
79 public void setHighLevelState(String stateID, Object newState)
84 if (logicSwitch != null)
85 logicSwitch.setToValueOf((Bit) newState);
88 super.setHighLevelState(stateID, newState);
94 public Object getHighLevelState(String stateID)
99 if (logicSwitch != null)
100 return logicSwitch.getValue();
103 return super.getHighLevelState(stateID);
107 private void registerLogicObs(LogicObservable observable)
109 if (observable != null)
110 observable.registerObserver(logicObs);
113 private void deregisterLogicObs(LogicObservable observable)
115 if (observable != null)
116 observable.deregisterObserver(logicObs);
120 public boolean clicked(double x, double y)
122 if (logicSwitch != null)
123 logicSwitch.toggle();
127 public ManualSwitch getManualSwitch()
132 public Pin getOutputPin()
139 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());