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;
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;
import net.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
import net.mograsim.logic.model.serializing.IdentifyParams;
import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
+import net.mograsim.logic.model.snippets.HighLevelStateHandler;
import net.mograsim.logic.model.util.JsonHandler;
import net.mograsim.preferences.Preferences;
private OrientationCalculator oc;
private CoreClock clock;
+ private final List<Consumer<Object>> hlsListeners;
+
public ModelClock(LogicModelModifiable model, ModelClockParams params)
{
this(model, params, null);
{
super(model, name, false);
this.params = params;
- logicObs = (i) -> model.requestRedraw();
oc = new OrientationCalculator(params.orientation, width, height);
setSize(oc.width(), oc.height());
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 get(String stateID)
+ {
+ switch (stateID)
+ {
+ case "out":
+ if (clock != null)
+ return getOutValues();
+ return null;
+ default:
+ throw new IllegalArgumentException("No high level state with ID " + stateID);
+ }
+ }
+
+ @Override
+ public void set(String stateID, Object newState)
+ {
+ switch (stateID)
+ {
+ case "out":
+ throw new UnsupportedOperationException("cannot set state of clock");
+ default:
+ throw new IllegalArgumentException("No high level state with ID " + stateID);
+ }
+ }
+
+ @Override
+ public void addListener(String stateID, Consumer<Object> 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<Object> 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)
+ {
+ return null;
+ }
+
+ @Override
+ public Object getParamsForSerializing(IdentifyParams idParams)
+ {
+ return null;
+ }
+ });
+
init();
}
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);
- }
- }
-
+ // TODO remove
public CoreClock getClock()
{
return clock;
return params;
}
+ private BitVector getOutValues()
+ {
+ return clock.getOutValues();
+ }
+
static
{
LogicCoreAdapter.addComponentAdapter(new ClockAdapter());