Merge branch 'development' of https://gitlab.lrz.de/lrr-tum/students/eragp-misim...
[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         public final int logicWidth;
34         private final Pin outputPin;
35
36         private final LogicObserver logicObs;
37         private ManualSwitch logicSwitch;
38         private ReadEnd end;
39
40         public GUIManualSwitch(ViewModelModifiable model, int logicWidth)
41         {
42                 this(model, logicWidth, null);
43         }
44
45         public GUIManualSwitch(ViewModelModifiable model, int logicWidth, String name)
46         {
47                 super(model, name);
48                 this.logicWidth = logicWidth;
49                 logicObs = (i) -> model.requestRedraw();
50
51                 setSize(width, height);
52                 addPin(this.outputPin = new Pin(this, "", logicWidth, width, height / 2));
53         }
54
55         @Override
56         public void render(GeneralGC gc, Rectangle visibleRegion)
57         {
58                 // TODO maybe draw switch state too?
59                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
60                 if (foreground != null)
61                         gc.setForeground(foreground);
62                 gc.drawRectangle(getBounds());
63                 String label = BitVectorFormatter.formatValueAsString(end);
64                 Font oldFont = gc.getFont();
65                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
66                 gc.setFont(labelFont);
67                 Point textExtent = gc.textExtent(label);
68                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
69                 if (textColor != null)
70                         gc.setForeground(textColor);
71                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
72                 gc.setFont(oldFont);
73         }
74
75         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
76         {
77                 deregisterLogicObs(this.end);
78                 deregisterLogicObs(this.logicSwitch);
79                 this.logicSwitch = logicSwitch;
80                 this.end = end;
81                 registerLogicObs(end);
82                 registerLogicObs(logicSwitch);
83         }
84
85         public boolean hasLogicModelBinding()
86         {
87                 return logicSwitch != null;
88         }
89
90         @Override
91         public Object getHighLevelState(String stateID)
92         {
93                 switch (stateID)
94                 {
95                 case "out":
96                         if (logicSwitch != null)
97                                 return logicSwitch.getValues();
98                         return null;
99                 default:
100                         return super.getHighLevelState(stateID);
101                 }
102         }
103
104         @Override
105         public void setHighLevelState(String stateID, Object newState)
106         {
107                 switch (stateID)
108                 {
109                 case "out":
110                         if (logicSwitch != null)
111                                 logicSwitch.setState((BitVector) newState);
112                         break;
113                 default:
114                         super.setHighLevelState(stateID, newState);
115                         break;
116                 }
117         }
118
119         private void registerLogicObs(LogicObservable observable)
120         {
121                 if (observable != null)
122                         observable.registerObserver(logicObs);
123         }
124
125         private void deregisterLogicObs(LogicObservable observable)
126         {
127                 if (observable != null)
128                         observable.deregisterObserver(logicObs);
129         }
130
131         @Override
132         public boolean clicked(double x, double y)
133         {
134                 if (logicSwitch != null)
135                         logicSwitch.toggle();
136                 return true;
137         }
138
139         public ManualSwitch getManualSwitch()
140         {
141                 return logicSwitch;
142         }
143
144         public Pin getOutputPin()
145         {
146                 return outputPin;
147         }
148
149         @Override
150         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
151         {
152                 return new JsonPrimitive(logicWidth);
153         }
154
155         static
156         {
157                 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
158                 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(),
159                                 (m, p, n) -> new GUIManualSwitch(m, p.getAsInt(), n));
160         }
161 }