1 package net.mograsim.logic.model.model.components.atomic;
3 import static net.mograsim.logic.model.preferences.RenderPreferences.FOREGROUND_COLOR;
4 import static net.mograsim.logic.model.preferences.RenderPreferences.TEXT_COLOR;
6 import java.util.ArrayList;
8 import java.util.function.Consumer;
10 import org.eclipse.swt.graphics.Color;
12 import com.google.gson.JsonSyntaxException;
14 import net.haspamelodica.swt.helper.gcs.GeneralGC;
15 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
16 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
17 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
18 import net.mograsim.logic.core.LogicObserver;
19 import net.mograsim.logic.core.components.CoreClock;
20 import net.mograsim.logic.core.types.BitVector;
21 import net.mograsim.logic.model.model.LogicModelModifiable;
22 import net.mograsim.logic.model.model.components.ModelComponent;
23 import net.mograsim.logic.model.model.components.Orientation;
24 import net.mograsim.logic.model.model.components.OrientationCalculator;
25 import net.mograsim.logic.model.model.wires.Pin;
26 import net.mograsim.logic.model.model.wires.PinUsage;
27 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
28 import net.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
29 import net.mograsim.logic.model.preferences.RenderPreferences;
30 import net.mograsim.logic.model.serializing.IdentifyParams;
31 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
32 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
33 import net.mograsim.logic.model.util.JsonHandler;
35 public class ModelClock extends ModelComponent
37 private static final double width = 20;
38 private static final double height = 20;
39 private static final double fontHeight = 5;
41 private final Pin outputPin;
43 private final LogicObserver logicObs;
44 private ModelClockParams params;
45 private OrientationCalculator oc;
46 private CoreClock clock;
48 private final List<Consumer<Object>> hlsListeners;
50 public ModelClock(LogicModelModifiable model, ModelClockParams params)
52 this(model, params, null);
55 public ModelClock(LogicModelModifiable model, ModelClockParams params, String name)
57 super(model, name, false);
60 oc = new OrientationCalculator(params.orientation, width, height);
61 setSize(oc.width(), oc.height());
63 this.outputPin = new Pin(model, this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
66 this.hlsListeners = new ArrayList<>();
70 model.requestRedraw();
71 BitVector v = getOutValues();
72 hlsListeners.forEach(l -> l.accept(v));
75 setHighLevelStateHandler(new HighLevelStateHandler()
78 public Object get(String stateID)
84 return getOutValues();
87 throw new IllegalArgumentException("No high level state with ID " + stateID);
92 public void set(String stateID, Object newState)
97 throw new UnsupportedOperationException("cannot set state of clock");
99 throw new IllegalArgumentException("No high level state with ID " + stateID);
104 public void addListener(String stateID, Consumer<Object> stateChanged)
109 hlsListeners.add(stateChanged);
112 throw new IllegalArgumentException("No high level state with ID " + stateID);
117 public void removeListener(String stateID, java.util.function.Consumer<Object> stateChanged)
122 hlsListeners.remove(stateChanged);
125 throw new IllegalArgumentException("No high level state with ID " + stateID);
130 public String getIDForSerializing(IdentifyParams idParams)
136 public Object getParamsForSerializing(IdentifyParams idParams)
146 public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
148 Color foreground = renderPrefs.getColor(FOREGROUND_COLOR);
149 if (foreground != null)
150 gc.setForeground(foreground);
151 gc.drawRectangle(getBounds());
152 String label = clock == null ? "null" : (clock.isOn() ? "|" : "\u2015");
153 Font oldFont = gc.getFont();
154 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
155 gc.setFont(labelFont);
156 Point textExtent = gc.textExtent(label);
157 Color textColor = renderPrefs.getColor(TEXT_COLOR);
158 if (textColor != null)
159 gc.setForeground(textColor);
160 gc.drawText(label, getPosX() + (oc.width() - textExtent.x) / 2, getPosY() + (oc.height() - textExtent.y) / 2, true);
164 public void setCoreModelBinding(CoreClock clock)
166 if (this.clock != null)
167 this.clock.deregisterObserver(logicObs);
170 clock.registerObserver(logicObs);
173 public boolean hasCoreModelBinding()
175 return clock != null;
179 public CoreClock getClock()
184 public Pin getOutputPin()
189 public int getDelta()
195 public String getIDForSerializing(IdentifyParams idParams)
201 public ModelClockParams getParamsForSerializing(IdentifyParams idParams)
206 private BitVector getOutValues()
208 return clock.getOutValues();
213 LogicCoreAdapter.addComponentAdapter(new ClockAdapter());
214 IndirectModelComponentCreator.setComponentSupplier(ModelClock.class.getName(), (m, p, n) ->
216 ModelClockParams params = JsonHandler.fromJsonTree(p, ModelClockParams.class);
218 throw new JsonSyntaxException("Invalid!!!");
219 return new ModelClock(m, params, n);
223 public static class ModelClockParams
226 Orientation orientation;
228 public ModelClockParams(int delta, Orientation orientation)
231 this.orientation = orientation;