X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fcomponents%2FSubmodelComponent.java;h=bb6cca1796f36cd5b1bf176f6af1d833fa3ec3a3;hb=f14ea37d69488dd51518a36413af7176916b8bd7;hp=be9b854800057fa18ab1e5ade0cf5a0b8b3b6970;hpb=7de4f2b3d61f8801459645d4bd337a91405e107a;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java index be9b8548..bb6cca17 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java @@ -24,23 +24,70 @@ import net.mograsim.logic.ui.model.wires.GUIWire; import net.mograsim.logic.ui.model.wires.MovablePin; import net.mograsim.logic.ui.model.wires.Pin; +/** + * A {@link GUIComponent} consisting of another model. A SubmodelComponent can have so-called "interface pins" connecting the + * inner and outer models. + */ public abstract class SubmodelComponent extends GUIComponent { + /** + * A modifiable view of {@link #submodel}. + */ protected final ViewModelModifiable submodelModifiable; + /** + * The model this {@link SubmodelComponent} consists of. + */ public final ViewModel submodel; + /** + * The list of all submodel interface pins of this {@link SubmodelComponent} on the submodel side. + */ private final Map submodelPins; + /** + * An unmodifiable view of {@link #submodelPins}. + */ private final Map submodelMovablePinsUnmodifiable; + /** + * An unmodifiable view of {@link #submodelPins} where pins are not movable. + */ private final Map submodelUnmovablePinsUnmodifiable; + /** + * The list of all submodel interface pins of this {@link SubmodelComponent} on the supermodel side. + */ private final Map supermodelPins; + /** + * An unmodifiable view of {@link #supermodelPins}. + */ private final Map supermodelMovablePinsUnmodifiable; + /** + * An unmodifiable view of {@link #supermodelPins} where pins are not movable. + */ private final Map supermodelUnmovablePinsUnmodifiable; + /** + * A pseudo-component containing all submodel interface pins on the submodel side. + */ private final SubmodelInterface submodelInterface; + /** + * The factor by which the submodel is scaled when rendering. + */ private double submodelScale; + /** + * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel starts to + * be visible. + */ private double maxVisibleRegionFillRatioForAlpha0; + /** + * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel is fully + * visible. + */ private double minVisibleRegionFillRatioForAlpha1; + /** + * The renderer used for rendering the submodel. + */ private final LogicUIRenderer renderer; + // creation and destruction + public SubmodelComponent(ViewModelModifiable model) { super(model); @@ -62,28 +109,24 @@ public abstract class SubmodelComponent extends GUIComponent submodelModifiable.addRedrawListener(this::requestRedraw); } - protected void setSubmodelScale(double submodelScale) - { - 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 - } - - protected double getSubmodelScale() - { - return submodelScale; - } + // pins /** - * Returns the submodel pin. + * Adds a new submodel interface pin. + * + * @param supermodelPin the submodel interface pin on the supermodel side + * + * @return the submodel interface pin on the submodel side + * + * @author Daniel Kirschten */ - protected Pin addSubmodelInterface(String name, int logicWidth, double relX, double relY) + protected Pin addSubmodelInterface(MovablePin supermodelPin) { - MovablePin submodelPin = new MovablePin(submodelInterface, name, logicWidth, relX / submodelScale, relY / submodelScale); - MovablePin supermodelPin = new MovablePin(this, name, logicWidth, relX, relY); + 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); submodelPin.addPinMovedListener(p -> { @@ -101,7 +144,6 @@ public abstract class SubmodelComponent extends GUIComponent }); submodelInterface.addPin(submodelPin); - super.addPin(supermodelPin); submodelPins.put(name, submodelPin); supermodelPins.put(name, supermodelPin); @@ -110,68 +152,136 @@ public abstract class SubmodelComponent extends GUIComponent return submodelPin; } + /** + * Removes a submodel interface pin. + * + * @author Daniel Kirschten + */ protected void removeSubmodelInterface(String name) { - super.removePin(name); - Pin submodelPin = getSubmodelMovablePin(name); + super.removePin(name);// do this first to be fail-fast if this component doesn't have a pin with the given name + Pin submodelPin = submodelPins.remove(name); submodelInterface.removePin(submodelPin.name); - - submodelPins.remove(name); supermodelPins.remove(name); // no need to call requestRedraw() because removePin() will request a redraw } + /** + * Returns a collection of submodel interface pins on the submodel side of this component. + * + * @author Daniel Kirschten + */ public Map getSubmodelPins() { return submodelUnmovablePinsUnmodifiable; } + /** + * Returns the submodel interface pin with the given name on the submodel side of this component. + * + * @author Daniel Kirschten + */ public Pin getSubmodelPin(String name) { return getSubmodelMovablePin(name); } + /** + * Returns a collection of movable submodel interface pins on the submodel side of this component. + * + * @author Daniel Kirschten + */ protected Map getSubmodelMovablePins() { return submodelMovablePinsUnmodifiable; } + /** + * Returns the movable submodel interface pin with the given name on the submodel side of this component. + * + * @author Daniel Kirschten + */ protected MovablePin getSubmodelMovablePin(String name) { return submodelPins.get(name); } + /** + * Returns a collection of submodel interface pins on the supermodel side of this component. + * + * @author Daniel Kirschten + */ public Map getSupermodelPins() { return supermodelUnmovablePinsUnmodifiable; } + /** + * Returns the submodel interface pin with the given name on the supermodel side of this component. + * + * @author Daniel Kirschten + */ public Pin getSupermodelPin(String name) { return getSupermodelMovablePin(name); } + /** + * Returns a collection of movable submodel interface pins on the supermodel side of this component. + * + * @author Daniel Kirschten + */ protected Map getSupermodelMovablePins() { return supermodelMovablePinsUnmodifiable; } + /** + * Returns the movable submodel interface pin with the given name on the supermodel side of this component. + * + * @author Daniel Kirschten + */ protected MovablePin getSupermodelMovablePin(String name) { return supermodelPins.get(name); } + // "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. + * + * @author Daniel Kirschten + */ + protected void setSubmodelScale(double submodelScale) + { + 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 + } + + /** + * Returns the current factor by which the submodel is scaled when rendering. + * + * @author Daniel Kirschten + */ + protected double getSubmodelScale() + { + return submodelScale; + } + @Override public void render(GeneralGC gc, Rectangle visibleRegion) { - double posX = getBounds().x; - double posY = getBounds().y; - GCConfig conf = new GCConfig(gc); - TranslatedGC tgc = new TranslatedGC(gc, posX, posY, submodelScale, true); + TranslatedGC tgc = new TranslatedGC(gc, getPosX(), getPosY(), submodelScale, true); conf.reset(tgc); - double visibleRegionFillRatio = Math.max(getBounds().width / visibleRegion.width, getBounds().height / visibleRegion.height); + double visibleRegionFillRatio = Math.max(getWidth() / visibleRegion.width, getHeight() / visibleRegion.height); double alphaFactor = map(visibleRegionFillRatio, maxVisibleRegionFillRatioForAlpha0, minVisibleRegionFillRatioForAlpha1, 0, 1); alphaFactor = Math.max(0, Math.min(1, alphaFactor)); // we need to take the old alpha into account to support nested submodules better. @@ -181,7 +291,7 @@ public abstract class SubmodelComponent extends GUIComponent if (submodelAlpha != 0) { gc.setAlpha(submodelAlpha); - renderer.render(tgc, visibleRegion.translate(posX / submodelScale, posY / submodelScale, 1 / submodelScale)); + renderer.render(tgc, visibleRegion.translate(getPosX() / submodelScale, getPosY() / submodelScale, 1 / submodelScale)); } if (labelAlpha != 0) { @@ -205,14 +315,16 @@ public abstract class SubmodelComponent extends GUIComponent @Override public boolean clicked(double x, double y) { - double scaledX = (x - getBounds().x) / submodelScale; - double scaledY = (y - getBounds().y) / submodelScale; + double scaledX = (x - getPosX()) / submodelScale; + double scaledY = (y - getPosY()) / submodelScale; for (GUIComponent component : submodel.getComponents()) if (component.getBounds().contains(scaledX, scaledY) && component.clicked(scaledX, scaledY)) return true; return false; } + // serializing + /** * @return {@link SubmodelComponentParams}, which describe this {@link SubmodelComponent}. */ @@ -222,13 +334,12 @@ public abstract class SubmodelComponent extends GUIComponent params.type = SubmodelComponent.class.getSimpleName(); params.composition = calculateCompositionParams(); - Rectangle bounds = getBounds(); - params.width = bounds.width; - params.height = bounds.height; + params.width = getWidth(); + params.height = getHeight(); InterfacePinParams[] iPins = new InterfacePinParams[getPins().size()]; int i = 0; - for (Pin p : getPins()) + for (Pin p : getPins().values()) { InterfacePinParams iPinParams = new InterfacePinParams(); iPins[i] = iPinParams; @@ -257,8 +368,7 @@ public abstract class SubmodelComponent extends GUIComponent InnerComponentParams inner = new InnerComponentParams(); comps[i] = inner; inner.params = component.getInstantiationParameters(); - Rectangle bounds = component.getBounds(); - inner.pos = new Point(bounds.x, bounds.y); + inner.pos = new Point(getPosX(), getPosY()); inner.type = component.getIdentifier(); i++; } @@ -286,6 +396,18 @@ public abstract class SubmodelComponent extends GUIComponent return params; } + public List getComponents() + { + return submodel.getComponents(); + } + + public List getWires() + { + return submodel.getWires(); + } + + // operations no longer supported + @Override protected void addPin(Pin pin) {