summaryrefslogtreecommitdiff
path: root/Userland/Applications
diff options
context:
space:
mode:
authorTobias Christiansen <tobyase@serenityos.org>2021-09-12 14:03:17 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-12 16:30:09 +0200
commitacf3e230b0ae0bf992e6b94768f0c5f1a78abca0 (patch)
treeb5b1e520c9c2eb8ddb8819cb2b46f926e1a464e6 /Userland/Applications
parent8ddce2faaff8405fc1435749631d1926767f9826 (diff)
downloadserenity-acf3e230b0ae0bf992e6b94768f0c5f1a78abca0.zip
PixelPaint: Fix zooming to cursor
This patch fixes the behavior of zooming. Now the cursor stays over the same spot in the image when zooming.
Diffstat (limited to 'Userland/Applications')
-rw-r--r--Userland/Applications/PixelPaint/ImageEditor.cpp30
1 files changed, 19 insertions, 11 deletions
diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp
index d34f7b3d51..aad67cccc3 100644
--- a/Userland/Applications/PixelPaint/ImageEditor.cpp
+++ b/Userland/Applications/PixelPaint/ImageEditor.cpp
@@ -320,8 +320,8 @@ void ImageEditor::mousemove_event(GUI::MouseEvent& event)
if (event.buttons() & GUI::MouseButton::Middle) {
auto delta = event.position() - m_click_position;
m_pan_origin = m_saved_pan_origin.translated(
- -delta.x() / m_scale,
- -delta.y() / m_scale);
+ -delta.x(),
+ -delta.y());
relayout();
return;
@@ -537,16 +537,24 @@ void ImageEditor::clamped_scale(float scale_delta)
void ImageEditor::scale_centered_on_position(Gfx::IntPoint const& position, float scale_delta)
{
auto old_scale = m_scale;
- clamped_scale(scale_delta);
+ auto image_coord_of_position = editor_position_to_image_position(position);
- Gfx::FloatPoint focus_point {
- m_pan_origin.x() - (position.x() - width() / 2.0f) / old_scale,
- m_pan_origin.y() - (position.y() - height() / 2.0f) / old_scale
+ auto image_size = m_image->size();
+ Gfx::FloatPoint offset_from_center_in_image_coords = {
+ image_coord_of_position.x() - image_size.width() / 2.0f,
+ image_coord_of_position.y() - image_size.height() / 2.0f
+ };
+ Gfx::FloatPoint offset_from_center_in_editor_coords = {
+ position.x() - width() / 2.0f,
+ position.y() - height() / 2.0f
};
- m_pan_origin = Gfx::FloatPoint(
- focus_point.x() - m_scale / old_scale * (focus_point.x() - m_pan_origin.x()),
- focus_point.y() - m_scale / old_scale * (focus_point.y() - m_pan_origin.y()));
+ clamped_scale(scale_delta);
+
+ m_pan_origin = {
+ offset_from_center_in_image_coords.x() * m_scale - offset_from_center_in_editor_coords.x(),
+ offset_from_center_in_image_coords.y() * m_scale - offset_from_center_in_editor_coords.y()
+ };
if (old_scale != m_scale)
relayout();
@@ -603,8 +611,8 @@ void ImageEditor::relayout()
m_editor_image_rect.set_size(new_size);
Gfx::IntPoint new_location;
- new_location.set_x((width() / 2) - (new_size.width() / 2) - (m_pan_origin.x() * m_scale));
- new_location.set_y((height() / 2) - (new_size.height() / 2) - (m_pan_origin.y() * m_scale));
+ new_location.set_x((width() / 2) - (new_size.width() / 2) - (m_pan_origin.x()));
+ new_location.set_y((height() / 2) - (new_size.height() / 2) - (m_pan_origin.y()));
m_editor_image_rect.set_location(new_location);
update();