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