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=431240d75490a4bec58e5e868a6e47fbc586ffb0;hb=0a04a4ed66ecebd4254541c4977599f6052c115a;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..431240d7 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,17 +1,19 @@ 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; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; +import net.mograsim.logic.core.wires.CoreWire.ReadEnd; +import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd; import net.mograsim.logic.model.model.ViewModelModifiable; 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.IdentifyParams; +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,21 @@ 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 String id; 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) + public SimpleRectangularHardcodedGUIComponent(ViewModelModifiable model, String id, String name, String centerText) { super(model, name); - pinUsages = new HashMap<>(); + this.id = id; this.outlineRenderer = new DefaultOutlineRenderer(this); CenteredTextParams centeredTextParams = new CenteredTextParams(); centeredTextParams.text = centerText; @@ -48,31 +52,73 @@ public abstract class SimpleRectangularHardcodedGUIComponent extends GUIComponen pinNamesParams.pinLabelMargin = pinNamesMargin; this.pinNamesRenderer = new PinNamesSymbolRenderer(this, pinNamesParams); addPinRemovedListener(this::pinRemoved); + setHighLevelStateHandler(new HighLevelStateHandler() + { + @Override + public String getIDForSerializing(IdentifyParams idParams) + { + return null;// we don't need to serialize this; it's implicit since we are a SimpleRectangularHardcodedGUIComponent + } + + @Override + public Object getParamsForSerializing(IdentifyParams idParams) + { + 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) + { + 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) { - return pinUsages.get(pin); + 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 @@ -84,21 +130,24 @@ public abstract class SimpleRectangularHardcodedGUIComponent extends GUIComponen pinNamesRenderer.render(gc, visibleRegion); } - // operations no longer supported + // serializing @Override - protected void addPin(Pin pin) + public String getIDForSerializing(IdentifyParams idParams) { - throw new UnsupportedOperationException("Can't add pins without setting usage, call addPin(Pin, Usage [, Position]) instead"); + return id; } - public static enum Usage + // operations no longer supported + + @Override + protected void addPin(Pin pin) { - 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