Made get/setHLS final to force components to set a HLSHandler
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / ModelClock.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import org.eclipse.swt.graphics.Color;
4
5 import com.google.gson.JsonSyntaxException;
6
7 import net.haspamelodica.swt.helper.gcs.GeneralGC;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.core.LogicObserver;
12 import net.mograsim.logic.core.components.CoreClock;
13 import net.mograsim.logic.model.model.LogicModelModifiable;
14 import net.mograsim.logic.model.model.components.ModelComponent;
15 import net.mograsim.logic.model.model.components.Orientation;
16 import net.mograsim.logic.model.model.components.OrientationCalculator;
17 import net.mograsim.logic.model.model.wires.Pin;
18 import net.mograsim.logic.model.model.wires.PinUsage;
19 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
20 import net.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
21 import net.mograsim.logic.model.serializing.IdentifyParams;
22 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
23 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
24 import net.mograsim.logic.model.util.JsonHandler;
25 import net.mograsim.preferences.Preferences;
26
27 public class ModelClock extends ModelComponent
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 ModelClockParams params;
37         private OrientationCalculator oc;
38         private CoreClock clock;
39
40         public ModelClock(LogicModelModifiable model, ModelClockParams params)
41         {
42                 this(model, params, null);
43         }
44
45         public ModelClock(LogicModelModifiable model, ModelClockParams params, String name)
46         {
47                 super(model, name, false);
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(model, this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
55                 addPin(outputPin);
56
57                 setHighLevelStateHandler(new HighLevelStateHandler()
58                 {
59                         @Override
60                         public Object getHighLevelState(String stateID)
61                         {
62                                 switch (stateID)
63                                 {
64                                 case "out":
65                                         if (clock != null)
66                                                 return clock.getOut().getInputValues();
67                                         return null;
68                                 default:
69                                         throw new IllegalArgumentException("No high level state with ID " + stateID);
70                                 }
71                         }
72
73                         @Override
74                         public void setHighLevelState(String stateID, Object newState)
75                         {
76                                 switch (stateID)
77                                 {
78                                 case "out":
79                                         throw new UnsupportedOperationException("cannot set state of clock");
80                                 default:
81                                         throw new IllegalArgumentException("No high level state with ID " + stateID);
82                                 }
83                         }
84
85                         @Override
86                         public String getIDForSerializing(IdentifyParams idParams)
87                         {
88                                 return null;
89                         }
90
91                         @Override
92                         public Object getParamsForSerializing(IdentifyParams idParams)
93                         {
94                                 return null;
95                         }
96                 });
97
98                 init();
99         }
100
101         @Override
102         public void render(GeneralGC gc, Rectangle visibleRegion)
103         {
104                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
105                 if (foreground != null)
106                         gc.setForeground(foreground);
107                 gc.drawRectangle(getBounds());
108                 String label = clock == null ? "null" : (clock.isOn() ? "|" : "\u2015");
109                 Font oldFont = gc.getFont();
110                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
111                 gc.setFont(labelFont);
112                 Point textExtent = gc.textExtent(label);
113                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
114                 if (textColor != null)
115                         gc.setForeground(textColor);
116                 gc.drawText(label, getPosX() + (oc.width() - textExtent.x) / 2, getPosY() + (oc.height() - textExtent.y) / 2, true);
117                 gc.setFont(oldFont);
118         }
119
120         public void setCoreModelBinding(CoreClock clock)
121         {
122                 if (this.clock != null)
123                         this.clock.deregisterObserver(logicObs);
124                 this.clock = clock;
125                 if (clock != null)
126                         clock.registerObserver(logicObs);
127         }
128
129         public boolean hasCoreModelBinding()
130         {
131                 return clock != null;
132         }
133
134         public CoreClock getClock()
135         {
136                 return clock;
137         }
138
139         public Pin getOutputPin()
140         {
141                 return outputPin;
142         }
143
144         public int getDelta()
145         {
146                 return params.delta;
147         }
148
149         @Override
150         public String getIDForSerializing(IdentifyParams idParams)
151         {
152                 return "Clock";
153         }
154
155         @Override
156         public ModelClockParams getParamsForSerializing(IdentifyParams idParams)
157         {
158                 return params;
159         }
160
161         static
162         {
163                 LogicCoreAdapter.addComponentAdapter(new ClockAdapter());
164                 IndirectModelComponentCreator.setComponentSupplier(ModelClock.class.getName(), (m, p, n) ->
165                 {
166                         ModelClockParams params = JsonHandler.fromJsonTree(p, ModelClockParams.class);
167                         if (params == null)
168                                 throw new JsonSyntaxException("Invalid!!!");
169                         return new ModelClock(m, params, n);
170                 });
171         }
172
173         public static class ModelClockParams
174         {
175                 int delta;
176                 Orientation orientation;
177
178                 public ModelClockParams(int delta, Orientation orientation)
179                 {
180                         this.delta = delta;
181                         this.orientation = orientation;
182                 }
183         }
184 }