2020-05-12 19:08:47 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
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>
|
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
|
|
|
|
|
|
|
|
MoveTool::MoveTool()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MoveTool::~MoveTool()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
void MoveTool::on_mousedown(Layer* layer, MouseEvent& event)
|
2020-05-12 19:08:47 -03:00
|
|
|
{
|
2021-09-12 00:26:09 -03:00
|
|
|
if (event.image_event().button() == GUI::MouseButton::Right && !m_is_panning) {
|
|
|
|
|
m_is_panning = true;
|
|
|
|
|
m_event_origin = event.raw_event().position();
|
|
|
|
|
m_saved_pan_origin = m_editor->pan_origin();
|
|
|
|
|
m_editor->set_override_cursor(Gfx::StandardCursor::Drag);
|
|
|
|
|
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();
|
|
|
|
|
if (layer_event.button() != GUI::MouseButton::Left)
|
2020-05-12 19:08:47 -03:00
|
|
|
return;
|
2021-08-25 05:07:24 -03:00
|
|
|
if (!layer->rect().contains(layer_event.position()))
|
2020-05-12 19:08:47 -03:00
|
|
|
return;
|
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
|
|
|
{
|
2021-09-12 00:26:09 -03:00
|
|
|
if (m_is_panning) {
|
|
|
|
|
auto& raw_event = event.raw_event();
|
|
|
|
|
auto delta = raw_event.position() - m_event_origin;
|
|
|
|
|
m_editor->set_pan_origin(m_saved_pan_origin.translated(
|
|
|
|
|
-delta.x(),
|
|
|
|
|
-delta.y()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
if (!layer)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-08-25 05:06:00 -03:00
|
|
|
auto& image_event = event.image_event();
|
2020-05-12 19:08:47 -03:00
|
|
|
if (!m_layer_being_moved)
|
|
|
|
|
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
|
|
|
{
|
2021-09-12 00:26:09 -03:00
|
|
|
if (event.image_event().button() == GUI::MouseButton::Right && m_is_panning) {
|
|
|
|
|
m_is_panning = false;
|
|
|
|
|
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();
|
|
|
|
|
if (layer_event.button() != GUI::MouseButton::Left)
|
2020-05-12 19:08:47 -03:00
|
|
|
return;
|
|
|
|
|
m_layer_being_moved = nullptr;
|
2020-10-17 13:47:34 -03:00
|
|
|
m_editor->did_complete_action();
|
2020-05-12 19:08:47 -03:00
|
|
|
}
|
|
|
|
|
|
2020-05-13 08:20:54 -03:00
|
|
|
void MoveTool::on_keydown(GUI::KeyEvent& event)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 19:08:47 -03:00
|
|
|
}
|