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=4b67650b9e6c037308801537b9eb752a5668c69f;hb=4c590b82bc039bec5669e8cbb40703057274ab8c;hp=9b0e41a3599d5ba7f11bc607f2492ab9edaed589;hpb=95d72ebf4ebc8b9aca649d3e604709fa9daaca24;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 9b0e41a3..4b67650b 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 @@ -8,6 +8,7 @@ import java.util.Map.Entry; import net.haspamelodica.swt.helper.gcs.GCDefaultConfig; 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.ui.LogicUIRenderer; import net.mograsim.logic.ui.model.ViewModel; @@ -24,10 +25,13 @@ public class SubmodelComponent extends GUIComponent private final Map supermodelPinsPerSubmodelPinUnmodifiable; private final SubmodelInterface submodelInterface; + private final String label; private double submodelScale; + private double maxVisibleRegionFillRatioForAlpha0; + private double minVisibleRegionFillRatioForAlpha1; private final LogicUIRenderer renderer; - public SubmodelComponent(ViewModelModifiable model) + public SubmodelComponent(ViewModelModifiable model, String label) { super(model); this.submodelModifiable = new ViewModelModifiable(); @@ -38,7 +42,10 @@ public class SubmodelComponent extends GUIComponent this.supermodelPinsPerSubmodelPinUnmodifiable = Collections.unmodifiableMap(supermodelPinsPerSubmodelPin); this.submodelInterface = new SubmodelInterface(submodelModifiable); + this.label = label; this.submodelScale = 1; + this.maxVisibleRegionFillRatioForAlpha0 = 0.1; + this.minVisibleRegionFillRatioForAlpha1 = 0.8; this.renderer = new LogicUIRenderer(submodelModifiable); submodelModifiable.addRedrawListener(this::requestRedraw); @@ -134,12 +141,26 @@ public class SubmodelComponent extends GUIComponent GCDefaultConfig conf = new GCDefaultConfig(gc); TranslatedGC tgc = new TranslatedGC(gc, posX, posY, submodelScale, true); conf.reset(tgc); + double visibleRegionFillRatio = Math.max(getBounds().width / visibleRegion.width, getBounds().height / 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. + int oldAlpha = gc.getAlpha(); + gc.setAlpha(Math.max(0, Math.min(255, (int) (oldAlpha * alphaFactor)))); renderer.render(tgc, visibleRegion.translate(posX, posY, submodelScale)); + gc.setAlpha(Math.max(0, Math.min(255, (int) (oldAlpha * (1 - alphaFactor))))); + Point textExtent = gc.textExtent(label); + gc.drawText(label, posX + (getBounds().width - textExtent.x) / 2, posY + (getBounds().height - textExtent.y) / 2, true); conf.reset(gc); // draw the "bounding box" after all other operations to make interface pins look better gc.drawRectangle(getBounds()); } + private static double map(double val, double valMin, double valMax, double mapMin, double mapMax) + { + return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin); + } + private static class PinMovable extends Pin { public PinMovable(GUIComponent component, int logicWidth, double relX, double relY)