X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2Fcomponents%2Fsubmodels%2FSubmodelComponent.java;h=621f72177cf3f941db6abaa5c285a26078c84cb2;hb=0b352440b31916f5f0c20390f7fe03b4a808e9c0;hp=b19f05ded9de7fc7065e65638f2f66bbdc43ffdc;hpb=47ea68ed5c444dd14864412639f6a6fd60ab8a0f;p=Mograsim.git
diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelComponent.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelComponent.java
index b19f05de..621f7217 100644
--- a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelComponent.java
+++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelComponent.java
@@ -2,39 +2,31 @@ package net.mograsim.logic.model.model.components.submodels;
import java.util.Collections;
import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Set;
-import java.util.function.Function;
+import java.util.function.Consumer;
import net.haspamelodica.swt.helper.gcs.GCConfig;
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.gcs.TranslatedGC;
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.model.LogicUIRenderer;
import net.mograsim.logic.model.model.ViewModel;
import net.mograsim.logic.model.model.ViewModelModifiable;
import net.mograsim.logic.model.model.components.GUIComponent;
-import net.mograsim.logic.model.model.wires.GUIWire;
import net.mograsim.logic.model.model.wires.MovablePin;
import net.mograsim.logic.model.model.wires.Pin;
-import net.mograsim.logic.model.serializing.SubmodelComponentParams;
-import net.mograsim.logic.model.serializing.SubmodelComponentParams.InterfacePinParams;
-import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters;
-import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerComponentParams;
-import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerWireParams;
-import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerWireParams.InnerPinParams;
+import net.mograsim.logic.model.model.wires.PinUsage;
+import net.mograsim.logic.model.snippets.Renderer;
/**
* A {@link GUIComponent} consisting of another model. A SubmodelComponent
can have so-called "interface pins" connecting the
* inner and outer models.
*/
+//TODO override getParams
public abstract class SubmodelComponent extends GUIComponent
{
- private static final String SUBMODEL_INTERFACE_NAME = "_submodelinterface";
+ public static final String SUBMODEL_INTERFACE_NAME = "_submodelinterface";
/**
* A modifiable view of {@link #submodel}.
*/
@@ -72,15 +64,6 @@ public abstract class SubmodelComponent extends GUIComponent
*/
private final SubmodelInterface submodelInterface;
- /**
- * The list of all high level state IDs this component supports without delegating to subcomponents.
- */
- private final Set highLevelAtomicStates;
- /**
- * A map of high level state subcomponent IDs to the {@link GUIComponent} high level state access requests are delegated to.
- */
- private final Map subcomponentsByHighLevelStateSubcomponentID;
-
/**
* The factor by which the submodel is scaled when rendering.
*/
@@ -100,6 +83,15 @@ public abstract class SubmodelComponent extends GUIComponent
*/
private final LogicUIRenderer renderer;
+ /**
+ * The {@link Renderer} used to render the symbol of this SubmodelCoponent.
+ */
+ private Renderer symbolRenderer;
+ /**
+ * The {@link Renderer} used to render the outline of this SubmodelCoponent.
+ */
+ private Renderer outlineRenderer;
+
// creation and destruction
public SubmodelComponent(ViewModelModifiable model, String name)
@@ -115,15 +107,19 @@ public abstract class SubmodelComponent extends GUIComponent
this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
this.submodelInterface = new SubmodelInterface(submodelModifiable, SUBMODEL_INTERFACE_NAME);
- this.highLevelAtomicStates = new HashSet<>();
- this.subcomponentsByHighLevelStateSubcomponentID = new HashMap<>();
-
this.submodelScale = 1;
- this.maxVisibleRegionFillRatioForAlpha0 = 0.4;
- this.minVisibleRegionFillRatioForAlpha1 = 0.8;
+ this.maxVisibleRegionFillRatioForAlpha0 = 0.8;
+ this.minVisibleRegionFillRatioForAlpha1 = 0.9;
this.renderer = new LogicUIRenderer(submodelModifiable);
- submodelModifiable.addRedrawListener(this::requestRedraw);
+ Consumer redrawHandlerChangedListener = submodelModifiable::setRedrawHandler;
+ model.addRedrawHandlerChangedListener(redrawHandlerChangedListener);
+ model.addComponentRemovedListener(c ->
+ {
+ if (c == this)
+ model.removeRedrawHandlerChangedListener(redrawHandlerChangedListener);
+ });
+ submodelModifiable.setRedrawHandler(model.getRedrawHandler());
}
// pins
@@ -142,8 +138,24 @@ public abstract class SubmodelComponent extends GUIComponent
super.addPin(supermodelPin);// do this first to be fail-fast if the supermodel does not belong to this component
String name = supermodelPin.name;
- MovablePin submodelPin = new MovablePin(submodelInterface, name, supermodelPin.logicWidth, supermodelPin.getRelX() / submodelScale,
- supermodelPin.getRelY() / submodelScale);
+ // TODO if we upgrade to Java 12, replace with switch-expression
+ PinUsage submodelPinUsage;
+ switch (supermodelPin.usage)
+ {
+ case INPUT:
+ submodelPinUsage = PinUsage.OUTPUT;
+ break;
+ case OUTPUT:
+ submodelPinUsage = PinUsage.INPUT;
+ break;
+ case TRISTATE:
+ submodelPinUsage = PinUsage.TRISTATE;
+ break;
+ default:
+ throw new IllegalArgumentException("Unknown enum constant: " + supermodelPin.usage);
+ }
+ MovablePin submodelPin = new MovablePin(submodelInterface, name, supermodelPin.logicWidth, submodelPinUsage,
+ supermodelPin.getRelX() / submodelScale, supermodelPin.getRelY() / submodelScale);
submodelPin.addPinMovedListener(p ->
{
@@ -264,179 +276,74 @@ public abstract class SubmodelComponent extends GUIComponent
return supermodelPins.get(name);
}
- // high-level access
-
- /**
- * Adds the given subcomponent ID to the set of allowed subcomponent IDs and links the given {@link GUIComponent} as the delegate target
- * for this subcomponent ID.
- * Note that this method does not affect whether {@link #setSubcomponentHighLevelState(String, String, Object)
- * set}/{@link #getSubcomponentHighLevelState(String, String)} will be called.
- * See {@link GUIComponent#setHighLevelState(String, Object)} for details about subcomponent IDs.
- *
- * @author Daniel Kirschten
- */
- protected void addHighLevelStateSubcomponentID(String subcomponentID, GUIComponent subcomponent)
- {
- checkHighLevelStateIDPart(subcomponentID);
- subcomponentsByHighLevelStateSubcomponentID.put(subcomponentID, subcomponent);
- }
-
- /**
- * Removes the given subcomponent ID from the set of allowed subcomponent IDs.
- * Note that this method does not affect whether {@link #setSubcomponentHighLevelState(String, String, Object)
- * set}/{@link #getSubcomponentHighLevelState(String, String)} will be called.
- * See {@link GUIComponent#setHighLevelState(String, Object)} for details about subcomponent IDs.
- *
- * @author Daniel Kirschten
- */
- protected void removeHighLevelStateSubcomponentID(String subcomponentID)
- {
- subcomponentsByHighLevelStateSubcomponentID.remove(subcomponentID);
- }
-
- /**
- * Adds the given atomic state ID to the set of allowed atomic state IDs.
- * See {@link GUIComponent#setHighLevelState(String, Object)} for details about atomic state IDs.
- *
- * @author Daniel Kirschten
- */
- protected void addAtomicHighLevelStateID(String stateID)
- {
- checkHighLevelStateIDPart(stateID);
- highLevelAtomicStates.add(stateID);
- }
+ // "graphical" operations
/**
- * Removes the given atomic state ID from the set of allowed atomic state IDs.
- * See {@link GUIComponent#setHighLevelState(String, Object)} for details about atomic state IDs.
+ * Sets the factor by which the submodel is scaled when rendering and calls redrawListeners. Note that the submodel interface pins will
+ * stay at their position relative to the supermodel, which means they will move relative to the submodel.
*
* @author Daniel Kirschten
*/
- protected void removeAtomicHighLevelStateID(String stateID)
+ protected void setSubmodelScale(double submodelScale)
{
- highLevelAtomicStates.remove(stateID);
- }
+ this.submodelScale = submodelScale;
- @Override
- public final void setHighLevelState(String stateID, Object newState)
- {
- int indexOfDot = stateID.indexOf('.');
- if (indexOfDot == -1)
- if (highLevelAtomicStates.contains(stateID))
- setAtomicHighLevelState(stateID, newState);
- else
- super.setHighLevelState(stateID, newState);
- else
- setSubcomponentHighLevelState(stateID.substring(0, indexOfDot), stateID.substring(indexOfDot + 1), newState);
- }
+ for (Entry e : supermodelPins.entrySet())
+ getSubmodelMovablePin(e.getKey()).setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale);
- /**
- * This method is called in {@link #setHighLevelState(String, Object)} when the state ID is not atomic. The default implementation uses
- * the information given to {@link #addHighLevelStateSubcomponentID(String, GUIComponent)
- * add}/{@link #removeHighLevelStateSubcomponentID(String)} to decide which subcomponent to delegate to.
- * Note that {@link #addHighLevelStateSubcomponentID(String, GUIComponent) add}/{@link #removeHighLevelStateSubcomponentID(String)}
- * don't affect whether this method will be called.
- *
- * @author Daniel Kirschten
- */
- protected void setSubcomponentHighLevelState(String subcomponentID, String subcomponentHighLevelStateID, Object newState)
- {
- GUIComponent subcomponent = subcomponentsByHighLevelStateSubcomponentID.get(subcomponentID);
- if (subcomponent != null)
- subcomponent.setHighLevelState(subcomponentHighLevelStateID, newState);
- else
- super.setHighLevelState(subcomponentID + "." + subcomponentHighLevelStateID, newState);
+ model.requestRedraw();// needed if there is no submodel interface pin
}
/**
- * This method is called in {@link #setHighLevelState(String, Object)} when the state ID is atomic and in the set of allowed atomic
- * state IDs.
- * See {@link GUIComponent#setHighLevelState(String, Object)} for details about atomic state IDs.
+ * Returns the current factor by which the submodel is scaled when rendering.
*
* @author Daniel Kirschten
*/
- @SuppressWarnings({ "static-method", "unused" }) // this method is intended to be overridden
- protected void setAtomicHighLevelState(String stateID, Object newState)
+ public double getSubmodelScale()
{
- throw new IllegalStateException("Unknown high level state ID: " + stateID);
- }
-
- @Override
- public final Object getHighLevelState(String stateID)
- {
- int indexOfDot = stateID.indexOf('.');
- if (indexOfDot == -1)
- {
- if (highLevelAtomicStates.contains(stateID))
- return getAtomicHighLevelState(stateID);
- return super.getHighLevelState(stateID);
- }
- return getSubcomponentHighLevelState(stateID.substring(0, indexOfDot), stateID.substring(indexOfDot + 1));
+ return submodelScale;
}
/**
- * This method is called in {@link #getHighLevelState(String, Object)} when the state ID is not atomic. The default implementation uses
- * the information given to {@link #addHighLevelStateSubcomponentID(String, GUIComponent)
- * add}/{@link #removeHighLevelStateSubcomponentID(String)} to decide which subcomponent to delegate to.
- * Note that {@link #addHighLevelStateSubcomponentID(String, GUIComponent) add}/{@link #removeHighLevelStateSubcomponentID(String)}
- * don't affect whether this method will be called.
+ * @see #renderSymbol(GeneralGC, Rectangle)
*
* @author Daniel Kirschten
*/
- protected Object getSubcomponentHighLevelState(String subcomponentID, String subcomponentHighLevelStateID)
+ protected void setSymbolRenderer(Renderer symbolRenderer)
{
- GUIComponent subcomponent = subcomponentsByHighLevelStateSubcomponentID.get(subcomponentID);
- if (subcomponent != null)
- return subcomponent.getHighLevelState(subcomponentHighLevelStateID);
- return super.getHighLevelState(subcomponentID + "." + subcomponentHighLevelStateID);
+ this.symbolRenderer = symbolRenderer;
+ model.requestRedraw();
}
/**
- * This method is called in {@link SubmodelComponent#getHighLevelState(String)} when the state ID is in the set of allowed atomic state
- * IDs.
- * See {@link GUIComponent#setHighLevelState(String, Object)} for details about atomic state IDs.
+ * @see #renderSymbol(GeneralGC, Rectangle)
*
* @author Daniel Kirschten
*/
- @SuppressWarnings("static-method") // this method is intended to be overridden
- protected Object getAtomicHighLevelState(String stateID)
+ public Renderer getSymbolRenderer()
{
- throw new IllegalStateException("Unknown high level state ID: " + stateID);
+ return symbolRenderer;
}
- private static void checkHighLevelStateIDPart(String stateIDPart)
- {
- if (stateIDPart.indexOf('.') != -1)
- throw new IllegalArgumentException("Illegal high level state ID part (contains dot): " + stateIDPart);
-
- }
-
- // "graphical" operations
-
/**
- * Sets the factor by which the submodel is scaled when rendering and calls redrawListeners. Note that the submodel interface pins will
- * stay at their position relative to the supermodel, which means they will move relative to the submodel.
+ * @see #renderOutline(GeneralGC, Rectangle)
*
* @author Daniel Kirschten
*/
- protected void setSubmodelScale(double submodelScale)
+ protected void setOutlineRenderer(Renderer outlineRenderer)
{
- this.submodelScale = submodelScale;
-
- for (Entry e : supermodelPins.entrySet())
- getSubmodelMovablePin(e.getKey()).setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale);
-
- requestRedraw();// needed if there is no submodel interface pin
+ this.outlineRenderer = outlineRenderer;
+ model.requestRedraw();
}
/**
- * Returns the current factor by which the submodel is scaled when rendering.
+ * @see #renderOutline(GeneralGC, Rectangle)
*
* @author Daniel Kirschten
*/
- protected double getSubmodelScale()
+ public Renderer getOutlineRenderer()
{
- return submodelScale;
+ return outlineRenderer;
}
@Override
@@ -467,20 +374,27 @@ public abstract class SubmodelComponent extends GUIComponent
renderOutline(gc, visibleRegion);
}
- // TODO make this a path
/**
- * Render the outline of this {@link SubmodelComponent}, e.g. the graphical elements that should stay visible if the submodel is drawn.
+ * Render the symbol of this {@link SubmodelComponent}, e.g. the things that should be hidden if the submodel is drawn.
*
* @author Daniel Kirschten
*/
- protected abstract void renderOutline(GeneralGC gc, Rectangle visibleRegion);
+ protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
+ {
+ if (symbolRenderer != null)
+ symbolRenderer.render(gc, visibleRegion);
+ }
/**
- * Render the symbol of this {@link SubmodelComponent}, e.g. the things that should be hidden if the submodel is drawn.
+ * Render the outline of this {@link SubmodelComponent}, e.g. the graphical elements that should stay visible if the submodel is drawn.
*
* @author Daniel Kirschten
*/
- protected abstract void renderSymbol(GeneralGC gc, Rectangle visibleRegion);
+ protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
+ {
+ if (outlineRenderer != null)
+ outlineRenderer.render(gc, visibleRegion);
+ }
private static double map(double val, double valMin, double valMax, double mapMin, double mapMax)
{
@@ -498,84 +412,6 @@ public abstract class SubmodelComponent extends GUIComponent
return false;
}
- // serializing
-
- // TODO move the methods below to serializing classes
-
- public SubmodelComponentParams calculateParams()
- {
- return calculateParams(c -> "class:" + c.getClass().getCanonicalName());
- }
-
- /**
- * @return {@link SubmodelComponentParams}, which describe this {@link SubmodelComponent}.
- */
- public SubmodelComponentParams calculateParams(Function getIdentifier)
- {
- SubmodelComponentParams params = new SubmodelComponentParams();
- params.submodel = calculateSubmodelParams(getIdentifier);
-
- params.width = getWidth();
- params.height = getHeight();
-
- InterfacePinParams[] iPins = new InterfacePinParams[getPins().size()];
- int i = 0;
- for (Pin p : getPins().values())
- {
- InterfacePinParams iPinParams = new InterfacePinParams();
- iPins[i] = iPinParams;
- iPinParams.location = p.getRelPos();
- iPinParams.name = p.name;
- iPinParams.logicWidth = p.logicWidth;
- i++;
- }
- params.interfacePins = iPins;
- return params;
- }
-
- private SubmodelParameters calculateSubmodelParams(Function getIdentifier)
- {
- SubmodelParameters params = new SubmodelParameters();
- params.innerScale = getSubmodelScale();
-
- Map components = new HashMap<>(submodel.getComponentsByName());
- components.remove(SUBMODEL_INTERFACE_NAME);
- InnerComponentParams[] comps = new InnerComponentParams[components.size()];
- int i = 0;
- for (GUIComponent component : components.values())
- {
- InnerComponentParams inner = new InnerComponentParams();
- comps[i] = inner;
- inner.pos = new Point(component.getPosX(), component.getPosY());
- inner.id = getIdentifier.apply(component);
- inner.params = component.getParams();
- inner.name = component.name;
- i++;
- }
- params.subComps = comps;
-
- List wireList = submodel.getWires();
- InnerWireParams wires[] = new InnerWireParams[wireList.size()];
- i = 0;
- for (GUIWire wire : wireList)
- {
- InnerWireParams inner = new InnerWireParams();
- wires[i] = inner;
- InnerPinParams pin1Params = new InnerPinParams(), pin2Params = new InnerPinParams();
-
- pin1Params.pinName = wire.getPin1().name;
- pin1Params.compName = wire.getPin1().component.name;
- pin2Params.pinName = wire.getPin2().name;
- pin2Params.compName = wire.getPin2().component.name;
- inner.pin1 = pin1Params;
- inner.pin2 = pin2Params;
- inner.path = wire.getPath();
- i++;
- }
- params.innerWires = wires;
- return params;
- }
-
// operations no longer supported
@Override