Renamed logic to core where appropiate
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / ModelManualSwitch.java
1 package net.mograsim.logic.model.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.LogicObserver;
10 import net.mograsim.logic.core.components.CoreManualSwitch;
11 import net.mograsim.logic.core.types.Bit;
12 import net.mograsim.logic.core.types.BitVector;
13 import net.mograsim.logic.core.types.BitVectorFormatter;
14 import net.mograsim.logic.model.model.ViewModelModifiable;
15 import net.mograsim.logic.model.model.components.ModelComponent;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.model.wires.PinUsage;
18 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
19 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
20 import net.mograsim.logic.model.serializing.IdentifyParams;
21 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
22 import net.mograsim.preferences.Preferences;
23
24 public class ModelManualSwitch extends ModelComponent
25 {
26         private static final double width = 20;
27         private static final double height = 15;
28         private static final double fontHeight = 5;
29         private static final double heightMiniButtons = 4; // 0 is disabled
30
31         public final int logicWidth;
32         private final Pin outputPin;
33
34         private final LogicObserver logicObs;
35         private CoreManualSwitch manualSwitch;
36
37         public ModelManualSwitch(ViewModelModifiable model, int logicWidth)
38         {
39                 this(model, logicWidth, null);
40         }
41
42         public ModelManualSwitch(ViewModelModifiable model, int logicWidth, String name)
43         {
44                 super(model, name);
45                 this.logicWidth = logicWidth;
46                 logicObs = (i) -> model.requestRedraw();
47
48                 setSize(width, height);
49                 addPin(this.outputPin = new Pin(this, "", logicWidth, PinUsage.OUTPUT, width, height / 2));
50         }
51
52         @Override
53         public void render(GeneralGC gc, Rectangle visibleRegion)
54         {
55                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
56                 if (foreground != null)
57                         gc.setForeground(foreground);
58                 gc.drawRectangle(getBounds());
59                 String label = BitVectorFormatter.formatAsString(manualSwitch == null ? null : manualSwitch.getValues());
60                 Font oldFont = gc.getFont();
61                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
62                 gc.setFont(labelFont);
63                 Point textExtent = gc.textExtent(label);
64                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
65                 if (textColor != null)
66                         gc.setForeground(textColor);
67                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
68                 gc.setFont(oldFont);
69
70                 if (manualSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons)
71                 {
72                         double x = getPosX();
73                         double y = getPosY();
74                         gc.drawLine(x, y + heightMiniButtons, x + width, y + heightMiniButtons);
75                         Color c = gc.getBackground();
76                         gc.setBackground(gc.getForeground());
77                         BitVector bv = manualSwitch.getValues();
78                         double part = width / bv.length();
79                         for (int i = 0; i < bv.length(); i++)
80                         {
81                                 double start = x + part * i;
82                                 if (i != 0)
83                                         gc.drawLine(start, y, start, y + heightMiniButtons);
84                                 if (bv.getMSBit(i) == Bit.ONE)
85                                 {
86 //                                      gc.fillRectangle(start, y, part, heightMiniButtons); // alternative, but not always visible what Bit is where 
87                                         gc.drawLine(start, y, start + part, y + heightMiniButtons);
88                                         gc.drawLine(start + part, y, start, y + heightMiniButtons);
89                                 }
90                         }
91                         gc.setBackground(c);
92                 }
93         }
94
95         public void setCoreModelBinding(CoreManualSwitch logicSwitch)
96         {
97                 if (this.manualSwitch != null)
98                         this.manualSwitch.deregisterObserver(logicObs);
99                 this.manualSwitch = logicSwitch;
100                 if (logicSwitch != null)
101                         logicSwitch.registerObserver(logicObs);
102         }
103
104         public boolean hasCoreModelBinding()
105         {
106                 return manualSwitch != null;
107         }
108
109         @Override
110         public Object getHighLevelState(String stateID)
111         {
112                 switch (stateID)
113                 {
114                 case "out":
115                         if (manualSwitch != null)
116                                 return manualSwitch.getValues();
117                         return null;
118                 default:
119                         return super.getHighLevelState(stateID);
120                 }
121         }
122
123         @Override
124         public void setHighLevelState(String stateID, Object newState)
125         {
126                 switch (stateID)
127                 {
128                 case "out":
129                         if (manualSwitch != null)
130                                 manualSwitch.setState((BitVector) newState);
131                         break;
132                 default:
133                         super.setHighLevelState(stateID, newState);
134                         break;
135                 }
136         }
137
138         @Override
139         public boolean clicked(double x, double y)
140         {
141                 if (manualSwitch != null)
142                 {
143                         if (heightMiniButtons > 0 && y - getPosY() < heightMiniButtons)
144                         {
145                                 int part = (int) ((x - getPosX()) * logicWidth / width);
146                                 manualSwitch.setState(manualSwitch.getValues().withBitChanged(part, Bit::not));
147                         } else
148                         {
149                                 manualSwitch.toggle();
150                         }
151                 }
152                 return true;
153         }
154
155         public CoreManualSwitch getManualSwitch()
156         {
157                 return manualSwitch;
158         }
159
160         public Pin getOutputPin()
161         {
162                 return outputPin;
163         }
164
165         @Override
166         public String getIDForSerializing(IdentifyParams idParams)
167         {
168                 return "ManualSwitch";
169         }
170
171         @Override
172         public Integer getParamsForSerializing(IdentifyParams idParams)
173         {
174                 return logicWidth;
175         }
176
177         static
178         {
179                 LogicCoreAdapter.addComponentAdapter(new ManualSwitchAdapter());
180                 IndirectModelComponentCreator.setComponentSupplier(ModelManualSwitch.class.getName(),
181                                 (m, p, n) -> new ModelManualSwitch(m, p.getAsInt(), n));
182         }
183 }