1 package net.mograsim.logic.ui.model.components;
\r
3 import net.mograsim.logic.ui.model.ViewModel;
\r
4 import net.mograsim.logic.ui.model.wires.Pin;
\r
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
\r
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
\r
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
\r
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
\r
9 import net.mograsim.logic.core.LogicObservable;
\r
10 import net.mograsim.logic.core.LogicObserver;
\r
11 import net.mograsim.logic.core.components.ManualSwitch;
\r
12 import net.mograsim.logic.core.types.BitVectorFormatter;
\r
13 import net.mograsim.logic.core.wires.Wire.ReadEnd;
\r
15 public class GUIManualSwitch extends GUIComponent
\r
17 private static final double width = 20;
\r
18 private static final double height = 15;
\r
19 private static final double fontHeight = 5;
\r
21 private final Pin outputPin;
\r
23 private final LogicObserver logicObs;
\r
24 private ManualSwitch logicSwitch;
\r
25 private ReadEnd end;
\r
27 public GUIManualSwitch(ViewModel model)
\r
30 logicObs = (i) -> requestRedraw();
\r
32 setSize(width, height);
\r
33 addPin(this.outputPin = new Pin(this, 1, width, height / 2));
\r
37 public void render(GeneralGC gc, Rectangle visibleRegion)
\r
39 double posX = getBounds().x;
\r
40 double posY = getBounds().y;
\r
42 // TODO maybe draw switch state too?
\r
43 gc.drawRectangle(posX, posY, width, height);
\r
44 String label = BitVectorFormatter.formatValueAsString(end);
\r
45 Font oldFont = gc.getFont();
\r
46 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
\r
47 gc.setFont(labelFont);
\r
48 Point textExtent = gc.textExtent(label);
\r
49 gc.drawText(label, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
\r
50 gc.setFont(oldFont);
\r
53 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
\r
55 deregisterLogicObs(this.end);
\r
56 deregisterLogicObs(this.logicSwitch);
\r
57 this.logicSwitch = logicSwitch;
\r
59 registerLogicObs(end);
\r
60 registerLogicObs(logicSwitch);
\r
63 private void registerLogicObs(LogicObservable observable)
\r
65 if (observable != null)
\r
66 observable.registerObserver(logicObs);
\r
69 private void deregisterLogicObs(LogicObservable observable)
\r
71 if (observable != null)
\r
72 observable.deregisterObserver(logicObs);
\r
76 public boolean clicked(double x, double y)
\r
78 if (logicSwitch != null)
\r
79 logicSwitch.toggle();
\r
83 public Pin getOutputPin()
\r