package net.mograsim.logic.core.components;
+import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
+import net.mograsim.logic.core.LogicObservable;
+import net.mograsim.logic.core.LogicObserver;
import net.mograsim.logic.core.timeline.Timeline;
import net.mograsim.logic.core.timeline.TimelineEvent;
import net.mograsim.logic.core.timeline.TimelineEventHandler;
import net.mograsim.logic.core.wires.Wire.ReadEnd;
import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
-public class Clock extends Component implements TimelineEventHandler
+public class Clock extends Component implements TimelineEventHandler, LogicObservable
{
+ private Collection<LogicObserver> observers;
private boolean toggle = false;
private ReadWriteEnd out;
private int delta;
super(timeline);
this.delta = delta;
this.out = out;
+ this.observers = new HashSet<>();
addToTimeline();
}
addToTimeline();
out.feedSignals(toggle ? Bit.ONE : Bit.ZERO);
toggle = !toggle;
+ notifyObservers();
}
public ReadWriteEnd getOut()
return out;
}
+ public boolean isOn()
+ {
+ return !toggle;
+ }
+
private void addToTimeline()
{
timeline.addEvent(this, delta);
{
return List.of(out);
}
+
+ @Override
+ public void registerObserver(LogicObserver ob)
+ {
+ observers.add(ob);
+ }
+
+ @Override
+ public void deregisterObserver(LogicObserver ob)
+ {
+ observers.remove(ob);
+ }
+
+ @Override
+ public void notifyObservers()
+ {
+ observers.forEach(ob -> ob.update(this));
+ }
}
--- /dev/null
+package net.mograsim.logic.model.model.components.atomic;
+
+import org.eclipse.swt.graphics.Color;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonSyntaxException;
+
+import net.haspamelodica.swt.helper.gcs.GeneralGC;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
+import net.mograsim.logic.core.LogicObserver;
+import net.mograsim.logic.core.components.Clock;
+import net.mograsim.logic.model.model.ViewModelModifiable;
+import net.mograsim.logic.model.model.components.GUIComponent;
+import net.mograsim.logic.model.model.components.Orientation;
+import net.mograsim.logic.model.model.components.OrientationCalculator;
+import net.mograsim.logic.model.model.wires.Pin;
+import net.mograsim.logic.model.model.wires.PinUsage;
+import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
+import net.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
+import net.mograsim.logic.model.serializing.IdentifierGetter;
+import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.preferences.Preferences;
+
+public class GUIClock extends GUIComponent
+{
+ private static final double width = 20;
+ private static final double height = 20;
+ private static final double fontHeight = 5;
+
+ private final Pin outputPin;
+
+ private final LogicObserver logicObs;
+ private GUIClockParams params;
+ private OrientationCalculator oc;
+ private Clock clock;
+
+ public GUIClock(ViewModelModifiable model, GUIClockParams params)
+ {
+ this(model, params, null);
+ }
+
+ public GUIClock(ViewModelModifiable model, GUIClockParams params, String name)
+ {
+ super(model, name);
+ this.params = params;
+ logicObs = (i) -> model.requestRedraw();
+
+ oc = new OrientationCalculator(params.orientation, width, height);
+ setSize(oc.width(), oc.height());
+
+ this.outputPin = new Pin(this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
+ addPin(outputPin);
+ }
+
+ @Override
+ public void render(GeneralGC gc, Rectangle visibleRegion)
+ {
+ Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
+ if (foreground != null)
+ gc.setForeground(foreground);
+ gc.drawRectangle(getBounds());
+ String label = clock == null ? "null" : (clock.isOn() ? "|" : "\u2015");
+ Font oldFont = gc.getFont();
+ Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
+ gc.setFont(labelFont);
+ Point textExtent = gc.textExtent(label);
+ Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
+ if (textColor != null)
+ gc.setForeground(textColor);
+ gc.drawText(label, getPosX() + (oc.width() - textExtent.x) / 2, getPosY() + (oc.height() - textExtent.y) / 2, true);
+ gc.setFont(oldFont);
+ }
+
+ public void setLogicModelBinding(Clock clock)
+ {
+ if (this.clock != null)
+ this.clock.deregisterObserver(logicObs);
+ this.clock = clock;
+ if (clock != null)
+ clock.registerObserver(logicObs);
+ }
+
+ public boolean hasLogicModelBinding()
+ {
+ return clock != null;
+ }
+
+ @Override
+ public Object getHighLevelState(String stateID)
+ {
+ switch (stateID)
+ {
+ case "out":
+ if (clock != null)
+ return clock.getOut().getInputValues();
+ return null;
+ default:
+ return super.getHighLevelState(stateID);
+ }
+ }
+
+ @Override
+ public void setHighLevelState(String stateID, Object newState)
+ {
+ switch (stateID)
+ {
+ case "out":
+ throw new UnsupportedOperationException("cannot set state of clock");
+ default:
+ super.setHighLevelState(stateID, newState);
+ }
+ }
+
+ public Clock getClock()
+ {
+ return clock;
+ }
+
+ public Pin getOutputPin()
+ {
+ return outputPin;
+ }
+
+ public int getDelta()
+ {
+ return params.delta;
+ }
+
+ @Override
+ public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ {
+ return new Gson().toJsonTree(params);
+ }
+
+ static
+ {
+ ViewLogicModelAdapter.addComponentAdapter(new ClockAdapter());
+ IndirectGUIComponentCreator.setComponentSupplier(GUIClock.class.getName(), (m, p, n) ->
+ {
+ GUIClockParams params = new Gson().fromJson(p, GUIClockParams.class);
+ if (params == null)
+ throw new JsonSyntaxException("Invalid!!!");
+ return new GUIClock(m, params, n);
+ });
+ }
+
+ public static class GUIClockParams
+ {
+ int delta;
+ Orientation orientation;
+
+ public GUIClockParams(int delta, Orientation orientation)
+ {
+ this.delta = delta;
+ this.orientation = orientation;
+ }
+ }
+}
--- /dev/null
+package net.mograsim.logic.model.modeladapter.componentadapters;
+
+import java.util.Map;
+
+import net.mograsim.logic.core.components.Clock;
+import net.mograsim.logic.core.timeline.Timeline;
+import net.mograsim.logic.core.wires.Wire;
+import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
+import net.mograsim.logic.model.model.components.atomic.GUIClock;
+import net.mograsim.logic.model.model.wires.Pin;
+import net.mograsim.logic.model.modeladapter.LogicModelParameters;
+
+public class ClockAdapter implements ComponentAdapter<GUIClock>
+{
+
+ @Override
+ public Class<GUIClock> getSupportedClass()
+ {
+ return GUIClock.class;
+ }
+
+ @Override
+ public void createAndLinkComponent(Timeline timeline, LogicModelParameters params, GUIClock guiClock, Map<Pin, Wire> logicWiresPerPin)
+ {
+ ReadWriteEnd out = logicWiresPerPin.get(guiClock.getOutputPin()).createReadWriteEnd();
+ Clock c = new Clock(timeline, out, guiClock.getDelta());
+ guiClock.setLogicModelBinding(c);
+ }
+
+}
\ No newline at end of file