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.DiagramHints;
11 import org.simantics.g2d.diagram.DiagramUtils;
12 import org.simantics.g2d.diagram.IDiagram;
13 import org.simantics.g2d.diagram.participant.Selection;
14 import org.simantics.g2d.element.ElementUtils;
15 import org.simantics.g2d.element.IElement;
16 import org.simantics.g2d.participant.CanvasBoundsParticipant;
17 import org.simantics.g2d.participant.PanZoomRotateHandler;
18 import org.simantics.maps.MapScalingTransform;
19 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
20 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseWheelMovedEvent;
21 import org.simantics.scenegraph.g2d.events.command.Command;
22 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
23 import org.simantics.scenegraph.g2d.events.command.Commands;
24 import org.simantics.scenegraph.g2d.nodes.NavigationNode;
26 public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
28 public static final int DISTRICT_TRANSLATE_AMOUNT = 2;
30 @Dependency DistrictTransformUtil util;
31 @Reference Selection selection;
33 public DistrictPanZoomRotateHandler() {
37 public void addedToContext(ICanvasContext ctx) {
38 super.addedToContext(ctx);
39 setHint(KEY_TRANSLATE_AMOUNT, DISTRICT_TRANSLATE_AMOUNT);
43 protected Class<? extends NavigationNode> getNavigationNodeClass() {
44 return DistrictNavigationNode.class;
48 public double getTranslateAmount() {
49 return 15 * super.getTranslateAmount();
53 public double limitScaleFactor(double scaleFactor) {
58 @EventHandler(priority = 1)
59 public boolean handleEvent(CommandEvent e) {
61 Command c = e.command;
62 boolean zoomDisabled = Boolean.TRUE.equals(getHint(KEY_DISABLE_ZOOM)) ? true : false;
63 // custom handling of zoom to fit & selection
64 if (Commands.ZOOM_TO_FIT.equals(c) && !zoomDisabled) {
65 boolean result = zoomToFit();
67 result = zoomToPage();
70 if (Commands.ZOOM_TO_PAGE.equals(c) && !zoomDisabled) {
73 if (Commands.ZOOM_TO_SELECTION.equals(c) && !zoomDisabled) {
74 return zoomToSelection();
76 return super.handleEvent(e);
79 private boolean zoomToFit() {
80 CanvasBoundsParticipant boundsParticipant = getContext().getAtMostOneItemOfClass(CanvasBoundsParticipant.class);
81 if (boundsParticipant == null)
84 final Rectangle2D controlBounds = boundsParticipant.getControlBounds().getFrame();
85 if (controlBounds == null || controlBounds.isEmpty())
88 IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
92 Rectangle2D diagramRect = DiagramUtils.getContentRect(d);
93 if (diagramRect == null)
95 if (diagramRect.isEmpty())
98 org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1);
100 // System.out.println("zoomToFit(" + controlArea + ", " + diagramRect + ")");
101 util.fitArea(controlBounds, diagramRect, null);
106 private boolean zoomToPage() {
107 int currentZoomLevel = MapScalingTransform.zoomLevel(util.getTransform());
109 util.setTransform(new AffineTransform(2,0,0,2,270,270));
113 private boolean zoomToSelection() {
114 CanvasBoundsParticipant boundsParticipant = getContext().getAtMostOneItemOfClass(CanvasBoundsParticipant.class);
115 if (boundsParticipant == null)
118 final Rectangle2D controlBounds = boundsParticipant.getControlBounds().getFrame();
119 if (controlBounds == null || controlBounds.isEmpty())
122 Set<IElement> selections = selection.getAllSelections();
123 if (selections == null || selections.isEmpty()) {
127 Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(selections);
129 // Make sure that even empty bounds can be zoomed into.
130 org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1);
132 util.fitArea(controlBounds, diagramRect, null);
136 public static class DistrictNavigationNode extends NavigationNode {
138 private static final long serialVersionUID = 5452897272925816875L;
140 public DistrictNavigationNode() {
141 setAdaptViewportToResizedControl(false);
145 public Double getZoomInLimit() {
146 return super.getZoomInLimit();
150 public Double getZoomOutLimit() {
151 return super.getZoomOutLimit();
155 public void setAdaptViewportToResizedControl(Boolean adapt) {
156 super.setAdaptViewportToResizedControl(false);
161 public boolean getAdaptViewportToResizedControl() {
166 public boolean mouseWheelMoved(MouseWheelMovedEvent me) {
167 if (navigationEnabled && zoomEnabled) {
168 // check if min/max zoom already
169 AffineTransform transform = getTransform();
170 int zoomLevel = MapScalingTransform.zoomLevel(transform);
172 if (0 < zoomLevel && zoomLevel < 20 || (zoomLevel == 0 && me.wheelRotation > 0) || (zoomLevel == 20 && me.wheelRotation < 0)) {
174 if (me.wheelRotation > 0) {
175 z = DISTRICT_TRANSLATE_AMOUNT;
177 z = 1.0d / DISTRICT_TRANSLATE_AMOUNT;
179 //double scroll = Math.min(0.9, -me.wheelRotation / 20.0);
180 //double z = 1 - scroll;
181 double dx = (me.controlPosition.getX() - transform.getTranslateX()) / transform.getScaleX();
182 double dy = (me.controlPosition.getY() - transform.getTranslateY()) / transform.getScaleY();
185 // double limitedScale = limitScaleFactor(z);
186 // if (limitedScale != 1.0) {
194 // max zoom level reached