1 package net.mograsim.logic.ui.model.components;
3 import net.haspamelodica.swt.helper.gcs.GeneralGC;
4 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
5 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
7 import net.mograsim.logic.core.LogicObservable;
8 import net.mograsim.logic.core.LogicObserver;
9 import net.mograsim.logic.core.components.ManualSwitch;
10 import net.mograsim.logic.core.types.BitVectorFormatter;
11 import net.mograsim.logic.core.wires.Wire.ReadEnd;
12 import net.mograsim.logic.ui.model.ViewModelModifiable;
13 import net.mograsim.logic.ui.model.wires.Pin;
14 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
15 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
17 public class GUIManualSwitch extends GUIComponent
19 private static final double width = 20;
20 private static final double height = 15;
21 private static final double fontHeight = 5;
23 private final Pin outputPin;
25 private final LogicObserver logicObs;
26 private ManualSwitch logicSwitch;
29 public GUIManualSwitch(ViewModelModifiable model)
32 logicObs = (i) -> requestRedraw();
34 setSize(width, height);
35 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
39 public void render(GeneralGC gc, Rectangle visibleRegion)
41 // TODO maybe draw switch state too?
42 gc.drawRectangle(getBounds());
43 String label = BitVectorFormatter.formatValueAsString(end);
44 Font oldFont = gc.getFont();
45 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
46 gc.setFont(labelFont);
47 Point textExtent = gc.textExtent(label);
48 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
52 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
54 deregisterLogicObs(this.end);
55 deregisterLogicObs(this.logicSwitch);
56 this.logicSwitch = logicSwitch;
58 registerLogicObs(end);
59 registerLogicObs(logicSwitch);
62 private void registerLogicObs(LogicObservable observable)
64 if (observable != null)
65 observable.registerObserver(logicObs);
68 private void deregisterLogicObs(LogicObservable observable)
70 if (observable != null)
71 observable.deregisterObserver(logicObs);
75 public boolean clicked(double x, double y)
77 if (logicSwitch != null)
82 public ManualSwitch getManualSwitch()
87 public Pin getOutputPin()
94 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());