1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import com.google.gson.Gson;
6 import com.google.gson.JsonElement;
7 import com.google.gson.JsonSyntaxException;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.core.LogicObserver;
14 import net.mograsim.logic.core.components.Clock;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.components.GUIComponent;
17 import net.mograsim.logic.model.model.components.Orientation;
18 import net.mograsim.logic.model.model.components.OrientationCalculator;
19 import net.mograsim.logic.model.model.wires.Pin;
20 import net.mograsim.logic.model.model.wires.PinUsage;
21 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
22 import net.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
23 import net.mograsim.logic.model.serializing.IdentifierGetter;
24 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
25 import net.mograsim.preferences.Preferences;
27 public class GUIClock extends GUIComponent
29 private static final double width = 20;
30 private static final double height = 20;
31 private static final double fontHeight = 5;
33 private final Pin outputPin;
35 private final LogicObserver logicObs;
36 private GUIClockParams params;
37 private OrientationCalculator oc;
40 public GUIClock(ViewModelModifiable model, GUIClockParams params)
42 this(model, params, null);
45 public GUIClock(ViewModelModifiable model, GUIClockParams params, String name)
49 logicObs = (i) -> model.requestRedraw();
51 oc = new OrientationCalculator(params.orientation, width, height);
52 setSize(oc.width(), oc.height());
54 this.outputPin = new Pin(this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
59 public void render(GeneralGC gc, Rectangle visibleRegion)
61 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
62 if (foreground != null)
63 gc.setForeground(foreground);
64 gc.drawRectangle(getBounds());
65 String label = clock == null ? "null" : (clock.isOn() ? "|" : "\u2015");
66 Font oldFont = gc.getFont();
67 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
68 gc.setFont(labelFont);
69 Point textExtent = gc.textExtent(label);
70 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
71 if (textColor != null)
72 gc.setForeground(textColor);
73 gc.drawText(label, getPosX() + (oc.width() - textExtent.x) / 2, getPosY() + (oc.height() - textExtent.y) / 2, true);
77 public void setLogicModelBinding(Clock clock)
79 if (this.clock != null)
80 this.clock.deregisterObserver(logicObs);
83 clock.registerObserver(logicObs);
86 public boolean hasLogicModelBinding()
92 public Object getHighLevelState(String stateID)
98 return clock.getOut().getInputValues();
101 return super.getHighLevelState(stateID);
106 public void setHighLevelState(String stateID, Object newState)
111 throw new UnsupportedOperationException("cannot set state of clock");
113 super.setHighLevelState(stateID, newState);
117 public Clock getClock()
122 public Pin getOutputPin()
127 public int getDelta()
133 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
135 return new Gson().toJsonTree(params);
140 ViewLogicModelAdapter.addComponentAdapter(new ClockAdapter());
141 IndirectGUIComponentCreator.setComponentSupplier(GUIClock.class.getName(), (m, p, n) ->
143 GUIClockParams params = new Gson().fromJson(p, GUIClockParams.class);
145 throw new JsonSyntaxException("Invalid!!!");
146 return new GUIClock(m, params, n);
150 public static class GUIClockParams
153 Orientation orientation;
155 public GUIClockParams(int delta, Orientation orientation)
158 this.orientation = orientation;