Przeglądaj źródła

PixelPaint: Allow layer to "scale" location when resizing

Andrew Smith 3 lat temu
rodzic
commit
bccf0a9346

+ 13 - 2
Userland/Applications/PixelPaint/Image.cpp

@@ -529,13 +529,24 @@ void Image::crop(Gfx::IntRect const& cropped_rect)
 
 void Image::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode)
 {
+    float scale_x = 1.0f;
+    float scale_y = 1.0f;
+
+    if (size().width() != 0.0f) {
+        scale_x = new_size.width() / static_cast<float>(size().width());
+    }
+
+    if (size().height() != 0.0f) {
+        scale_y = new_size.height() / static_cast<float>(size().height());
+    }
+
     for (auto& layer : m_layers) {
-        layer.resize(new_size, scaling_mode);
+        Gfx::IntPoint new_location(scale_x * layer.location().x(), scale_y * layer.location().y());
+        layer.resize(new_size, new_location, scaling_mode);
     }
 
     m_size = { new_size.width(), new_size.height() };
     did_change_rect();
-
 }
 
 Color Image::color_at(Gfx::IntPoint const& point) const

+ 22 - 11
Userland/Applications/PixelPaint/Layer.cpp

@@ -180,31 +180,42 @@ void Layer::crop(Gfx::IntRect const& rect)
     did_modify_bitmap();
 }
 
-void Layer::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode)
+void Layer::resize(Gfx::IntSize const& new_size, Gfx::IntPoint const& new_location, Gfx::Painter::ScalingMode scaling_mode)
 {
-    const Gfx::IntRect old_rect(Gfx::IntPoint(0, 0), size());
-    const Gfx::IntRect new_rect(Gfx::IntPoint(0, 0), new_size);
+    auto src_rect = Gfx::IntRect(Gfx::IntPoint(0, 0), size());
+    auto dst_rect = Gfx::IntRect(Gfx::IntPoint(0, 0), new_size);
 
     {
-        auto resized = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors();
-        Gfx::Painter painter(resized);
+        auto dst = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors();
+        Gfx::Painter painter(dst);
 
-        painter.draw_scaled_bitmap(new_rect, *m_content_bitmap, old_rect, 1.0f, scaling_mode);
+        painter.draw_scaled_bitmap(dst_rect, *m_content_bitmap, src_rect, 1.0f, scaling_mode);
 
-        m_content_bitmap = move(resized);
+        m_content_bitmap = move(dst);
     }
 
     if (m_mask_bitmap) {
-        auto resized = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors();
-        Gfx::Painter painter(resized);
+        auto dst = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors();
+        Gfx::Painter painter(dst);
 
-        painter.draw_scaled_bitmap(new_rect, *m_mask_bitmap, old_rect, 1.0f, scaling_mode);
-        m_mask_bitmap = move(resized);
+        painter.draw_scaled_bitmap(dst_rect, *m_mask_bitmap, src_rect, 1.0f, scaling_mode);
+        m_mask_bitmap = move(dst);
     }
 
+    set_location(new_location);
     did_modify_bitmap();
 }
 
+void Layer::resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode)
+{
+    resize(new_rect.size(), new_rect.location(), scaling_mode);
+}
+
+void Layer::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode)
+{
+    resize(new_size, location(), scaling_mode);
+}
+
 void Layer::update_cached_bitmap()
 {
     if (!is_masked()) {

+ 2 - 0
Userland/Applications/PixelPaint/Layer.h

@@ -58,6 +58,8 @@ public:
     void rotate(Gfx::RotationDirection direction);
     void crop(Gfx::IntRect const& rect);
     void resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode);
+    void resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode);
+    void resize(Gfx::IntSize const& new_size, Gfx::IntPoint const& new_location, Gfx::Painter::ScalingMode scaling_mode);
 
     ErrorOr<void> try_set_bitmaps(NonnullRefPtr<Gfx::Bitmap> content, RefPtr<Gfx::Bitmap> mask);