From: Reino Ruusu Date: Mon, 11 Mar 2019 18:17:17 +0000 (+0200) Subject: Fix to zoom level handling in Map UI. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=d0545b1d11197b928b21513dc0cc792185040342;p=simantics%2Fdistrict.git Fix to zoom level handling in Map UI. gitlab #32 Change-Id: If91a547d85d044d2e4cc78c10ee632f4dc5845b1 (cherry picked from commit a94392a0c857026b936436fbf61853b44a2c8d42) --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java index fecc1754..05866bda 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java @@ -2,13 +2,15 @@ package org.simantics.district.network.ui; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; +import java.util.Collection; +import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency; import org.simantics.g2d.canvas.impl.DependencyReflection.Reference; import org.simantics.g2d.diagram.DiagramHints; -import org.simantics.g2d.diagram.DiagramUtils; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.diagram.participant.Selection; import org.simantics.g2d.element.ElementUtils; @@ -89,7 +91,7 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler { if (d == null) return false; - Rectangle2D diagramRect = DiagramUtils.getContentRect(d); + Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(getMapElements(d.getElements())); if (diagramRect == null) return false; if (diagramRect.isEmpty()) @@ -103,6 +105,14 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler { return true; } + protected static List getMapElements(Collection elements) { + List justMapElements = elements.stream() + .filter(e -> "DistrictNetworkEdgeElement".equals(e.getElementClass().getId()) + || "DistrictNetworkVertexElement".equals(e.getElementClass().getId())) + .collect(Collectors.toList()); + return justMapElements; + } + private boolean zoomToPage() { int currentZoomLevel = MapScalingTransform.zoomLevel(util.getTransform()); @@ -124,7 +134,7 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler { // no can do, return zoomToPage(); } - Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(selections); + Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(getMapElements(selections)); // Make sure that even empty bounds can be zoomed into. org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictTransformUtil.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictTransformUtil.java index ce28aba5..e71acf0a 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictTransformUtil.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictTransformUtil.java @@ -14,7 +14,7 @@ public class DistrictTransformUtil extends TransformUtil { @Override public void fitArea(Rectangle2D controlArea, Rectangle2D diagramArea, Margins margins) { if (margins == null) { - MarginUtils.Margin margin = MarginUtils.marginOf(40, 0, 0); + MarginUtils.Margin margin = MarginUtils.marginOf(0, 0, 0); margins = new MarginUtils.Margins(margin, margin, margin, margin); } org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramArea, -1); @@ -38,12 +38,10 @@ public class DistrictTransformUtil extends TransformUtil { if (controlAspectRatio>canvasAspectRatio) { scale = controlArea.getHeight() / diagramArea.getHeight(); - tx = ( controlArea.getWidth() - diagramArea.getWidth() * scale ) / 2; } else // Control is really tall => center canvas vertically, match horizontally { scale = controlArea.getWidth() / diagramArea.getWidth(); - ty = ( controlArea.getHeight() - diagramArea.getHeight() * scale ) / 2; } AffineTransform current = getTransform(); double currentX = current.getScaleX(); @@ -52,30 +50,24 @@ public class DistrictTransformUtil extends TransformUtil { if (targetX > 0) { // let's check that targetX zoomLevel is between 0-20 AffineTransform tar = new AffineTransform(); - tar.scale(toBeX, toBeX); + tar.scale(targetX, targetX); int tarZoomLevel = MapScalingTransform.zoomLevel(tar); if (tarZoomLevel < 20 && tarZoomLevel > 0) { - if (toBeX < targetX) { - while ((toBeX * 0.5) < targetX) { - toBeX = toBeX * 2; - AffineTransform tr = new AffineTransform(); - tr.scale(toBeX, toBeX); - int zoomLevel = MapScalingTransform.zoomLevel(tr); - if (zoomLevel >= 20) { // let's not zoom too close - break; - } - } - } else { - while ((toBeX * 0.25) > targetX) { - toBeX = toBeX * 0.5; - AffineTransform tr = new AffineTransform(); - tr.scale(toBeX, toBeX); - if (MapScalingTransform.zoomLevel(tr) <= 0) { // let's not zoom too far away - break; - } - } - } + toBeX = Math.pow(2.0, tarZoomLevel); } + else { + toBeX = targetX; + } + + if (controlAspectRatio>canvasAspectRatio) { + tx = ( controlArea.getWidth() - diagramArea.getWidth() * toBeX ) / 2; + ty = diagramArea.getHeight() * (targetX - toBeX) / 2; + } + else { + ty = ( controlArea.getHeight() - diagramArea.getHeight() * toBeX ) / 2; + tx = diagramArea.getWidth() * (targetX - toBeX) / 2; + } + AffineTransform at = new AffineTransform(); at.translate(tx, ty); at.translate(controlArea.getMinX(), controlArea.getMinY());