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