zoom scroll improvement

This commit is contained in:
bancika
2017-04-12 14:33:14 +02:00
parent 422545ffe2
commit aadeac3f34
@@ -222,36 +222,36 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
// disable scrolling // disable scrolling
scrollPane.setWheelScrollingEnabled(false); scrollPane.setWheelScrollingEnabled(false);
Dimension oldDim = plugInPort.getCanvasDimensions(true);
Point mousePos = getCanvasPanel().getMousePosition(true);
// change zoom level // change zoom level
double d = plugInPort.getZoomLevel(); double oldZoom = plugInPort.getZoomLevel();
double newZoom;
Double[] availableZoomLevels = plugInPort.getAvailableZoomLevels(); Double[] availableZoomLevels = plugInPort.getAvailableZoomLevels();
if (e.getWheelRotation() > 0) { if (e.getWheelRotation() > 0) {
int i = availableZoomLevels.length - 1; int i = availableZoomLevels.length - 1;
while (i > 0 && availableZoomLevels[i] >= d) { while (i > 0 && availableZoomLevels[i] >= oldZoom) {
i--; i--;
} }
plugInPort.setZoomLevel(availableZoomLevels[i]); plugInPort.setZoomLevel(newZoom = availableZoomLevels[i]);
} else { } else {
int i = 0; int i = 0;
while (i < availableZoomLevels.length - 1 && availableZoomLevels[i] <= d) { while (i < availableZoomLevels.length - 1 && availableZoomLevels[i] <= oldZoom) {
i++; i++;
} }
plugInPort.setZoomLevel(availableZoomLevels[i]); plugInPort.setZoomLevel(newZoom = 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);
} }
// 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 { } else {
// enable scrolling // enable scrolling
scrollPane.setWheelScrollingEnabled(true); scrollPane.setWheelScrollingEnabled(true);