From f4ecc0a914889474581caf5f4da0dad7a410ae3c Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Mon, 3 Jun 2019 22:35:53 +0200 Subject: [PATCH] Improved submodule rendering --- .../ui/model/components/SubmodelComponent.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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..eae636a9 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 @@ -25,6 +25,8 @@ public class SubmodelComponent extends GUIComponent private final SubmodelInterface submodelInterface; private double submodelScale; + private double maxVisibleRegionFillRatioForAlpha0; + private double minVisibleRegionFillRatioForAlpha1; private final LogicUIRenderer renderer; public SubmodelComponent(ViewModelModifiable model) @@ -39,6 +41,8 @@ public class SubmodelComponent extends GUIComponent this.submodelInterface = new SubmodelInterface(submodelModifiable); this.submodelScale = 1; + this.maxVisibleRegionFillRatioForAlpha0 = 0.1; + this.minVisibleRegionFillRatioForAlpha1 = 0.8; this.renderer = new LogicUIRenderer(submodelModifiable); submodelModifiable.addRedrawListener(this::requestRedraw); @@ -134,12 +138,22 @@ 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. + gc.setAlpha(Math.max(0, Math.min(255, (int) (gc.getAlpha() * alphaFactor)))); renderer.render(tgc, visibleRegion.translate(posX, posY, submodelScale)); 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) -- 2.17.1