Renamed GUI to Model:
[Mograsim.git] / 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.ViewModelModifiable;
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.ViewLogicModelAdapter;
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.util.JsonHandler;
24 import net.mograsim.preferences.Preferences;
25
26 public class ModelClock extends ModelComponent
27 {
28         private static final double width = 20;
29         private static final double height = 20;
30         private static final double fontHeight = 5;
31
32         private final Pin outputPin;
33
34         private final LogicObserver logicObs;
35         private ModelClockParams params;
36         private OrientationCalculator oc;
37         private CoreClock clock;
38
39         public ModelClock(ViewModelModifiable model, ModelClockParams params)
40         {
41                 this(model, params, null);
42         }
43
44         public ModelClock(ViewModelModifiable model, ModelClockParams params, String name)
45         {
46                 super(model, name);
47                 this.params = params;
48                 logicObs = (i) -> model.requestRedraw();
49
50                 oc = new OrientationCalculator(params.orientation, width, height);
51                 setSize(oc.width(), oc.height());
52
53                 this.outputPin = new Pin(this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
54                 addPin(outputPin);
55         }
56
57         @Override
58         public void render(GeneralGC gc, Rectangle visibleRegion)
59         {
60                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
61                 if (foreground != null)
62                         gc.setForeground(foreground);
63                 gc.drawRectangle(getBounds());
64                 String label = clock == null ? "null" : (clock.isOn() ? "|" : "\u2015");
65                 Font oldFont = gc.getFont();
66                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
67                 gc.setFont(labelFont);
68                 Point textExtent = gc.textExtent(label);
69                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
70                 if (textColor != null)
71                         gc.setForeground(textColor);
72                 gc.drawText(label, getPosX() + (oc.width() - textExtent.x) / 2, getPosY() + (oc.height() - textExtent.y) / 2, true);
73                 gc.setFont(oldFont);
74         }
75
76         public void setLogicModelBinding(CoreClock clock)
77         {
78                 if (this.clock != null)
79                         this.clock.deregisterObserver(logicObs);
80                 this.clock = clock;
81                 if (clock != null)
82                         clock.registerObserver(logicObs);
83         }
84
85         public boolean hasLogicModelBinding()
86         {
87                 return clock != null;
88         }
89
90         @Override
91         public Object getHighLevelState(String stateID)
92         {
93                 switch (stateID)
94                 {
95                 case "out":
96                         if (clock != null)
97                                 return clock.getOut().getInputValues();
98                         return null;
99                 default:
100                         return super.getHighLevelState(stateID);
101                 }
102         }
103
104         @Override
105         public void setHighLevelState(String stateID, Object newState)
106         {
107                 switch (stateID)
108                 {
109                 case "out":
110                         throw new UnsupportedOperationException("cannot set state of clock");
111                 default:
112                         super.setHighLevelState(stateID, newState);
113                 }
114         }
115
116         public CoreClock getClock()
117         {
118                 return clock;
119         }
120
121         public Pin getOutputPin()
122         {
123                 return outputPin;
124         }
125
126         public int getDelta()
127         {
128                 return params.delta;
129         }
130
131         @Override
132         public String getIDForSerializing(IdentifyParams idParams)
133         {
134                 return "Clock";
135         }
136
137         @Override
138         public ModelClockParams getParamsForSerializing(IdentifyParams idParams)
139         {
140                 return params;
141         }
142
143         static
144         {
145                 ViewLogicModelAdapter.addComponentAdapter(new ClockAdapter());
146                 IndirectModelComponentCreator.setComponentSupplier(ModelClock.class.getName(), (m, p, n) ->
147                 {
148                         ModelClockParams params = JsonHandler.fromJsonTree(p, ModelClockParams.class);
149                         if (params == null)
150                                 throw new JsonSyntaxException("Invalid!!!");
151                         return new ModelClock(m, params, n);
152                 });
153         }
154
155         public static class ModelClockParams
156         {
157                 int delta;
158                 Orientation orientation;
159
160                 public ModelClockParams(int delta, Orientation orientation)
161                 {
162                         this.delta = delta;
163                         this.orientation = orientation;
164                 }
165         }
166 }