From 9013d0fe3800c926572abdcb5f93b365817c8995 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Mon, 22 Nov 2021 00:33:55 +0100 Subject: [PATCH] PixelPaint: Add ability to fit to either width, height, or image --- Userland/Applications/PixelPaint/ImageEditor.cpp | 14 ++++++++++++-- Userland/Applications/PixelPaint/ImageEditor.h | 8 +++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp index f3723ee77f..6cca19abbe 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.cpp +++ b/Userland/Applications/PixelPaint/ImageEditor.cpp @@ -610,7 +610,7 @@ void ImageEditor::set_pan_origin(Gfx::FloatPoint const& pan_origin) relayout(); } -void ImageEditor::fit_image_to_view() +void ImageEditor::fit_image_to_view(FitType type) { auto viewport_rect = rect(); m_pan_origin = Gfx::FloatPoint(0, 0); @@ -628,7 +628,17 @@ void ImageEditor::fit_image_to_view() auto image_size = image().size(); auto height_ratio = floorf(border_ratio * viewport_rect.height()) / (float)image_size.height(); auto width_ratio = floorf(border_ratio * viewport_rect.width()) / (float)image_size.width(); - set_absolute_scale(min(height_ratio, width_ratio), false); + switch (type) { + case FitType::Width: + set_absolute_scale(width_ratio, false); + break; + case FitType::Height: + set_absolute_scale(height_ratio, false); + break; + case FitType::Image: + set_absolute_scale(min(height_ratio, width_ratio), false); + break; + } float offset = m_show_rulers ? -m_ruler_thickness / (m_scale * 2.0f) : 0.0f; m_pan_origin = Gfx::FloatPoint(offset, offset); diff --git a/Userland/Applications/PixelPaint/ImageEditor.h b/Userland/Applications/PixelPaint/ImageEditor.h index 45dd29d764..4a1701588b 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.h +++ b/Userland/Applications/PixelPaint/ImageEditor.h @@ -54,9 +54,15 @@ public: Layer* layer_at_editor_position(Gfx::IntPoint const&); + enum class FitType { + Width, + Height, + Image + }; + float scale() const { return m_scale; } void scale_centered_on_position(Gfx::IntPoint const&, float); - void fit_image_to_view(); + void fit_image_to_view(FitType type = FitType::Image); void reset_scale_and_position(); void scale_by(float); void set_absolute_scale(float, bool do_relayout = true);