SubmodelComponent now prints click coordinates
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / SubmodelComponent.java
index 9b0e41a..8068e06 100644 (file)
@@ -5,9 +5,11 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 
-import net.haspamelodica.swt.helper.gcs.GCDefaultConfig;
+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;
@@ -24,10 +26,13 @@ public class SubmodelComponent extends GUIComponent
        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();
@@ -38,7 +43,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.4;
+               this.minVisibleRegionFillRatioForAlpha1 = 0.8;
                this.renderer = new LogicUIRenderer(submodelModifiable);
 
                submodelModifiable.addRedrawListener(this::requestRedraw);
@@ -131,15 +139,49 @@ public class SubmodelComponent extends GUIComponent
                double posX = getBounds().x;
                double posY = getBounds().y;
 
-               GCDefaultConfig conf = new GCDefaultConfig(gc);
+               GCConfig conf = new GCConfig(gc);
                TranslatedGC tgc = new TranslatedGC(gc, posX, posY, submodelScale, true);
                conf.reset(tgc);
-               renderer.render(tgc, visibleRegion.translate(posX, posY, submodelScale));
+               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();
+               int submodelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * alphaFactor)));
+               int labelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * (1 - alphaFactor))));
+               if (submodelAlpha != 0)
+               {
+                       gc.setAlpha(submodelAlpha);
+                       renderer.render(tgc, visibleRegion.translate(-posX, -posY, 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);
+               }
                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);
+       }
+
+       @Override
+       public boolean clicked(double x, double y)
+       {
+               // TODO
+               System.out.println(((x - getBounds().x) / submodelScale) + "|" + ((y - getBounds().y) / submodelScale));
+               return true;
+       }
+
        private static class PinMovable extends Pin
        {
                public PinMovable(GUIComponent component, int logicWidth, double relX, double relY)