2020-05-12 19:08:47 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-02-10 16:28:48 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-05-12 19:08:47 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-12 19:08:47 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MoveTool.h"
|
2021-09-17 04:54:55 -03:00
|
|
|
#include "../Image.h"
|
|
|
|
|
#include "../ImageEditor.h"
|
|
|
|
|
#include "../Layer.h"
|
2020-05-13 17:03:29 -03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
#include <LibGUI/Menu.h>
|
2022-08-13 11:44:18 -03:00
|
|
|
#include <LibGUI/Painter.h>
|
2020-05-12 19:16:54 -03:00
|
|
|
#include <LibGUI/Window.h>
|
2020-05-12 19:08:47 -03:00
|
|
|
|
2020-05-20 15:35:35 -03:00
|
|
|
namespace PixelPaint {
|
2020-05-12 19:08:47 -03:00
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
void MoveTool::on_mousedown(Layer* layer, MouseEvent& event)
|
2020-05-12 19:08:47 -03:00
|
|
|
{
|
2022-01-08 06:44:19 -03:00
|
|
|
if (event.image_event().button() == GUI::MouseButton::Secondary) {
|
|
|
|
|
m_editor->start_panning(event.raw_event().position());
|
2021-09-12 00:26:09 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
if (!layer)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-08-25 05:06:00 -03:00
|
|
|
auto& layer_event = event.layer_event();
|
|
|
|
|
auto& image_event = event.image_event();
|
2021-10-27 08:20:27 -03:00
|
|
|
if (layer_event.button() != GUI::MouseButton::Primary)
|
2020-05-12 19:08:47 -03:00
|
|
|
return;
|
2022-08-08 09:34:50 -03:00
|
|
|
if (!layer->rect().contains(layer_event.position()) && !m_mouse_in_resize_corner)
|
2020-05-12 19:08:47 -03:00
|
|
|
return;
|
2022-08-08 09:34:50 -03:00
|
|
|
m_scaling = m_mouse_in_resize_corner;
|
2021-08-25 05:07:24 -03:00
|
|
|
m_layer_being_moved = *layer;
|
2020-05-21 16:57:57 -03:00
|
|
|
m_event_origin = image_event.position();
|
2021-08-25 05:07:24 -03:00
|
|
|
m_layer_origin = layer->location();
|
2020-05-12 19:08:47 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
void MoveTool::on_mousemove(Layer* layer, MouseEvent& event)
|
2020-05-12 19:08:47 -03:00
|
|
|
{
|
2022-01-08 06:44:19 -03:00
|
|
|
if (m_editor->is_panning()) {
|
|
|
|
|
m_editor->pan_to(event.raw_event().position());
|
2021-09-12 00:26:09 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
if (!layer)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-08-08 09:34:50 -03:00
|
|
|
constexpr int sensitivity = 20;
|
|
|
|
|
Gfx::IntPoint grab_rect_position = Gfx::IntPoint(layer->location().x() + layer->size().width() - sensitivity / 2, layer->location().y() + layer->size().height() - sensitivity / 2);
|
|
|
|
|
Gfx::IntRect grab_rect = Gfx::IntRect(grab_rect_position, Gfx::IntSize(sensitivity, sensitivity));
|
|
|
|
|
auto updated_is_in_lower_right_corner = grab_rect.contains(event.image_event().position()); // check if the mouse is in the lower right corner
|
|
|
|
|
if (m_mouse_in_resize_corner != updated_is_in_lower_right_corner) {
|
|
|
|
|
m_mouse_in_resize_corner = updated_is_in_lower_right_corner;
|
|
|
|
|
m_editor->update_tool_cursor();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 11:44:18 -03:00
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:06:00 -03:00
|
|
|
auto& image_event = event.image_event();
|
2022-08-08 09:34:50 -03:00
|
|
|
if (!m_layer_being_moved || m_scaling)
|
2020-05-12 19:08:47 -03:00
|
|
|
return;
|
2020-05-21 16:57:57 -03:00
|
|
|
auto delta = image_event.position() - m_event_origin;
|
2020-05-12 19:08:47 -03:00
|
|
|
m_layer_being_moved->set_location(m_layer_origin.translated(delta));
|
2020-05-12 19:21:13 -03:00
|
|
|
m_editor->layers_did_change();
|
2020-05-12 19:08:47 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
void MoveTool::on_mouseup(Layer* layer, MouseEvent& event)
|
2020-05-12 19:08:47 -03:00
|
|
|
{
|
2022-01-08 06:44:19 -03:00
|
|
|
if (event.image_event().button() == GUI::MouseButton::Secondary) {
|
|
|
|
|
m_editor->stop_panning();
|
2021-09-12 00:26:09 -03:00
|
|
|
m_editor->set_override_cursor(cursor());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
if (!layer)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-08-25 05:06:00 -03:00
|
|
|
auto& layer_event = event.layer_event();
|
2021-10-27 08:20:27 -03:00
|
|
|
if (layer_event.button() != GUI::MouseButton::Primary)
|
2020-05-12 19:08:47 -03:00
|
|
|
return;
|
2022-08-08 09:34:50 -03:00
|
|
|
|
|
|
|
|
if (m_scaling) {
|
2022-08-13 11:44:18 -03:00
|
|
|
m_editor->active_layer()->resize(m_new_layer_size, m_new_scaled_layer_location, Gfx::Painter::ScalingMode::BilinearBlend);
|
2022-08-08 09:34:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_scaling = false;
|
2020-05-12 19:08:47 -03:00
|
|
|
m_layer_being_moved = nullptr;
|
2022-08-13 11:44:18 -03:00
|
|
|
m_editor->update_tool_cursor();
|
2022-08-21 15:33:03 -03:00
|
|
|
m_editor->did_complete_action(tool_name());
|
2020-05-12 19:08:47 -03:00
|
|
|
}
|
|
|
|
|
|
2020-05-13 08:20:54 -03:00
|
|
|
void MoveTool::on_keydown(GUI::KeyEvent& event)
|
|
|
|
|
{
|
2022-08-13 11:44:18 -03:00
|
|
|
if (event.key() == Key_Shift)
|
|
|
|
|
m_keep_ascept_ratio = true;
|
|
|
|
|
|
2022-08-08 09:34:50 -03:00
|
|
|
if (m_scaling)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-05-13 08:20:54 -03:00
|
|
|
if (event.modifiers() != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto* layer = m_editor->active_layer();
|
|
|
|
|
if (!layer)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto new_location = layer->location();
|
|
|
|
|
|
|
|
|
|
switch (event.key()) {
|
|
|
|
|
case Key_Up:
|
2021-04-12 15:47:09 -03:00
|
|
|
new_location.translate_by(0, -1);
|
2020-05-13 08:20:54 -03:00
|
|
|
break;
|
|
|
|
|
case Key_Down:
|
2021-04-12 15:47:09 -03:00
|
|
|
new_location.translate_by(0, 1);
|
2020-05-13 08:20:54 -03:00
|
|
|
break;
|
|
|
|
|
case Key_Left:
|
2021-04-12 15:47:09 -03:00
|
|
|
new_location.translate_by(-1, 0);
|
2020-05-13 08:20:54 -03:00
|
|
|
break;
|
|
|
|
|
case Key_Right:
|
2021-04-12 15:47:09 -03:00
|
|
|
new_location.translate_by(1, 0);
|
2020-05-13 08:20:54 -03:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
layer->set_location(new_location);
|
|
|
|
|
m_editor->layers_did_change();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 11:44:18 -03:00
|
|
|
void MoveTool::on_keyup(GUI::KeyEvent& event)
|
|
|
|
|
{
|
|
|
|
|
if (event.key() == Key_Shift)
|
|
|
|
|
m_keep_ascept_ratio = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 09:34:50 -03:00
|
|
|
Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> MoveTool::cursor()
|
|
|
|
|
{
|
|
|
|
|
if (m_mouse_in_resize_corner || m_scaling)
|
|
|
|
|
return Gfx::StandardCursor::ResizeDiagonalTLBR;
|
|
|
|
|
return Gfx::StandardCursor::Move;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 19:08:47 -03:00
|
|
|
}
|