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