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.components.ManualSwitch;
\r
10 import net.mograsim.logic.core.types.BitVectorFormatter;
\r
11 import net.mograsim.logic.core.wires.Wire.ReadEnd;
\r
13 public class GUIManualSwitch extends GUIComponent
\r
15 private static final double width = 20;
\r
16 private static final double height = 15;
\r
17 private static final double fontHeight = 5;
\r
19 private final Pin outputPin;
\r
21 private ManualSwitch logicSwitch;
\r
22 private ReadEnd end;
\r
24 public GUIManualSwitch(ViewModel model)
\r
27 setSize(width, height);
\r
28 addPin(this.outputPin = new Pin(this, 1, width, height / 2));
\r
32 public void render(GeneralGC gc, Rectangle visibleRegion)
\r
34 double posX = getBounds().x;
\r
35 double posY = getBounds().y;
\r
37 gc.drawRectangle(posX, posY, width, height);
\r
38 String label = BitVectorFormatter.formatValueAsString(end);
\r
39 Font oldFont = gc.getFont();
\r
40 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
\r
41 gc.setFont(labelFont);
\r
42 Point textExtent = gc.textExtent(label);
\r
43 gc.drawText(label, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
\r
44 gc.setFont(oldFont);
\r
47 public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
\r
49 this.logicSwitch = logicSwitch;
\r
51 // TODO when ManualSwitch supports it, add listeners
\r
52 end.registerObserver((i) -> callComponentLookChangedListeners());
\r
56 public boolean clicked(double x, double y)
\r
58 if (logicSwitch != null)
\r
59 logicSwitch.toggle();
\r
63 public Pin getOutputPin()
\r