be4c3272fa35ba31e48c271ef55eaf325a128420
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUIManualSwitch.java
1 package net.mograsim.logic.ui.model.components;
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.LogicObservable;
10 import net.mograsim.logic.core.LogicObserver;
11 import net.mograsim.logic.core.components.ManualSwitch;
12 import net.mograsim.logic.core.types.BitVectorFormatter;
13 import net.mograsim.logic.core.wires.Wire.ReadEnd;
14 import net.mograsim.logic.ui.model.ViewModelModifiable;
15 import net.mograsim.logic.ui.model.wires.Pin;
16 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
17 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
18 import net.mograsim.preferences.Preferences;
19
20 public class GUIManualSwitch extends GUIComponent
21 {
22         private static final double width = 20;
23         private static final double height = 15;
24         private static final double fontHeight = 5;
25
26         private final Pin outputPin;
27
28         private final LogicObserver logicObs;
29         private ManualSwitch logicSwitch;
30         private ReadEnd end;
31
32         public GUIManualSwitch(ViewModelModifiable model)
33         {
34                 super(model);
35                 logicObs = (i) -> requestRedraw();
36
37                 setSize(width, height);
38                 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
39         }
40
41         @Override
42         public void render(GeneralGC gc, Rectangle visibleRegion)
43         {
44                 // TODO maybe draw switch state too?
45                 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
46                 if (foreground != null)
47                         gc.setForeground(foreground);
48                 gc.drawRectangle(getBounds());
49                 String label = BitVectorFormatter.formatValueAsString(end);
50                 Font oldFont = gc.getFont();
51                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
52                 gc.setFont(labelFont);
53                 Point textExtent = gc.textExtent(label);
54                 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
55                 if (textColor != null)
56                         gc.setForeground(textColor);
57                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
58                 gc.setFont(oldFont);
59         }
60
61         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
62         {
63                 deregisterLogicObs(this.end);
64                 deregisterLogicObs(this.logicSwitch);
65                 this.logicSwitch = logicSwitch;
66                 this.end = end;
67                 registerLogicObs(end);
68                 registerLogicObs(logicSwitch);
69         }
70
71         private void registerLogicObs(LogicObservable observable)
72         {
73                 if (observable != null)
74                         observable.registerObserver(logicObs);
75         }
76
77         private void deregisterLogicObs(LogicObservable observable)
78         {
79                 if (observable != null)
80                         observable.deregisterObserver(logicObs);
81         }
82
83         @Override
84         public boolean clicked(double x, double y)
85         {
86                 if (logicSwitch != null)
87                         logicSwitch.toggle();
88                 return true;
89         }
90
91         public ManualSwitch getManualSwitch()
92         {
93                 return logicSwitch;
94         }
95
96         public Pin getOutputPin()
97         {
98                 return outputPin;
99         }
100
101         static
102         {
103                 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
104         }
105 }