1 package net.mograsim.logic.model.model.components.atomic;
3 import java.util.ArrayList;
5 import java.util.function.Consumer;
7 import org.eclipse.swt.graphics.Color;
9 import com.google.gson.JsonSyntaxException;
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
15 import net.mograsim.logic.core.LogicObserver;
16 import net.mograsim.logic.core.components.CoreClock;
17 import net.mograsim.logic.core.types.BitVector;
18 import net.mograsim.logic.model.model.LogicModelModifiable;
19 import net.mograsim.logic.model.model.components.ModelComponent;
20 import net.mograsim.logic.model.model.components.Orientation;
21 import net.mograsim.logic.model.model.components.OrientationCalculator;
22 import net.mograsim.logic.model.model.wires.Pin;
23 import net.mograsim.logic.model.model.wires.PinUsage;
24 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
25 import net.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
26 import net.mograsim.logic.model.serializing.IdentifyParams;
27 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
28 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
29 import net.mograsim.logic.model.util.JsonHandler;
30 import net.mograsim.preferences.Preferences;
32 public class ModelClock extends ModelComponent
34 private static final double width = 20;
35 private static final double height = 20;
36 private static final double fontHeight = 5;
38 private final Pin outputPin;
40 private final LogicObserver logicObs;
41 private ModelClockParams params;
42 private OrientationCalculator oc;
43 private CoreClock clock;
45 private final List<Consumer<Object>> hlsListeners;
47 public ModelClock(LogicModelModifiable model, ModelClockParams params)
49 this(model, params, null);
52 public ModelClock(LogicModelModifiable model, ModelClockParams params, String name)
54 super(model, name, false);
57 oc = new OrientationCalculator(params.orientation, width, height);
58 setSize(oc.width(), oc.height());
60 this.outputPin = new Pin(model, this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
63 this.hlsListeners = new ArrayList<>();
67 model.requestRedraw();
68 BitVector v = getOutValues();
69 hlsListeners.forEach(l -> l.accept(v));
72 setHighLevelStateHandler(new HighLevelStateHandler()
75 public Object get(String stateID)
81 return getOutValues();
84 throw new IllegalArgumentException("No high level state with ID " + stateID);
89 public void set(String stateID, Object newState)
94 throw new UnsupportedOperationException("cannot set state of clock");
96 throw new IllegalArgumentException("No high level state with ID " + stateID);
101 public void addListener(String stateID, Consumer<Object> stateChanged)
106 hlsListeners.add(stateChanged);
109 throw new IllegalArgumentException("No high level state with ID " + stateID);
114 public void removeListener(String stateID, java.util.function.Consumer<Object> stateChanged)
119 hlsListeners.remove(stateChanged);
122 throw new IllegalArgumentException("No high level state with ID " + stateID);
127 public String getIDForSerializing(IdentifyParams idParams)
133 public Object getParamsForSerializing(IdentifyParams idParams)
143 public void render(GeneralGC gc, Rectangle visibleRegion)
145 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
146 if (foreground != null)
147 gc.setForeground(foreground);
148 gc.drawRectangle(getBounds());
149 String label = clock == null ? "null" : (clock.isOn() ? "|" : "\u2015");
150 Font oldFont = gc.getFont();
151 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
152 gc.setFont(labelFont);
153 Point textExtent = gc.textExtent(label);
154 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
155 if (textColor != null)
156 gc.setForeground(textColor);
157 gc.drawText(label, getPosX() + (oc.width() - textExtent.x) / 2, getPosY() + (oc.height() - textExtent.y) / 2, true);
161 public void setCoreModelBinding(CoreClock clock)
163 if (this.clock != null)
164 this.clock.deregisterObserver(logicObs);
167 clock.registerObserver(logicObs);
170 public boolean hasCoreModelBinding()
172 return clock != null;
176 public CoreClock getClock()
181 public Pin getOutputPin()
186 public int getDelta()
192 public String getIDForSerializing(IdentifyParams idParams)
198 public ModelClockParams getParamsForSerializing(IdentifyParams idParams)
203 private BitVector getOutValues()
205 return clock.getOutValues();
210 LogicCoreAdapter.addComponentAdapter(new ClockAdapter());
211 IndirectModelComponentCreator.setComponentSupplier(ModelClock.class.getName(), (m, p, n) ->
213 ModelClockParams params = JsonHandler.fromJsonTree(p, ModelClockParams.class);
215 throw new JsonSyntaxException("Invalid!!!");
216 return new ModelClock(m, params, n);
220 public static class ModelClockParams
223 Orientation orientation;
225 public ModelClockParams(int delta, Orientation orientation)
228 this.orientation = orientation;