X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2Fcomponents%2Fatomic%2FModelClock.java;h=3c4eec617377899491730f63462286eafa4829ce;hb=13577856cd85c46f2cd4ad956332697bc820f425;hp=d84e2027a41d8d31db743c3c6bff2c2023858820;hpb=0c97f70d199e1ffcd2bc13817f7bb12867ba033e;p=Mograsim.git diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelClock.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelClock.java index d84e2027..3c4eec61 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelClock.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelClock.java @@ -1,5 +1,9 @@ package net.mograsim.logic.model.model.components.atomic; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + import org.eclipse.swt.graphics.Color; import com.google.gson.JsonSyntaxException; @@ -10,6 +14,7 @@ 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.CoreClock; +import net.mograsim.logic.core.types.BitVector; import net.mograsim.logic.model.model.LogicModelModifiable; import net.mograsim.logic.model.model.components.ModelComponent; import net.mograsim.logic.model.model.components.Orientation; @@ -37,6 +42,8 @@ public class ModelClock extends ModelComponent private OrientationCalculator oc; private CoreClock clock; + private final List> hlsListeners; + public ModelClock(LogicModelModifiable model, ModelClockParams params) { this(model, params, null); @@ -46,7 +53,6 @@ public class ModelClock extends ModelComponent { super(model, name, false); this.params = params; - logicObs = (i) -> model.requestRedraw(); oc = new OrientationCalculator(params.orientation, width, height); setSize(oc.width(), oc.height()); @@ -54,16 +60,25 @@ public class ModelClock extends ModelComponent this.outputPin = new Pin(model, this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2)); addPin(outputPin); + this.hlsListeners = new ArrayList<>(); + + logicObs = i -> + { + model.requestRedraw(); + BitVector v = getOutValues(); + hlsListeners.forEach(l -> l.accept(v)); + }; + setHighLevelStateHandler(new HighLevelStateHandler() { @Override - public Object getHighLevelState(String stateID) + public Object get(String stateID) { switch (stateID) { case "out": if (clock != null) - return clock.getOut().getInputValues(); + return getOutValues(); return null; default: throw new IllegalArgumentException("No high level state with ID " + stateID); @@ -71,7 +86,7 @@ public class ModelClock extends ModelComponent } @Override - public void setHighLevelState(String stateID, Object newState) + public void set(String stateID, Object newState) { switch (stateID) { @@ -82,6 +97,32 @@ public class ModelClock extends ModelComponent } } + @Override + public void addListener(String stateID, Consumer stateChanged) + { + switch (stateID) + { + case "out": + hlsListeners.add(stateChanged); + break; + default: + throw new IllegalArgumentException("No high level state with ID " + stateID); + } + } + + @Override + public void removeListener(String stateID, java.util.function.Consumer stateChanged) + { + switch (stateID) + { + case "out": + hlsListeners.remove(stateChanged); + break; + default: + throw new IllegalArgumentException("No high level state with ID " + stateID); + } + } + @Override public String getIDForSerializing(IdentifyParams idParams) { @@ -131,6 +172,7 @@ public class ModelClock extends ModelComponent return clock != null; } + // TODO remove public CoreClock getClock() { return clock; @@ -158,6 +200,11 @@ public class ModelClock extends ModelComponent return params; } + private BitVector getOutValues() + { + return clock.getOutValues(); + } + static { LogicCoreAdapter.addComponentAdapter(new ClockAdapter());