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;
private final Map<Pin, Pin> 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();
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);
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)