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.IDiagram;
import org.simantics.g2d.diagram.participant.Selection;
import org.simantics.g2d.element.ElementUtils;
import org.simantics.g2d.element.IElement;
}
private boolean zoomToFit() {
- return false;
+ CanvasBoundsParticipant boundsParticipant = getContext().getAtMostOneItemOfClass(CanvasBoundsParticipant.class);
+ if (boundsParticipant == null)
+ return false;
+
+ final Rectangle2D controlBounds = boundsParticipant.getControlBounds().getFrame();
+ if (controlBounds == null || controlBounds.isEmpty())
+ return false;
+
+ IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
+ if (d == null)
+ return false;
+
+ Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(getMapElements(d.getElements()));
+ if (diagramRect == null)
+ return false;
+ if (diagramRect.isEmpty())
+ return false;
+
+ org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1);
+
+ // System.out.println("zoomToFit(" + controlArea + ", " + diagramRect + ")");
+ util.fitArea(controlBounds, diagramRect, null);
+
+ return true;
+ }
+
+ protected static List<IElement> getMapElements(Collection<IElement> elements) {
+ List<IElement> 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());
-
util.setTransform(new AffineTransform(2,0,0,2,270,270));
-// util.setTransform(new AffineTransform(MapScalingTransform.INSTANCE));
return true;
}
return false;
Set<IElement> selections = selection.getAllSelections();
- Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(selections);
+ if (selections == null || selections.isEmpty()) {
+ // no can do,
+ return zoomToPage();
+ }
+ Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(getMapElements(selections));
// Make sure that even empty bounds can be zoomed into.
org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1);
@Override
public boolean mouseWheelMoved(MouseWheelMovedEvent me) {
if (navigationEnabled && zoomEnabled) {
- double z;
- if (me.wheelRotation > 0) {
- z = DISTRICT_TRANSLATE_AMOUNT;
+ // check if min/max zoom already
+ AffineTransform transform = getTransform();
+ int zoomLevel = MapScalingTransform.zoomLevel(transform);
+
+ if (0 < zoomLevel && zoomLevel < 20 || (zoomLevel == 0 && me.wheelRotation > 0) || (zoomLevel == 20 && me.wheelRotation < 0)) {
+ double z;
+ if (me.wheelRotation > 0) {
+ z = DISTRICT_TRANSLATE_AMOUNT;
+ } else {
+ z = 1.0d / DISTRICT_TRANSLATE_AMOUNT;
+ }
+ //double scroll = Math.min(0.9, -me.wheelRotation / 20.0);
+ //double z = 1 - scroll;
+ double dx = (me.controlPosition.getX() - transform.getTranslateX()) / transform.getScaleX();
+ double dy = (me.controlPosition.getY() - transform.getTranslateY()) / transform.getScaleY();
+ dx = dx * (1 - z);
+ dy = dy * (1 - z);
+// double limitedScale = limitScaleFactor(z);
+// if (limitedScale != 1.0) {
+ translate(dx, dy);
+ scale(z, z);
+ transformChanged();
+ dropQuality();
+ repaint();
+// }
} else {
- z = 1.0d / DISTRICT_TRANSLATE_AMOUNT;
+ // max zoom level reached
}
- //double scroll = Math.min(0.9, -me.wheelRotation / 20.0);
- //double z = 1 - scroll;
- double dx = (me.controlPosition.getX() - transform.getTranslateX()) / transform.getScaleX();
- double dy = (me.controlPosition.getY() - transform.getTranslateY()) / transform.getScaleY();
- dx = dx * (1 - z);
- dy = dy * (1 - z);
-// double limitedScale = limitScaleFactor(z);
-// if (limitedScale != 1.0) {
- translate(dx, dy);
- scale(z, z);
- transformChanged();
- dropQuality();
- repaint();
-// }
}
return false;
}