43c719905fc6e4dd3803ae6c8cbf29046e3b6b66
[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 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
15 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
16
17 public class GUIManualSwitch extends GUIComponent
18 {
19         private static final double width = 20;
20         private static final double height = 15;
21         private static final double fontHeight = 5;
22
23         private final Pin outputPin;
24
25         private final LogicObserver logicObs;
26         private ManualSwitch logicSwitch;
27         private ReadEnd end;
28
29         public GUIManualSwitch(ViewModelModifiable model)
30         {
31                 super(model);
32                 logicObs = (i) -> requestRedraw();
33
34                 setSize(width, height);
35                 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
36         }
37
38         @Override
39         public void render(GeneralGC gc, Rectangle visibleRegion)
40         {
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);
49                 gc.setFont(oldFont);
50         }
51
52         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
53         {
54                 deregisterLogicObs(this.end);
55                 deregisterLogicObs(this.logicSwitch);
56                 this.logicSwitch = logicSwitch;
57                 this.end = end;
58                 registerLogicObs(end);
59                 registerLogicObs(logicSwitch);
60         }
61
62         private void registerLogicObs(LogicObservable observable)
63         {
64                 if (observable != null)
65                         observable.registerObserver(logicObs);
66         }
67
68         private void deregisterLogicObs(LogicObservable observable)
69         {
70                 if (observable != null)
71                         observable.deregisterObserver(logicObs);
72         }
73
74         @Override
75         public boolean clicked(double x, double y)
76         {
77                 if (logicSwitch != null)
78                         logicSwitch.toggle();
79                 return true;
80         }
81
82         public ManualSwitch getManualSwitch()
83         {
84                 return logicSwitch;
85         }
86
87         public Pin getOutputPin()
88         {
89                 return outputPin;
90         }
91
92         static
93         {
94                 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
95         }
96 }