Center visible area to the selection when using mouse wheel to zoom in

and out
This commit is contained in:
Branislav Stojkovic
2018-12-18 19:18:39 +01:00
parent aadc9e394d
commit 99f2482b39
2 changed files with 652 additions and 634 deletions
@@ -1,24 +1,20 @@
/* /*
*
DIY Layout Creator (DIYLC). * DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
Copyright (c) 2009-2018 held jointly by the individual authors. *
* This file is part of DIYLC.
This file is part of DIYLC. *
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
DIYLC is free software: you can redistribute it and/or modify * General Public License as published by the Free Software Foundation, either version 3 of the
it under the terms of the GNU General Public License as published by * License, or (at your option) any later version.
the Free Software Foundation, either version 3 of the License, or *
(at your option) any later version. * DIYLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
DIYLC is distributed in the hope that it will be useful, * Public License for more details.
but WITHOUT ANY WARRANTY; without even the implied warranty of *
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * You should have received a copy of the GNU General Public License along with DIYLC. If not, see
GNU General Public License for more details. * <http://www.gnu.org/licenses/>.
*/
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.swing.plugins.canvas; package org.diylc.swing.plugins.canvas;
import java.awt.Dimension; import java.awt.Dimension;
@@ -36,6 +32,7 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener; import java.awt.event.MouseWheelListener;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
@@ -275,23 +272,40 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
plugInPort.setZoomLevel(newZoom = availableZoomLevels[i]); plugInPort.setZoomLevel(newZoom = availableZoomLevels[i]);
} }
// center to cursor Rectangle2D selectionBounds = plugInPort.getSelectionBounds(true);
Point desiredPos = Rectangle visibleRect = scrollPane.getVisibleRect();
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(); JScrollBar horizontal = scrollPane.getHorizontalScrollBar();
horizontal.setValue(horizontal.getValue() + dx);
JScrollBar vertical = scrollPane.getVerticalScrollBar(); JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue(vertical.getValue() + dy);
} if (e.isShiftDown()) { if (selectionBounds == null) {
// 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;
horizontal.setValue(horizontal.getValue() + dx);
vertical.setValue(vertical.getValue() + dy);
} else {
// center to selection
horizontal.setValue((int) (selectionBounds.getX() + selectionBounds.getWidth() / 2 - visibleRect
.getWidth() / 2));
vertical.setValue((int) (selectionBounds.getY() + selectionBounds.getHeight() / 2 - visibleRect
.getHeight() / 2));
}
}
if (e.isShiftDown()) {
int iScrollAmount = e.getScrollAmount(); int iScrollAmount = e.getScrollAmount();
int iNewValue = horizontalScrollBar.getValue() + horizontalScrollBar.getBlockIncrement() * iScrollAmount * e.getWheelRotation(); int iNewValue =
horizontalScrollBar.getValue() + horizontalScrollBar.getBlockIncrement() * iScrollAmount
* e.getWheelRotation();
if (iNewValue <= horizontalScrollBar.getMaximum()) if (iNewValue <= horizontalScrollBar.getMaximum())
horizontalScrollBar.setValue(iNewValue); horizontalScrollBar.setValue(iNewValue);
} else { } else {
int iScrollAmount = e.getScrollAmount(); int iScrollAmount = e.getScrollAmount();
int iNewValue = verticalScrollBar.getValue() + verticalScrollBar.getBlockIncrement() * iScrollAmount * e.getWheelRotation(); int iNewValue =
verticalScrollBar.getValue() + verticalScrollBar.getBlockIncrement() * iScrollAmount
* e.getWheelRotation();
if (iNewValue <= verticalScrollBar.getMaximum()) if (iNewValue <= verticalScrollBar.getMaximum())
verticalScrollBar.setValue(iNewValue); verticalScrollBar.setValue(iNewValue);
} }
+4
View File
@@ -1970,6 +1970,10 @@
<changeType>NEW_FEATURE</changeType> <changeType>NEW_FEATURE</changeType>
<description>All file export actions (PNG, PDF, BOM...) take the current file name as a default</description> <description>All file export actions (PNG, PDF, BOM...) take the current file name as a default</description>
</org.diylc.appframework.update.Change> </org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Center visible area to the selection when using mouse wheel to zoom in and out</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change> <org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType> <changeType>IMPROVEMENT</changeType>
<description>Change mouse cursor when in "Highlight Connected Areas" mode to avoid confusion</description> <description>Change mouse cursor when in "Highlight Connected Areas" mode to avoid confusion</description>