mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:17:35 +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
2 changed files with 32 additions and 6 deletions
|
@ -11,6 +11,7 @@
|
||||||
#include "../Layer.h"
|
#include "../Layer.h"
|
||||||
#include <LibGUI/Action.h>
|
#include <LibGUI/Action.h>
|
||||||
#include <LibGUI/Menu.h>
|
#include <LibGUI/Menu.h>
|
||||||
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGUI/Window.h>
|
#include <LibGUI/Window.h>
|
||||||
|
|
||||||
namespace PixelPaint {
|
namespace PixelPaint {
|
||||||
|
@ -56,6 +57,22 @@ void MoveTool::on_mousemove(Layer* layer, MouseEvent& event)
|
||||||
m_editor->update_tool_cursor();
|
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();
|
auto& image_event = event.image_event();
|
||||||
if (!m_layer_being_moved || m_scaling)
|
if (!m_layer_being_moved || m_scaling)
|
||||||
return;
|
return;
|
||||||
|
@ -80,21 +97,20 @@ void MoveTool::on_mouseup(Layer* layer, MouseEvent& event)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (m_scaling) {
|
if (m_scaling) {
|
||||||
auto& cursor_location = event.image_event().position();
|
m_editor->active_layer()->resize(m_new_layer_size, m_new_scaled_layer_location, Gfx::Painter::ScalingMode::BilinearBlend);
|
||||||
|
|
||||||
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_scaling = false;
|
m_scaling = false;
|
||||||
m_layer_being_moved = nullptr;
|
m_layer_being_moved = nullptr;
|
||||||
|
m_editor->update_tool_cursor();
|
||||||
m_editor->did_complete_action(tool_name());
|
m_editor->did_complete_action(tool_name());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MoveTool::on_keydown(GUI::KeyEvent& event)
|
void MoveTool::on_keydown(GUI::KeyEvent& event)
|
||||||
{
|
{
|
||||||
|
if (event.key() == Key_Shift)
|
||||||
|
m_keep_ascept_ratio = true;
|
||||||
|
|
||||||
if (m_scaling)
|
if (m_scaling)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -128,6 +144,12 @@ void MoveTool::on_keydown(GUI::KeyEvent& event)
|
||||||
m_editor->layers_did_change();
|
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()
|
Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> MoveTool::cursor()
|
||||||
{
|
{
|
||||||
if (m_mouse_in_resize_corner || m_scaling)
|
if (m_mouse_in_resize_corner || m_scaling)
|
||||||
|
|
|
@ -21,6 +21,7 @@ public:
|
||||||
virtual void on_mousemove(Layer*, MouseEvent&) override;
|
virtual void on_mousemove(Layer*, MouseEvent&) override;
|
||||||
virtual void on_mouseup(Layer*, MouseEvent&) override;
|
virtual void on_mouseup(Layer*, MouseEvent&) override;
|
||||||
virtual void on_keydown(GUI::KeyEvent&) override;
|
virtual void on_keydown(GUI::KeyEvent&) override;
|
||||||
|
virtual void on_keyup(GUI::KeyEvent&) override;
|
||||||
virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> cursor() override;
|
virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> cursor() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -29,8 +30,11 @@ private:
|
||||||
RefPtr<Layer> m_layer_being_moved;
|
RefPtr<Layer> m_layer_being_moved;
|
||||||
Gfx::IntPoint m_event_origin;
|
Gfx::IntPoint m_event_origin;
|
||||||
Gfx::IntPoint m_layer_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_scaling { false };
|
||||||
bool m_mouse_in_resize_corner { false };
|
bool m_mouse_in_resize_corner { false };
|
||||||
|
bool m_keep_ascept_ratio { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue