The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / 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.LogicModelModifiable;
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(LogicModelModifiable model, int logicWidth)
38         {
39                 this(model, logicWidth, null);
40         }
41
42         public ModelManualSwitch(LogicModelModifiable model, int logicWidth, String name)
43         {
44                 super(model, name, false);
45                 this.logicWidth = logicWidth;
46                 logicObs = (i) -> model.requestRedraw();
47
48                 setSize(width, height);
49                 addPin(this.outputPin = new Pin(model, this, "", logicWidth, PinUsage.OUTPUT, width, height / 2));
50
51                 init();
52         }
53
54         @Override
55         public void render(GeneralGC gc, Rectangle visibleRegion)
56         {
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.formatAsString(manualSwitch == null ? null : manualSwitch.getValues());
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                 if (manualSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons)
73                 {
74                         double x = getPosX();
75                         double y = getPosY();
76                         gc.drawLine(x, y + heightMiniButtons, x + width, y + heightMiniButtons);
77                         Color c = gc.getBackground();
78                         gc.setBackground(gc.getForeground());
79                         BitVector bv = manualSwitch.getValues();
80                         double part = width / bv.length();
81                         for (int i = 0; i < bv.length(); i++)
82                         {
83                                 double start = x + part * i;
84                                 if (i != 0)
85                                         gc.drawLine(start, y, start, y + heightMiniButtons);
86                                 if (bv.getMSBit(i) == Bit.ONE)
87                                 {
88 //                                      gc.fillRectangle(start, y, part, heightMiniButtons); // alternative, but not always visible what Bit is where 
89                                         gc.drawLine(start, y, start + part, y + heightMiniButtons);
90                                         gc.drawLine(start + part, y, start, y + heightMiniButtons);
91                                 }
92                         }
93                         gc.setBackground(c);
94                 }
95         }
96
97         public void setCoreModelBinding(CoreManualSwitch logicSwitch)
98         {
99                 if (this.manualSwitch != null)
100                         this.manualSwitch.deregisterObserver(logicObs);
101                 this.manualSwitch = logicSwitch;
102                 if (logicSwitch != null)
103                         logicSwitch.registerObserver(logicObs);
104         }
105
106         public boolean hasCoreModelBinding()
107         {
108                 return manualSwitch != null;
109         }
110
111         @Override
112         public Object getHighLevelState(String stateID)
113         {
114                 switch (stateID)
115                 {
116                 case "out":
117                         if (manualSwitch != null)
118                                 return manualSwitch.getValues();
119                         return null;
120                 default:
121                         return super.getHighLevelState(stateID);
122                 }
123         }
124
125         @Override
126         public void setHighLevelState(String stateID, Object newState)
127         {
128                 switch (stateID)
129                 {
130                 case "out":
131                         if (manualSwitch != null)
132                                 manualSwitch.setState((BitVector) newState);
133                         break;
134                 default:
135                         super.setHighLevelState(stateID, newState);
136                         break;
137                 }
138         }
139
140         @Override
141         public boolean clicked(double x, double y)
142         {
143                 if (manualSwitch != null)
144                 {
145                         if (heightMiniButtons > 0 && y - getPosY() < heightMiniButtons)
146                         {
147                                 int part = (int) ((x - getPosX()) * logicWidth / width);
148                                 manualSwitch.setState(manualSwitch.getValues().withBitChanged(part, Bit::not));
149                         } else
150                         {
151                                 manualSwitch.toggle();
152                         }
153                 }
154                 return true;
155         }
156
157         public CoreManualSwitch getManualSwitch()
158         {
159                 return manualSwitch;
160         }
161
162         public Pin getOutputPin()
163         {
164                 return outputPin;
165         }
166
167         @Override
168         public String getIDForSerializing(IdentifyParams idParams)
169         {
170                 return "ManualSwitch";
171         }
172
173         @Override
174         public Integer getParamsForSerializing(IdentifyParams idParams)
175         {
176                 return logicWidth;
177         }
178
179         static
180         {
181                 LogicCoreAdapter.addComponentAdapter(new ManualSwitchAdapter());
182                 IndirectModelComponentCreator.setComponentSupplier(ModelManualSwitch.class.getName(),
183                                 (m, p, n) -> new ModelManualSwitch(m, p.getAsInt(), n));
184         }
185 }