1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import com.google.gson.JsonSyntaxException;
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.util.JsonHandler;
24 import net.mograsim.preferences.Preferences;
26 public class ModelClock extends ModelComponent
28 private static final double width = 20;
29 private static final double height = 20;
30 private static final double fontHeight = 5;
32 private final Pin outputPin;
34 private final LogicObserver logicObs;
35 private ModelClockParams params;
36 private OrientationCalculator oc;
37 private CoreClock clock;
39 public ModelClock(LogicModelModifiable model, ModelClockParams params)
41 this(model, params, null);
44 public ModelClock(LogicModelModifiable model, ModelClockParams params, String name)
46 super(model, name, false);
48 logicObs = (i) -> model.requestRedraw();
50 oc = new OrientationCalculator(params.orientation, width, height);
51 setSize(oc.width(), oc.height());
53 this.outputPin = new Pin(model, this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
60 public void render(GeneralGC gc, Rectangle visibleRegion)
62 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
63 if (foreground != null)
64 gc.setForeground(foreground);
65 gc.drawRectangle(getBounds());
66 String label = clock == null ? "null" : (clock.isOn() ? "|" : "\u2015");
67 Font oldFont = gc.getFont();
68 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
69 gc.setFont(labelFont);
70 Point textExtent = gc.textExtent(label);
71 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
72 if (textColor != null)
73 gc.setForeground(textColor);
74 gc.drawText(label, getPosX() + (oc.width() - textExtent.x) / 2, getPosY() + (oc.height() - textExtent.y) / 2, true);
78 public void setCoreModelBinding(CoreClock clock)
80 if (this.clock != null)
81 this.clock.deregisterObserver(logicObs);
84 clock.registerObserver(logicObs);
87 public boolean hasCoreModelBinding()
93 public Object getHighLevelState(String stateID)
99 return clock.getOut().getInputValues();
102 return super.getHighLevelState(stateID);
107 public void setHighLevelState(String stateID, Object newState)
112 throw new UnsupportedOperationException("cannot set state of clock");
114 super.setHighLevelState(stateID, newState);
118 public CoreClock getClock()
123 public Pin getOutputPin()
128 public int getDelta()
134 public String getIDForSerializing(IdentifyParams idParams)
140 public ModelClockParams getParamsForSerializing(IdentifyParams idParams)
147 LogicCoreAdapter.addComponentAdapter(new ClockAdapter());
148 IndirectModelComponentCreator.setComponentSupplier(ModelClock.class.getName(), (m, p, n) ->
150 ModelClockParams params = JsonHandler.fromJsonTree(p, ModelClockParams.class);
152 throw new JsonSyntaxException("Invalid!!!");
153 return new ModelClock(m, params, n);
157 public static class ModelClockParams
160 Orientation orientation;
162 public ModelClockParams(int delta, Orientation orientation)
165 this.orientation = orientation;