mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 05:20:30 +00:00
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.
This commit is contained in:
parent
293ab2cdc9
commit
f547b9be7b
Notes:
sideshowbarker
2024-07-17 21:26:19 +09:00
Author: https://github.com/tommasopeduzzi Commit: https://github.com/SerenityOS/serenity/commit/f547b9be7b Pull-request: https://github.com/SerenityOS/serenity/pull/14806 Reviewed-by: https://github.com/MacDue Reviewed-by: https://github.com/TobyAsE ✅
2 changed files with 32 additions and 6 deletions
|
@ -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)
|
||||
|
|
|
@ -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 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue