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;
15 public class GUIManualSwitch extends GUIComponent
17 private static final double width = 20;
18 private static final double height = 15;
19 private static final double fontHeight = 5;
21 private final Pin outputPin;
23 private final LogicObserver logicObs;
24 private ManualSwitch logicSwitch;
27 public GUIManualSwitch(ViewModelModifiable model)
30 logicObs = (i) -> requestRedraw();
32 setSize(width, height);
33 addPin(this.outputPin = new Pin(this, 1, width, height / 2));
37 public void render(GeneralGC gc, Rectangle visibleRegion)
39 double posX = getBounds().x;
40 double posY = getBounds().y;
42 // TODO maybe draw switch state too?
43 gc.drawRectangle(posX, posY, width, height);
44 String label = BitVectorFormatter.formatValueAsString(end);
45 Font oldFont = gc.getFont();
46 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
47 gc.setFont(labelFont);
48 Point textExtent = gc.textExtent(label);
49 gc.drawText(label, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
53 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
55 deregisterLogicObs(this.end);
56 deregisterLogicObs(this.logicSwitch);
57 this.logicSwitch = logicSwitch;
59 registerLogicObs(end);
60 registerLogicObs(logicSwitch);
63 private void registerLogicObs(LogicObservable observable)
65 if (observable != null)
66 observable.registerObserver(logicObs);
69 private void deregisterLogicObs(LogicObservable observable)
71 if (observable != null)
72 observable.deregisterObserver(logicObs);
76 public boolean clicked(double x, double y)
78 if (logicSwitch != null)
83 public Pin getOutputPin()