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.BitVectorFormatter;
13 import net.mograsim.logic.core.wires.Wire.ReadEnd;
14 import net.mograsim.logic.ui.model.ViewModelModifiable;
15 import net.mograsim.logic.ui.model.wires.Pin;
16 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
17 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
18 import net.mograsim.preferences.Preferences;
20 public class GUIManualSwitch extends GUIComponent
22 private static final double width = 20;
23 private static final double height = 15;
24 private static final double fontHeight = 5;
26 private final Pin outputPin;
28 private final LogicObserver logicObs;
29 private ManualSwitch logicSwitch;
32 public GUIManualSwitch(ViewModelModifiable model)
35 logicObs = (i) -> requestRedraw();
37 setSize(width, height);
38 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
42 public void render(GeneralGC gc, Rectangle visibleRegion)
44 // TODO maybe draw switch state too?
45 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
46 if (foreground != null)
47 gc.setForeground(foreground);
48 gc.drawRectangle(getBounds());
49 String label = BitVectorFormatter.formatValueAsString(end);
50 Font oldFont = gc.getFont();
51 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
52 gc.setFont(labelFont);
53 Point textExtent = gc.textExtent(label);
54 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
55 if (textColor != null)
56 gc.setForeground(textColor);
57 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
61 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
63 deregisterLogicObs(this.end);
64 deregisterLogicObs(this.logicSwitch);
65 this.logicSwitch = logicSwitch;
67 registerLogicObs(end);
68 registerLogicObs(logicSwitch);
71 private void registerLogicObs(LogicObservable observable)
73 if (observable != null)
74 observable.registerObserver(logicObs);
77 private void deregisterLogicObs(LogicObservable observable)
79 if (observable != null)
80 observable.deregisterObserver(logicObs);
84 public boolean clicked(double x, double y)
86 if (logicSwitch != null)
91 public ManualSwitch getManualSwitch()
96 public Pin getOutputPin()
103 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());