Refactored MemoryView for extensibility
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUIManualSwitch.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import org.eclipse.swt.graphics.Color;
4
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonPrimitive;
7
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.core.LogicObservable;
13 import net.mograsim.logic.core.LogicObserver;
14 import net.mograsim.logic.core.components.ManualSwitch;
15 import net.mograsim.logic.core.types.BitVector;
16 import net.mograsim.logic.core.types.BitVectorFormatter;
17 import net.mograsim.logic.core.wires.Wire.ReadEnd;
18 import net.mograsim.logic.model.model.ViewModelModifiable;
19 import net.mograsim.logic.model.model.components.GUIComponent;
20 import net.mograsim.logic.model.model.wires.Pin;
21 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
22 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
23 import net.mograsim.logic.model.serializing.IdentifierGetter;
24 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
25 import net.mograsim.preferences.Preferences;
26
27 public class GUIManualSwitch extends GUIComponent
28 {
29         private static final double width = 20;
30         private static final double height = 15;
31         private static final double fontHeight = 5;
32
33         private final Pin outputPin;
34
35         private final LogicObserver logicObs;
36         private ManualSwitch logicSwitch;
37         private ReadEnd end;
38
39         public GUIManualSwitch(ViewModelModifiable model, int logicWidth)
40         {
41                 this(model, logicWidth, null);
42         }
43
44         public GUIManualSwitch(ViewModelModifiable model, int logicWidth, String name)
45         {
46                 super(model, name);
47                 logicObs = (i) -> model.requestRedraw();
48
49                 setSize(width, height);
50                 addPin(this.outputPin = new Pin(this, "", logicWidth, width, height / 2));
51         }
52
53         @Override
54         public void render(GeneralGC gc, Rectangle visibleRegion)
55         {
56                 // TODO maybe draw switch state too?
57                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
58                 if (foreground != null)
59                         gc.setForeground(foreground);
60                 gc.drawRectangle(getBounds());
61                 String label = BitVectorFormatter.formatValueAsString(end);
62                 Font oldFont = gc.getFont();
63                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
64                 gc.setFont(labelFont);
65                 Point textExtent = gc.textExtent(label);
66                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
67                 if (textColor != null)
68                         gc.setForeground(textColor);
69                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
70                 gc.setFont(oldFont);
71         }
72
73         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
74         {
75                 deregisterLogicObs(this.end);
76                 deregisterLogicObs(this.logicSwitch);
77                 this.logicSwitch = logicSwitch;
78                 this.end = end;
79                 registerLogicObs(end);
80                 registerLogicObs(logicSwitch);
81         }
82
83         public boolean hasLogicModelBinding()
84         {
85                 return logicSwitch != null;
86         }
87
88         @Override
89         public Object getHighLevelState(String stateID)
90         {
91                 switch (stateID)
92                 {
93                 case "out":
94                         if (logicSwitch != null)
95                                 return logicSwitch.getValues();
96                         return null;
97                 default:
98                         return super.getHighLevelState(stateID);
99                 }
100         }
101
102         @Override
103         public void setHighLevelState(String stateID, Object newState)
104         {
105                 switch (stateID)
106                 {
107                 case "out":
108                         if (logicSwitch != null)
109                                 logicSwitch.setState((BitVector) newState);
110                         break;
111                 default:
112                         super.setHighLevelState(stateID, newState);
113                         break;
114                 }
115         }
116
117         private void registerLogicObs(LogicObservable observable)
118         {
119                 if (observable != null)
120                         observable.registerObserver(logicObs);
121         }
122
123         private void deregisterLogicObs(LogicObservable observable)
124         {
125                 if (observable != null)
126                         observable.deregisterObserver(logicObs);
127         }
128
129         @Override
130         public boolean clicked(double x, double y)
131         {
132                 if (logicSwitch != null)
133                         logicSwitch.toggle();
134                 return true;
135         }
136
137         public ManualSwitch getManualSwitch()
138         {
139                 return logicSwitch;
140         }
141
142         public Pin getOutputPin()
143         {
144                 return outputPin;
145         }
146
147         @Override
148         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
149         {
150                 return new JsonPrimitive(outputPin.logicWidth);
151         }
152
153         static
154         {
155                 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
156                 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(),
157                                 (m, p, n) -> new GUIManualSwitch(m, p.getAsInt(), n));
158         }
159 }