Skip to content

Commit

Permalink
Improve map rotation by mouse event (#5825)
Browse files Browse the repository at this point in the history
  • Loading branch information
qsavoye authored Dec 15, 2024
1 parent 4903162 commit 2ead9dc
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/qml/MapCanvas.qml
Original file line number Diff line number Diff line change
Expand Up @@ -355,30 +355,40 @@ Item {
grabPermissions: PointerHandler.TakeOverForbidden
acceptedButtons: Qt.MiddleButton

property real oldTranslationY: 0
property bool translationThresholdReached: false
property real pressClickX: 0
property real pressClickY: 0
property real screenCenterX: 0
property real screenCenterY: 0
property real lastRotateAngle: 0

onActiveChanged: {
if (active) {
freeze('rotate');
oldTranslationY = 0;
translationThresholdReached = false;
pressClickX = centroid.position.x;
pressClickY = centroid.position.y;
screenCenterX = width / 2;
screenCenterY = height / 2;
lastRotateAngle = 0;
} else {
unfreeze('rotate');
}
}

onTranslationChanged: {
if (active) {
if (translationThresholdReached) {
if (oldTranslationY != 0) {
mapCanvasWrapper.rotate(oldTranslationY - translation.y);
if (lastRotateAngle != 0) {
let newPositionX = pressClickX + translation.x;
let newPositionY = pressClickY + translation.y;
let angle = Math.atan2(newPositionY - screenCenterY, newPositionX - screenCenterX) - Math.atan2(pressClickY - screenCenterY, pressClickX - screenCenterX);
if (angle != 0) {
mapCanvasWrapper.rotate(angle * 180 / Math.PI - lastRotateAngle);
}
oldTranslationY = translation.y;
translationThresholdReached = true;
} else if (Math.abs(oldTranslationY - translation.y) > pinchHandler.rotationTreshold) {
oldTranslationY = translation.y;
translationThresholdReached = true;
lastRotateAngle = angle * 180 / Math.PI;
} else {
let newPositionX = pressClickX + translation.x;
let newPositionY = pressClickY + translation.y;
let angle = Math.atan2(newPositionY - screenCenterY, newPositionX - screenCenterX) - Math.atan2(pressClickY - screenCenterY, pressClickX - screenCenterX);
lastRotateAngle = angle * 180 / Math.PI;
}
}
}
Expand Down

1 comment on commit 2ead9dc

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.