renderOutline() and renderSymbol() now have access to visibleRegion
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / SubmodelComponent.java
index eae636a..a4fe1aa 100644 (file)
@@ -5,7 +5,7 @@ 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.Rectangle;
@@ -14,7 +14,7 @@ import net.mograsim.logic.ui.model.ViewModel;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
 import net.mograsim.logic.ui.model.wires.Pin;
 
-public class SubmodelComponent extends GUIComponent
+public abstract class SubmodelComponent extends GUIComponent
 {
        protected final ViewModelModifiable submodelModifiable;
        public final ViewModel submodel;
@@ -41,7 +41,7 @@ public class SubmodelComponent extends GUIComponent
                this.submodelInterface = new SubmodelInterface(submodelModifiable);
 
                this.submodelScale = 1;
-               this.maxVisibleRegionFillRatioForAlpha0 = 0.1;
+               this.maxVisibleRegionFillRatioForAlpha0 = 0.4;
                this.minVisibleRegionFillRatioForAlpha1 = 0.8;
                this.renderer = new LogicUIRenderer(submodelModifiable);
 
@@ -135,25 +135,52 @@ 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);
                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));
+               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 / submodelScale, posY / submodelScale, 1 / submodelScale));
+               }
+               if (labelAlpha != 0)
+               {
+                       gc.setAlpha(labelAlpha);
+                       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);
        }
 
+       @Override
+       public boolean clicked(double x, double y)
+       {
+               // TODO
+               double scaledX = (x - getBounds().x) / submodelScale;
+               double scaledY = (y - getBounds().y) / submodelScale;
+               double roundedScaledX = Math.round(scaledX / 5 * 2) * 5 / 2.;
+               double roundedScaledY = Math.round(scaledY / 5 * 2) * 5 / 2.;
+               System.out.println(scaledX + "|" + scaledY + ", rounded " + roundedScaledX + "|" + roundedScaledY);
+               return true;
+       }
+
        private static class PinMovable extends Pin
        {
                public PinMovable(GUIComponent component, int logicWidth, double relX, double relY)