77decef247a140bafaf6f0e4baa5833fd81322c3
[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 org.eclipse.swt.graphics.Color;
4
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
9 import net.mograsim.logic.core.LogicObservable;
10 import net.mograsim.logic.core.LogicObserver;
11 import net.mograsim.logic.core.components.ManualSwitch;
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.types.BitVectorFormatter;
14 import net.mograsim.logic.core.wires.Wire.ReadEnd;
15 import net.mograsim.logic.ui.model.ViewModelModifiable;
16 import net.mograsim.logic.ui.model.wires.Pin;
17 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
18 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
19 import net.mograsim.preferences.Preferences;
20
21 public class GUIManualSwitch extends GUIComponent
22 {
23         private static final double width = 20;
24         private static final double height = 15;
25         private static final double fontHeight = 5;
26
27         private final Pin outputPin;
28
29         private final LogicObserver logicObs;
30         private ManualSwitch logicSwitch;
31         private ReadEnd end;
32
33         public GUIManualSwitch(ViewModelModifiable model)
34         {
35                 super(model);
36                 logicObs = (i) -> requestRedraw();
37
38                 setSize(width, height);
39                 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
40         }
41
42         @Override
43         public void render(GeneralGC gc, Rectangle visibleRegion)
44         {
45                 // TODO maybe draw switch state too?
46                 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
47                 if (foreground != null)
48                         gc.setForeground(foreground);
49                 gc.drawRectangle(getBounds());
50                 String label = BitVectorFormatter.formatValueAsString(end);
51                 Font oldFont = gc.getFont();
52                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
53                 gc.setFont(labelFont);
54                 Point textExtent = gc.textExtent(label);
55                 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
56                 if (textColor != null)
57                         gc.setForeground(textColor);
58                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
59                 gc.setFont(oldFont);
60         }
61
62         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
63         {
64                 deregisterLogicObs(this.end);
65                 deregisterLogicObs(this.logicSwitch);
66                 this.logicSwitch = logicSwitch;
67                 this.end = end;
68                 registerLogicObs(end);
69                 registerLogicObs(logicSwitch);
70         }
71
72         public boolean hasLogicModelBinding()
73         {
74                 return logicSwitch != null;
75         }
76
77         @Override
78         public void setHighLevelState(String stateID, Object newState)
79         {
80                 if ("out".equals(stateID))
81                 {
82                         if (logicSwitch != null)
83                                 logicSwitch.setToValueOf((Bit) newState);
84                 } else
85                         super.setHighLevelState(stateID, newState);
86         }
87
88         private void registerLogicObs(LogicObservable observable)
89         {
90                 if (observable != null)
91                         observable.registerObserver(logicObs);
92         }
93
94         private void deregisterLogicObs(LogicObservable observable)
95         {
96                 if (observable != null)
97                         observable.deregisterObserver(logicObs);
98         }
99
100         @Override
101         public boolean clicked(double x, double y)
102         {
103                 if (logicSwitch != null)
104                         logicSwitch.toggle();
105                 return true;
106         }
107
108         public ManualSwitch getManualSwitch()
109         {
110                 return logicSwitch;
111         }
112
113         public Pin getOutputPin()
114         {
115                 return outputPin;
116         }
117
118         static
119         {
120                 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
121         }
122 }