diff options
author | Tommaso Peduzzi <tommaso.peduzzi@gmx.ch> | 2022-08-13 16:44:18 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-16 22:53:07 +0200 |
commit | f547b9be7ba7186980b3890d69ad6e9607e579f9 (patch) | |
tree | 8b75b7d74cab579586fa79331dbac1aa6b8588e9 | |
parent | 293ab2cdc9a0420bee7491481fcf70f7cfa53752 (diff) | |
download | serenity-f547b9be7ba7186980b3890d69ad6e9607e579f9.zip |
PixelPaint: Scale layer and preserve aspect ratio
This patch adds the ability to scale a layer and
preserve the aspect ratio.
When the Shift key is pressed, the aspect ratio is preserved.
-rw-r--r-- | Userland/Applications/PixelPaint/Tools/MoveTool.cpp | 34 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Tools/MoveTool.h | 4 |
2 files changed, 32 insertions, 6 deletions
diff --git a/Userland/Applications/PixelPaint/Tools/MoveTool.cpp b/Userland/Applications/PixelPaint/Tools/MoveTool.cpp index c5e9e7bc3c..7fb00a714e 100644 --- a/Userland/Applications/PixelPaint/Tools/MoveTool.cpp +++ b/Userland/Applications/PixelPaint/Tools/MoveTool.cpp @@ -11,6 +11,7 @@ #include "../Layer.h" #include <LibGUI/Action.h> #include <LibGUI/Menu.h> +#include <LibGUI/Painter.h> #include <LibGUI/Window.h> namespace PixelPaint { @@ -56,6 +57,22 @@ void MoveTool::on_mousemove(Layer* layer, MouseEvent& event) m_editor->update_tool_cursor(); } + if (m_scaling) { + auto& cursor_location = event.image_event().position(); + auto width = abs(m_layer_being_moved->location().x() - cursor_location.x()); + auto height = abs(m_layer_being_moved->location().y() - cursor_location.y()); + if (m_keep_ascept_ratio) { + if (abs(width - m_layer_being_moved->size().width()) > abs(height - m_layer_being_moved->size().height())) { + height = width * m_layer_being_moved->size().height() / m_layer_being_moved->size().width(); + } else { + width = height * m_layer_being_moved->size().width() / m_layer_being_moved->size().height(); + } + } + m_new_layer_size = Gfx::IntSize(width, height); + // TODO: Change this according to which direction the user is scaling + m_new_scaled_layer_location = Gfx::IntPoint(m_layer_being_moved->location().x(), m_layer_being_moved->location().y()); + } + auto& image_event = event.image_event(); if (!m_layer_being_moved || m_scaling) return; @@ -80,21 +97,20 @@ void MoveTool::on_mouseup(Layer* layer, MouseEvent& event) return; if (m_scaling) { - auto& cursor_location = event.image_event().position(); - - auto new_size = Gfx::IntSize(abs(m_layer_being_moved->location().x() - cursor_location.x()), abs(m_layer_being_moved->location().y() - cursor_location.y())); - // TODO: Change this according to which direction the user is scaling - auto new_location = Gfx::IntPoint(m_layer_being_moved->location().x(), m_layer_being_moved->location().y()); - m_editor->active_layer()->resize(new_size, new_location, Gfx::Painter::ScalingMode::BilinearBlend); + m_editor->active_layer()->resize(m_new_layer_size, m_new_scaled_layer_location, Gfx::Painter::ScalingMode::BilinearBlend); } m_scaling = false; m_layer_being_moved = nullptr; + m_editor->update_tool_cursor(); m_editor->did_complete_action(tool_name()); } void MoveTool::on_keydown(GUI::KeyEvent& event) { + if (event.key() == Key_Shift) + m_keep_ascept_ratio = true; + if (m_scaling) return; @@ -128,6 +144,12 @@ void MoveTool::on_keydown(GUI::KeyEvent& event) m_editor->layers_did_change(); } +void MoveTool::on_keyup(GUI::KeyEvent& event) +{ + if (event.key() == Key_Shift) + m_keep_ascept_ratio = false; +} + Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> MoveTool::cursor() { if (m_mouse_in_resize_corner || m_scaling) diff --git a/Userland/Applications/PixelPaint/Tools/MoveTool.h b/Userland/Applications/PixelPaint/Tools/MoveTool.h index 60d0502785..16b601397b 100644 --- a/Userland/Applications/PixelPaint/Tools/MoveTool.h +++ b/Userland/Applications/PixelPaint/Tools/MoveTool.h @@ -21,6 +21,7 @@ public: virtual void on_mousemove(Layer*, MouseEvent&) override; virtual void on_mouseup(Layer*, MouseEvent&) override; virtual void on_keydown(GUI::KeyEvent&) override; + virtual void on_keyup(GUI::KeyEvent&) override; virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> cursor() override; private: @@ -29,8 +30,11 @@ private: RefPtr<Layer> m_layer_being_moved; Gfx::IntPoint m_event_origin; Gfx::IntPoint m_layer_origin; + Gfx::IntPoint m_new_scaled_layer_location; + Gfx::IntSize m_new_layer_size; bool m_scaling { false }; bool m_mouse_in_resize_corner { false }; + bool m_keep_ascept_ratio { false }; }; } |