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