1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:17:44 +00:00

PixelPaint: ImageEditor scaling method

Moved the code on mousewheel_event to its own function.
This commit is contained in:
lucastarche 2021-03-22 14:33:30 -03:00 committed by Andreas Kling
parent ca49f96b78
commit 3947d301e5
2 changed files with 26 additions and 18 deletions

View file

@ -249,24 +249,8 @@ void ImageEditor::mouseup_event(GUI::MouseEvent& event)
void ImageEditor::mousewheel_event(GUI::MouseEvent& event)
{
auto old_scale = m_scale;
m_scale += -event.wheel_delta() * 0.1f;
if (m_scale < 0.1f)
m_scale = 0.1f;
if (m_scale > 100.0f)
m_scale = 100.0f;
auto focus_point = Gfx::FloatPoint(
m_pan_origin.x() - ((float)event.x() - (float)width() / 2.0) / old_scale,
m_pan_origin.y() - ((float)event.y() - (float)height() / 2.0) / old_scale);
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()));
if (old_scale != m_scale)
relayout();
auto scale_delta = -event.wheel_delta() * 0.1f;
scale_centered_on_position(event.position(), scale_delta);
}
void ImageEditor::context_menu_event(GUI::ContextMenuEvent& event)
@ -386,6 +370,28 @@ Layer* ImageEditor::layer_at_editor_position(const Gfx::IntPoint& editor_positio
return nullptr;
}
void ImageEditor::scale_centered_on_position(const Gfx::IntPoint& position, float scale_delta)
{
auto old_scale = m_scale;
m_scale += scale_delta;
if (m_scale < 0.1f)
m_scale = 0.1f;
if (m_scale > 100.0f)
m_scale = 100.0f;
auto focus_point = Gfx::FloatPoint(
m_pan_origin.x() - ((float)position.x() - (float)width() / 2.0) / old_scale,
m_pan_origin.y() - ((float)position.y() - (float)height() / 2.0) / old_scale);
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()));
if (old_scale != m_scale)
relayout();
}
void ImageEditor::relayout()
{
if (!image())

View file

@ -63,6 +63,8 @@ public:
Layer* layer_at_editor_position(const Gfx::IntPoint&);
void scale_centered_on_position(const Gfx::IntPoint&, float);
Color primary_color() const { return m_primary_color; }
void set_primary_color(Color);