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.Clock;
13 import net.mograsim.logic.model.model.ViewModelModifiable;
14 import net.mograsim.logic.model.model.components.GUIComponent;
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.IdentifierGetter;
22 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
23 import net.mograsim.logic.model.util.JsonHandler;
24 import net.mograsim.preferences.Preferences;
26 public class GUIClock extends GUIComponent
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 GUIClockParams params;
36 private OrientationCalculator oc;
39 public GUIClock(ViewModelModifiable model, GUIClockParams params)
41 this(model, params, null);
44 public GUIClock(ViewModelModifiable model, GUIClockParams params, String name)
48 logicObs = (i) -> model.requestRedraw();
50 oc = new OrientationCalculator(params.orientation, width, height);
51 setSize(oc.width(), oc.height());
53 this.outputPin = new Pin(this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
58 public void render(GeneralGC gc, Rectangle visibleRegion)
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);
76 public void setLogicModelBinding(Clock clock)
78 if (this.clock != null)
79 this.clock.deregisterObserver(logicObs);
82 clock.registerObserver(logicObs);
85 public boolean hasLogicModelBinding()
91 public Object getHighLevelState(String stateID)
97 return clock.getOut().getInputValues();
100 return super.getHighLevelState(stateID);
105 public void setHighLevelState(String stateID, Object newState)
110 throw new UnsupportedOperationException("cannot set state of clock");
112 super.setHighLevelState(stateID, newState);
116 public Clock getClock()
121 public Pin getOutputPin()
126 public int getDelta()
132 public GUIClockParams getParamsForSerializing(IdentifierGetter idGetter)
139 ViewLogicModelAdapter.addComponentAdapter(new ClockAdapter());
140 IndirectGUIComponentCreator.setComponentSupplier(GUIClock.class.getName(), (m, p, n) ->
142 GUIClockParams params = JsonHandler.fromJsonTree(p, GUIClockParams.class);
144 throw new JsonSyntaxException("Invalid!!!");
145 return new GUIClock(m, params, n);
149 public static class GUIClockParams
152 Orientation orientation;
154 public GUIClockParams(int delta, Orientation orientation)
157 this.orientation = orientation;