1 package org.simantics.district.network.ui;
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Rectangle2D;
7 import org.simantics.g2d.canvas.ICanvasContext;
8 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
9 import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;
10 import org.simantics.g2d.diagram.participant.Selection;
11 import org.simantics.g2d.element.ElementUtils;
12 import org.simantics.g2d.element.IElement;
13 import org.simantics.g2d.participant.CanvasBoundsParticipant;
14 import org.simantics.g2d.participant.PanZoomRotateHandler;
15 import org.simantics.maps.MapScalingTransform;
16 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
17 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseWheelMovedEvent;
18 import org.simantics.scenegraph.g2d.events.command.Command;
19 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
20 import org.simantics.scenegraph.g2d.events.command.Commands;
21 import org.simantics.scenegraph.g2d.nodes.NavigationNode;
23 public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
25 public static final int DISTRICT_TRANSLATE_AMOUNT = 2;
27 @Dependency DistrictTransformUtil util;
28 @Reference Selection selection;
30 public DistrictPanZoomRotateHandler() {
34 public void addedToContext(ICanvasContext ctx) {
35 super.addedToContext(ctx);
36 setHint(KEY_TRANSLATE_AMOUNT, DISTRICT_TRANSLATE_AMOUNT);
40 protected Class<? extends NavigationNode> getNavigationNodeClass() {
41 return DistrictNavigationNode.class;
45 public double getTranslateAmount() {
46 return 15 * super.getTranslateAmount();
50 public double limitScaleFactor(double scaleFactor) {
55 @EventHandler(priority = 1)
56 public boolean handleEvent(CommandEvent e) {
58 Command c = e.command;
59 boolean zoomDisabled = Boolean.TRUE.equals(getHint(KEY_DISABLE_ZOOM)) ? true : false;
60 // custom handling of zoom to fit & selection
61 if (Commands.ZOOM_TO_FIT.equals(c) && !zoomDisabled) {
62 boolean result = zoomToFit();
64 result = zoomToPage();
67 if (Commands.ZOOM_TO_PAGE.equals(c) && !zoomDisabled) {
70 if (Commands.ZOOM_TO_SELECTION.equals(c) && !zoomDisabled) {
71 return zoomToSelection();
73 return super.handleEvent(e);
76 private boolean zoomToFit() {
80 private boolean zoomToPage() {
81 int currentZoomLevel = MapScalingTransform.zoomLevel(util.getTransform());
83 util.setTransform(new AffineTransform(2,0,0,2,270,270));
84 // util.setTransform(new AffineTransform(MapScalingTransform.INSTANCE));
88 private boolean zoomToSelection() {
89 CanvasBoundsParticipant boundsParticipant = getContext().getAtMostOneItemOfClass(CanvasBoundsParticipant.class);
90 if (boundsParticipant == null)
93 final Rectangle2D controlBounds = boundsParticipant.getControlBounds().getFrame();
94 if (controlBounds == null || controlBounds.isEmpty())
97 Set<IElement> selections = selection.getAllSelections();
98 Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(selections);
100 // Make sure that even empty bounds can be zoomed into.
101 org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1);
103 util.fitArea(controlBounds, diagramRect, null);
107 public static class DistrictNavigationNode extends NavigationNode {
109 private static final long serialVersionUID = 5452897272925816875L;
111 public DistrictNavigationNode() {
112 setAdaptViewportToResizedControl(false);
116 public Double getZoomInLimit() {
117 return super.getZoomInLimit();
121 public Double getZoomOutLimit() {
122 return super.getZoomOutLimit();
126 public void setAdaptViewportToResizedControl(Boolean adapt) {
127 super.setAdaptViewportToResizedControl(false);
132 public boolean getAdaptViewportToResizedControl() {
137 public boolean mouseWheelMoved(MouseWheelMovedEvent me) {
138 if (navigationEnabled && zoomEnabled) {
140 if (me.wheelRotation > 0) {
141 z = DISTRICT_TRANSLATE_AMOUNT;
143 z = 1.0d / DISTRICT_TRANSLATE_AMOUNT;
145 //double scroll = Math.min(0.9, -me.wheelRotation / 20.0);
146 //double z = 1 - scroll;
147 double dx = (me.controlPosition.getX() - transform.getTranslateX()) / transform.getScaleX();
148 double dy = (me.controlPosition.getY() - transform.getTranslateY()) / transform.getScaleY();
151 // double limitedScale = limitScaleFactor(z);
152 // if (limitedScale != 1.0) {