1 package net.mograsim.logic.model.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.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.components.GUIComponent;
17 import net.mograsim.logic.model.model.wires.Pin;
18 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
19 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
20 import net.mograsim.logic.model.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)
40 public GUIManualSwitch(ViewModelModifiable model, String name)
43 logicObs = (i) -> model.requestRedraw();
45 setSize(width, height);
46 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
50 public void render(GeneralGC gc, Rectangle visibleRegion)
52 // TODO maybe draw switch state too?
53 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
54 if (foreground != null)
55 gc.setForeground(foreground);
56 gc.drawRectangle(getBounds());
57 String label = BitVectorFormatter.formatValueAsString(end);
58 Font oldFont = gc.getFont();
59 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
60 gc.setFont(labelFont);
61 Point textExtent = gc.textExtent(label);
62 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
63 if (textColor != null)
64 gc.setForeground(textColor);
65 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
69 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
71 deregisterLogicObs(this.end);
72 deregisterLogicObs(this.logicSwitch);
73 this.logicSwitch = logicSwitch;
75 registerLogicObs(end);
76 registerLogicObs(logicSwitch);
79 public boolean hasLogicModelBinding()
81 return logicSwitch != null;
85 public Object getHighLevelState(String stateID)
90 if (logicSwitch != null)
91 return logicSwitch.getValue();
94 return super.getHighLevelState(stateID);
99 public void setHighLevelState(String stateID, Object newState)
104 if (logicSwitch != null)
105 logicSwitch.setToValueOf((Bit) newState);
108 super.setHighLevelState(stateID, newState);
113 private void registerLogicObs(LogicObservable observable)
115 if (observable != null)
116 observable.registerObserver(logicObs);
119 private void deregisterLogicObs(LogicObservable observable)
121 if (observable != null)
122 observable.deregisterObserver(logicObs);
126 public boolean clicked(double x, double y)
128 if (logicSwitch != null)
129 logicSwitch.toggle();
133 public ManualSwitch getManualSwitch()
138 public Pin getOutputPin()
145 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
146 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(), (m, p, n) -> new GUIManualSwitch(m, n));