From 80b78ae6dd79367be4532c6c325ebce625916cf3 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 7 Feb 2014 23:16:34 +0100 Subject: [PATCH] cooja: plugins/Visualizer: Implemented mouse wheel zooming with mouse coordinates as center --- .../contikios/cooja/plugins/Visualizer.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java index 69f5022e9..8926e9af7 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/Visualizer.java @@ -56,6 +56,8 @@ import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; import java.awt.geom.AffineTransform; import java.io.File; import java.io.IOException; @@ -412,6 +414,20 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { repaint(); } }); + canvas.addMouseWheelListener(new MouseWheelListener() { + public void mouseWheelMoved(MouseWheelEvent mwe) { + int x = mwe.getX(); + int y = mwe.getY(); + int rot = mwe.getWheelRotation(); + + if (rot > 0) { + zoomToFactor(zoomFactor() / 1.2, new Point(x, y)); + } else { + zoomToFactor(zoomFactor() * 1.2, new Point(x, y)); + } + + } + }); /* Register mote menu actions */ registerMoteMenuAction(MoveMoteMenuAction.class); @@ -855,16 +871,16 @@ public class Visualizer extends VisPlugin implements HasQuickHelp { } private void zoomToFactor(double newZoom) { - Position center = transformPixelToPosition( - new Point(canvas.getWidth()/2, canvas.getHeight()/2) - ); + zoomToFactor(newZoom, new Point(canvas.getWidth()/2, canvas.getHeight()/2)); + } + + private void zoomToFactor(double newZoom, Point zoomCenter) { + Position center = transformPixelToPosition(zoomCenter); viewportTransform.setToScale( newZoom, newZoom ); - Position newCenter = transformPixelToPosition( - new Point(canvas.getWidth()/2, canvas.getHeight()/2) - ); + Position newCenter = transformPixelToPosition(zoomCenter); viewportTransform.translate( newCenter.getXCoordinate() - center.getXCoordinate(), newCenter.getYCoordinate() - center.getYCoordinate()