Stopped creation of unneccessary Rectangle instances
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUIManualSwitch.java
1 package net.mograsim.logic.ui.model.components;
2
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
15 public class GUIManualSwitch extends GUIComponent
16 {
17         private static final double width = 20;
18         private static final double height = 15;
19         private static final double fontHeight = 5;
20
21         private final Pin outputPin;
22
23         private final LogicObserver logicObs;
24         private ManualSwitch logicSwitch;
25         private ReadEnd end;
26
27         public GUIManualSwitch(ViewModelModifiable model)
28         {
29                 super(model);
30                 logicObs = (i) -> requestRedraw();
31
32                 setSize(width, height);
33                 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
34         }
35
36         @Override
37         public void render(GeneralGC gc, Rectangle visibleRegion)
38         {
39                 // TODO maybe draw switch state too?
40                 gc.drawRectangle(getBounds());
41                 String label = BitVectorFormatter.formatValueAsString(end);
42                 Font oldFont = gc.getFont();
43                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
44                 gc.setFont(labelFont);
45                 Point textExtent = gc.textExtent(label);
46                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
47                 gc.setFont(oldFont);
48         }
49
50         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
51         {
52                 deregisterLogicObs(this.end);
53                 deregisterLogicObs(this.logicSwitch);
54                 this.logicSwitch = logicSwitch;
55                 this.end = end;
56                 registerLogicObs(end);
57                 registerLogicObs(logicSwitch);
58         }
59
60         private void registerLogicObs(LogicObservable observable)
61         {
62                 if (observable != null)
63                         observable.registerObserver(logicObs);
64         }
65
66         private void deregisterLogicObs(LogicObservable observable)
67         {
68                 if (observable != null)
69                         observable.deregisterObserver(logicObs);
70         }
71
72         @Override
73         public boolean clicked(double x, double y)
74         {
75                 if (logicSwitch != null)
76                         logicSwitch.toggle();
77                 return true;
78         }
79
80         public Pin getOutputPin()
81         {
82                 return outputPin;
83         }
84 }