From aadeac3f34a1099bbb074cde8b29ae79431b7ed3 Mon Sep 17 00:00:00 2001 From: bancika Date: Wed, 12 Apr 2017 14:33:14 +0200 Subject: [PATCH] zoom scroll improvement --- .../swing/plugins/canvas/CanvasPlugin.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/diylc/diylc-swing/src/org/diylc/swing/plugins/canvas/CanvasPlugin.java b/diylc/diylc-swing/src/org/diylc/swing/plugins/canvas/CanvasPlugin.java index d1236e6d..9fbb040d 100644 --- a/diylc/diylc-swing/src/org/diylc/swing/plugins/canvas/CanvasPlugin.java +++ b/diylc/diylc-swing/src/org/diylc/swing/plugins/canvas/CanvasPlugin.java @@ -222,36 +222,36 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner { // disable scrolling scrollPane.setWheelScrollingEnabled(false); - Dimension oldDim = plugInPort.getCanvasDimensions(true); + + Point mousePos = getCanvasPanel().getMousePosition(true); // change zoom level - double d = plugInPort.getZoomLevel(); + double oldZoom = plugInPort.getZoomLevel(); + double newZoom; Double[] availableZoomLevels = plugInPort.getAvailableZoomLevels(); if (e.getWheelRotation() > 0) { int i = availableZoomLevels.length - 1; - while (i > 0 && availableZoomLevels[i] >= d) { + while (i > 0 && availableZoomLevels[i] >= oldZoom) { i--; } - plugInPort.setZoomLevel(availableZoomLevels[i]); + plugInPort.setZoomLevel(newZoom = availableZoomLevels[i]); } else { int i = 0; - while (i < availableZoomLevels.length - 1 && availableZoomLevels[i] <= d) { + while (i < availableZoomLevels.length - 1 && availableZoomLevels[i] <= oldZoom) { i++; } - plugInPort.setZoomLevel(availableZoomLevels[i]); - // center to cursor - Point mousePos = getCanvasPanel().getMousePosition(true); - Dimension newDim = plugInPort.getCanvasDimensions(true); - Point desiredPos = - new Point((int) (1d * mousePos.x / oldDim.width * newDim.width), (int) (1d * mousePos.y - / oldDim.height * newDim.height)); - int dx = desiredPos.x - mousePos.x; - int dy = desiredPos.y - mousePos.y; - JScrollBar horizontal = scrollPane.getHorizontalScrollBar(); - horizontal.setValue(horizontal.getValue() + dx); - JScrollBar vertical = scrollPane.getVerticalScrollBar(); - vertical.setValue(vertical.getValue() + dy); + plugInPort.setZoomLevel(newZoom = availableZoomLevels[i]); } + + // center to cursor + Point desiredPos = + new Point((int) (1d * mousePos.x / oldZoom * newZoom), (int) (1d * mousePos.y / oldZoom * newZoom)); + int dx = desiredPos.x - mousePos.x; + int dy = desiredPos.y - mousePos.y; + JScrollBar horizontal = scrollPane.getHorizontalScrollBar(); + horizontal.setValue(horizontal.getValue() + dx); + JScrollBar vertical = scrollPane.getVerticalScrollBar(); + vertical.setValue(vertical.getValue() + dy); } else { // enable scrolling scrollPane.setWheelScrollingEnabled(true);