Created model for the logic Clock
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUIClock.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import org.eclipse.swt.graphics.Color;
4
5 import com.google.gson.Gson;
6 import com.google.gson.JsonElement;
7 import com.google.gson.JsonSyntaxException;
8
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.core.LogicObserver;
14 import net.mograsim.logic.core.components.Clock;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.components.GUIComponent;
17 import net.mograsim.logic.model.model.components.Orientation;
18 import net.mograsim.logic.model.model.components.OrientationCalculator;
19 import net.mograsim.logic.model.model.wires.Pin;
20 import net.mograsim.logic.model.model.wires.PinUsage;
21 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
22 import net.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
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 GUIClock extends GUIComponent
28 {
29         private static final double width = 20;
30         private static final double height = 20;
31         private static final double fontHeight = 5;
32
33         private final Pin outputPin;
34
35         private final LogicObserver logicObs;
36         private GUIClockParams params;
37         private OrientationCalculator oc;
38         private Clock clock;
39
40         public GUIClock(ViewModelModifiable model, GUIClockParams params)
41         {
42                 this(model, params, null);
43         }
44
45         public GUIClock(ViewModelModifiable model, GUIClockParams params, String name)
46         {
47                 super(model, name);
48                 this.params = params;
49                 logicObs = (i) -> model.requestRedraw();
50
51                 oc = new OrientationCalculator(params.orientation, width, height);
52                 setSize(oc.width(), oc.height());
53
54                 this.outputPin = new Pin(this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
55                 addPin(outputPin);
56         }
57
58         @Override
59         public void render(GeneralGC gc, Rectangle visibleRegion)
60         {
61                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
62                 if (foreground != null)
63                         gc.setForeground(foreground);
64                 gc.drawRectangle(getBounds());
65                 String label = clock == null ? "null" : (clock.isOn() ? "|" : "\u2015");
66                 Font oldFont = gc.getFont();
67                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
68                 gc.setFont(labelFont);
69                 Point textExtent = gc.textExtent(label);
70                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
71                 if (textColor != null)
72                         gc.setForeground(textColor);
73                 gc.drawText(label, getPosX() + (oc.width() - textExtent.x) / 2, getPosY() + (oc.height() - textExtent.y) / 2, true);
74                 gc.setFont(oldFont);
75         }
76
77         public void setLogicModelBinding(Clock clock)
78         {
79                 if (this.clock != null)
80                         this.clock.deregisterObserver(logicObs);
81                 this.clock = clock;
82                 if (clock != null)
83                         clock.registerObserver(logicObs);
84         }
85
86         public boolean hasLogicModelBinding()
87         {
88                 return clock != null;
89         }
90
91         @Override
92         public Object getHighLevelState(String stateID)
93         {
94                 switch (stateID)
95                 {
96                 case "out":
97                         if (clock != null)
98                                 return clock.getOut().getInputValues();
99                         return null;
100                 default:
101                         return super.getHighLevelState(stateID);
102                 }
103         }
104
105         @Override
106         public void setHighLevelState(String stateID, Object newState)
107         {
108                 switch (stateID)
109                 {
110                 case "out":
111                         throw new UnsupportedOperationException("cannot set state of clock");
112                 default:
113                         super.setHighLevelState(stateID, newState);
114                 }
115         }
116
117         public Clock getClock()
118         {
119                 return clock;
120         }
121
122         public Pin getOutputPin()
123         {
124                 return outputPin;
125         }
126
127         public int getDelta()
128         {
129                 return params.delta;
130         }
131
132         @Override
133         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
134         {
135                 return new Gson().toJsonTree(params);
136         }
137
138         static
139         {
140                 ViewLogicModelAdapter.addComponentAdapter(new ClockAdapter());
141                 IndirectGUIComponentCreator.setComponentSupplier(GUIClock.class.getName(), (m, p, n) ->
142                 {
143                         GUIClockParams params = new Gson().fromJson(p, GUIClockParams.class);
144                         if (params == null)
145                                 throw new JsonSyntaxException("Invalid!!!");
146                         return new GUIClock(m, params, n);
147                 });
148         }
149
150         public static class GUIClockParams
151         {
152                 int delta;
153                 Orientation orientation;
154
155                 public GUIClockParams(int delta, Orientation orientation)
156                 {
157                         this.delta = delta;
158                         this.orientation = orientation;
159                 }
160         }
161 }