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=08e7d1af1d539a803367b166fd05f17295d0c53d;hb=a84700145147c263ad6692c99117a7cf37832378;hp=8068e06c7c314972bf14ecf5d4413a9ded43d2f2;hpb=3a9b1d1922b328b40bf17c6dfc0f4c3948c90158;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 8068e06c..08e7d1af 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 @@ -2,48 +2,105 @@ package net.mograsim.logic.ui.model.components; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; 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.Font; import net.haspamelodica.swt.helper.swtobjectwrappers.Point; import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; import net.mograsim.logic.ui.LogicUIRenderer; import net.mograsim.logic.ui.model.ViewModel; import net.mograsim.logic.ui.model.ViewModelModifiable; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerPinParams; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerWireParams; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InterfacePinParams; +import net.mograsim.logic.ui.model.wires.GUIWire; +import net.mograsim.logic.ui.model.wires.MovablePin; import net.mograsim.logic.ui.model.wires.Pin; -public class SubmodelComponent extends GUIComponent +/** + * 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; - private final Map submodelPinsPerSupermodelPin; - private final Map submodelPinsPerSupermodelPinUnmodifiable; - private final Map supermodelPinsPerSubmodelPin; - private final Map supermodelPinsPerSubmodelPinUnmodifiable; + /** + * 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; - private final String label; + /** + * 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; - public SubmodelComponent(ViewModelModifiable model, String label) + // creation and destruction + + public SubmodelComponent(ViewModelModifiable model) { super(model); this.submodelModifiable = new ViewModelModifiable(); this.submodel = submodelModifiable; - this.submodelPinsPerSupermodelPin = new HashMap<>(); - this.submodelPinsPerSupermodelPinUnmodifiable = Collections.unmodifiableMap(submodelPinsPerSupermodelPin); - this.supermodelPinsPerSubmodelPin = new HashMap<>(); - this.supermodelPinsPerSubmodelPinUnmodifiable = Collections.unmodifiableMap(supermodelPinsPerSubmodelPin); + this.submodelPins = new HashMap<>(); + this.submodelMovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins); + this.submodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins); + this.supermodelPins = new HashMap<>(); + this.supermodelMovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins); + this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins); this.submodelInterface = new SubmodelInterface(submodelModifiable); - this.label = label; this.submodelScale = 1; this.maxVisibleRegionFillRatioForAlpha0 = 0.4; this.minVisibleRegionFillRatioForAlpha1 = 0.8; @@ -52,97 +109,179 @@ public class SubmodelComponent extends GUIComponent submodelModifiable.addRedrawListener(this::requestRedraw); } - protected void setSubmodelScale(double submodelScale) - { - this.submodelScale = submodelScale; - - for (Entry e : supermodelPinsPerSubmodelPin.entrySet()) - e.getKey().setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale); - - requestRedraw();// needed if there is no submodel interface pin - } + // 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(int logicWidth, double relX, double relY) + protected Pin addSubmodelInterface(MovablePin supermodelPin) { - PinMovable submodelPin = new PinMovable(submodelInterface, logicWidth, relX / submodelScale, relY / submodelScale); - submodelInterface.addPin(submodelPin); + 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 -> + { + double newRelX = p.getRelX() * submodelScale; + double newRelY = p.getRelY() * submodelScale; + if (supermodelPin.getRelX() != newRelX || supermodelPin.getRelY() != newRelY) + supermodelPin.setRelPos(newRelX, newRelY); + }); + supermodelPin.addPinMovedListener(p -> + { + double newRelX = p.getRelX() / submodelScale; + double newRelY = p.getRelY() / submodelScale; + if (submodelPin.getRelX() != newRelX || submodelPin.getRelY() != newRelY) + submodelPin.setRelPos(newRelX, newRelY); + }); - PinMovable supermodelPin = new PinMovable(this, logicWidth, relX, relY); - addPin(supermodelPin); + submodelInterface.addPin(submodelPin); - submodelPinsPerSupermodelPin.put(supermodelPin, submodelPin); - supermodelPinsPerSubmodelPin.put(submodelPin, supermodelPin); + submodelPins.put(name, submodelPin); + supermodelPins.put(name, supermodelPin); // no need to call requestRedraw() because addPin() will request a redraw return submodelPin; } - protected void moveSubmodelInterface(Pin supermodelPin, double relX, double relY) + /** + * Removes a submodel interface pin. + * + * @author Daniel Kirschten + */ + protected void removeSubmodelInterface(String name) { - PinMovable submodelPin = getSubmodelMovablePin(supermodelPin); - PinMovable supermodelPinMovable = getSupermodelMovablePin(submodelPin); + 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); + supermodelPins.remove(name); - submodelPin.setRelPos(relX / submodelScale, relY / submodelScale); - supermodelPinMovable.setRelPos(relX, relY); + // no need to call requestRedraw() because removePin() will request a redraw + } - // no need to call requestRedraw() because setRelPos() 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; } - protected void removeSubmodelInterface(Pin supermodelPin) + /** + * Returns the submodel interface pin with the given name on the submodel side of this component. + * + * @author Daniel Kirschten + */ + public Pin getSubmodelPin(String name) { - removePin(supermodelPin); - Pin submodelPin = getSubmodelMovablePin(supermodelPin); - submodelInterface.removePin(submodelPin); + return getSubmodelMovablePin(name); + } - submodelPinsPerSupermodelPin.remove(supermodelPin); - supermodelPinsPerSubmodelPin.remove(submodelPin); + /** + * Returns a collection of movable submodel interface pins on the submodel side of this component. + * + * @author Daniel Kirschten + */ + protected Map getSubmodelMovablePins() + { + return submodelMovablePinsUnmodifiable; + } - // no need to call requestRedraw() because removePin() will request a redraw + /** + * 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); } - public Map getSupermodelPinsPerSubmodelPin() + /** + * Returns a collection of submodel interface pins on the supermodel side of this component. + * + * @author Daniel Kirschten + */ + public Map getSupermodelPins() { - return supermodelPinsPerSubmodelPinUnmodifiable; + return supermodelUnmovablePinsUnmodifiable; } - public Pin getSupermodelPin(Pin submodelPin) + /** + * 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(submodelPin); + return getSupermodelMovablePin(name); } - protected PinMovable getSupermodelMovablePin(Pin submodelPin) + /** + * Returns a collection of movable submodel interface pins on the supermodel side of this component. + * + * @author Daniel Kirschten + */ + protected Map getSupermodelMovablePins() { - return supermodelPinsPerSubmodelPin.get(submodelPin); + return supermodelMovablePinsUnmodifiable; } - public Map getSubmodelPinsPerSupermodelPin() + /** + * 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 submodelPinsPerSupermodelPinUnmodifiable; + return supermodelPins.get(name); } - public Pin getSubmodelPin(Pin supermodelPin) + // "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) { - return getSubmodelMovablePin(supermodelPin); + 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 PinMovable getSubmodelMovablePin(Pin supermodelPin) + /** + * Returns the current factor by which the submodel is scaled when rendering. + * + * @author Daniel Kirschten + */ + protected double getSubmodelScale() { - return submodelPinsPerSupermodelPin.get(supermodelPin); + 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. @@ -152,23 +291,22 @@ public class SubmodelComponent extends GUIComponent if (submodelAlpha != 0) { gc.setAlpha(submodelAlpha); - renderer.render(tgc, visibleRegion.translate(-posX, -posY, 1 / submodelScale)); + renderer.render(tgc, visibleRegion.translate(getPosX() / submodelScale, getPosY() / submodelScale, 1 / submodelScale)); } if (labelAlpha != 0) { gc.setAlpha(labelAlpha); - Font oldFont = gc.getFont(); - Font labelFont = new Font(oldFont.getName(), 6, oldFont.getStyle()); - gc.setFont(labelFont); - Point textExtent = gc.textExtent(label); - gc.drawText(label, posX + (getBounds().width - textExtent.x) / 2, posY + (getBounds().height - textExtent.y) / 2, true); - gc.setFont(oldFont); + renderSymbol(gc, visibleRegion); } conf.reset(gc); - // draw the "bounding box" after all other operations to make interface pins look better - gc.drawRectangle(getBounds()); + // draw the outline after all other operations to make interface pins look better + renderOutline(gc, visibleRegion); } + protected abstract void renderOutline(GeneralGC gc, Rectangle visibleRegion); + + protected abstract void renderSymbol(GeneralGC gc, Rectangle visibleRegion); + private static double map(double val, double valMin, double valMax, double mapMin, double mapMax) { return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin); @@ -177,22 +315,98 @@ public class SubmodelComponent extends GUIComponent @Override public boolean clicked(double x, double y) { - // TODO - System.out.println(((x - getBounds().x) / submodelScale) + "|" + ((y - getBounds().y) / submodelScale)); - return true; + 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}. + */ + public SubmodelComponentParams calculateParams() + { + SubmodelComponentParams params = new SubmodelComponentParams(); + params.type = SubmodelComponent.class.getSimpleName(); + params.composition = calculateCompositionParams(); + + 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 static class PinMovable extends Pin + protected ComponentCompositionParams calculateCompositionParams() { - public PinMovable(GUIComponent component, int logicWidth, double relX, double relY) + ComponentCompositionParams params = new ComponentCompositionParams(); + params.innerScale = getSubmodelScale(); + + List compList = submodelModifiable.getComponents(); + Iterator componentIt = compList.iterator(); + componentIt.next(); // Skip inner SubmodelInterface + InnerComponentParams[] comps = new InnerComponentParams[compList.size() - 1]; + int i = 0; + while (componentIt.hasNext()) { - super(component, logicWidth, relX, relY); + GUIComponent component = componentIt.next(); + InnerComponentParams inner = new InnerComponentParams(); + comps[i] = inner; + inner.params = component.getInstantiationParameters(); + inner.pos = new Point(getPosX(), getPosY()); + inner.type = component.getIdentifier(); + i++; } + params.subComps = comps; - @Override - protected void setRelPos(double relX, double relY) + List wireList = submodelModifiable.getWires(); + InnerWireParams wires[] = new InnerWireParams[wireList.size()]; + i = 0; + for (GUIWire wire : wireList) { - super.setRelPos(relX, relY); + InnerWireParams inner = new InnerWireParams(); + wires[i] = inner; + InnerPinParams pin1Params = new InnerPinParams(), pin2Params = new InnerPinParams(); + + pin1Params.pinName = wire.getPin1().name; + pin1Params.compId = compList.indexOf(wire.getPin1().component); + pin2Params.pinName = wire.getPin2().name; + pin2Params.compId = compList.indexOf(wire.getPin2().component); + inner.pin1 = pin1Params; + inner.pin2 = pin2Params; + inner.path = wire.getPath(); + i++; } + params.innerWires = wires; + return params; + } + + // operations no longer supported + + @Override + protected void addPin(Pin pin) + { + throw new UnsupportedOperationException("Can't add pins to a SubmodelComponent directly, call addSubmodelInterface instead"); + } + + @Override + protected void removePin(String name) + { + throw new UnsupportedOperationException("Can't remove pins of a SubmodelComponent directly, call removeSubmodelInterface instead"); } } \ No newline at end of file