X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2Fcomponents%2Fatomic%2FSimpleRectangularHardcodedGUIComponent.java;h=c06ec1494bd47e903d6ac34fa2dd14d736513f7e;hb=1372abe81775e52cc52b715139cf13097a481b49;hp=1ced6d4a86410a76db0cfcba5880aee83f2e5eb8;hpb=a9c80655950119176572052903dd95304ed1c141;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/SimpleRectangularHardcodedGUIComponent.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/SimpleRectangularHardcodedGUIComponent.java index 1ced6d4a..c06ec149 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/SimpleRectangularHardcodedGUIComponent.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/SimpleRectangularHardcodedGUIComponent.java @@ -1,7 +1,7 @@ package net.mograsim.logic.model.model.components.atomic; -import java.util.HashMap; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; @@ -12,6 +12,8 @@ import net.mograsim.logic.model.model.components.GUIComponent; import net.mograsim.logic.model.model.wires.Pin; import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter; import net.mograsim.logic.model.modeladapter.componentadapters.SimpleRectangularHardcodedGUIComponentAdapter; +import net.mograsim.logic.model.serializing.IdentifierGetter; +import net.mograsim.logic.model.snippets.HighLevelStateHandler; import net.mograsim.logic.model.snippets.outlinerenderers.DefaultOutlineRenderer; import net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer; import net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer.CenteredTextParams; @@ -25,19 +27,18 @@ public abstract class SimpleRectangularHardcodedGUIComponent extends GUIComponen private static final double pinNamesHeight = 3.5; private static final double pinNamesMargin = .5; - // TODO maybe make this more general? - private final Map pinUsages; - private final DefaultOutlineRenderer outlineRenderer; private final CenteredTextSymbolRenderer centerTextRenderer; private final PinNamesSymbolRenderer pinNamesRenderer; + private AtomicReference state; + private Runnable recalculate; + // creation and destruction public SimpleRectangularHardcodedGUIComponent(ViewModelModifiable model, String name, String centerText) { super(model, name); - pinUsages = new HashMap<>(); this.outlineRenderer = new DefaultOutlineRenderer(this); CenteredTextParams centeredTextParams = new CenteredTextParams(); centeredTextParams.text = centerText; @@ -48,31 +49,67 @@ public abstract class SimpleRectangularHardcodedGUIComponent extends GUIComponen pinNamesParams.pinLabelMargin = pinNamesMargin; this.pinNamesRenderer = new PinNamesSymbolRenderer(this, pinNamesParams); addPinRemovedListener(this::pinRemoved); + setHighLevelStateHandler(new HighLevelStateHandler() + { + @Override + public Object getParamsForSerializing(IdentifierGetter idGetter) + { + return null; + } + + @Override + public Object getHighLevelState(String stateID) + { + return SimpleRectangularHardcodedGUIComponent.this.getHighLevelState(state.get(), stateID); + } + + @Override + public void setHighLevelState(String stateID, Object newState) + { + state.updateAndGet(s -> SimpleRectangularHardcodedGUIComponent.this.setHighLevelState(s, stateID, newState)); + recalculate.run(); + } + }); } // pins - protected void addPin(Pin pin, Usage usage, Position namePosition) + protected void addPin(Pin pin, Position namePosition) { super.addPin(pin); // do this first to catch errors - pinUsages.put(pin, usage); pinNamesRenderer.setPinPosition(pin, namePosition); } private void pinRemoved(Pin pin) { - pinUsages.remove(pin); pinNamesRenderer.setPinPosition(pin, null); } - public Usage getPinUsage(Pin pin) + // high-level access + + @SuppressWarnings({ "static-method", "unused" }) // this method is intended to be overridden + protected Object getHighLevelState(Object state, String stateID) { - return pinUsages.get(pin); + throw new IllegalArgumentException("No high level state with ID " + stateID); + } + + @SuppressWarnings({ "static-method", "unused" }) // this method is intended to be overridden + protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState) + { + throw new IllegalArgumentException("No high level state with ID " + stateID); } // logic - protected abstract Object recalculate(Object lastState, Map readEnds, Map readWriteEnds); + public abstract Object recalculate(Object lastState, Map readEnds, Map readWriteEnds); + + // logic model binding + + public void setLogicModelBindingAndResetState(AtomicReference state, Runnable recalculate) + { + this.state = state; + this.recalculate = recalculate; + } // "graphical" operations @@ -89,16 +126,11 @@ public abstract class SimpleRectangularHardcodedGUIComponent extends GUIComponen @Override protected void addPin(Pin pin) { - throw new UnsupportedOperationException("Can't add pins without setting usage, call addPin(Pin, Usage [, Position]) instead"); - } - - public static enum Usage - { - INPUT, OUTPUT, TRISTATE; + throw new UnsupportedOperationException("Can't add pins without setting usage, call addPin(Pin [, Position]) instead"); } static { - ViewLogicModelAdapter.addComponentAdapter(new SimpleRectangularHardcodedGUIComponentAdapter(c -> c::recalculate)); + ViewLogicModelAdapter.addComponentAdapter(new SimpleRectangularHardcodedGUIComponentAdapter()); } } \ No newline at end of file