PaintBrush: Add a "Move" tool for moving layers around :^)

Tool mouse event handlers now receive both a layer-relative mouse event
and the original event. This is needed for the move tool since it moves
the layer and thereby changes the origin of future events every time it
moves.
This commit is contained in:
Andreas Kling 2020-05-13 00:08:47 +02:00
parent 58ee75c87a
commit 3d50f40396
Notes: sideshowbarker 2024-07-19 06:41:43 +09:00
22 changed files with 167 additions and 44 deletions

View File

@ -78,7 +78,7 @@ static void flood_fill(Gfx::Bitmap& bitmap, const Gfx::Point& start_position, Co
}
}
void BucketTool::on_mousedown(Layer& layer, GUI::MouseEvent& event)
void BucketTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (!layer.rect().contains(event.position()))
return;

View File

@ -35,7 +35,7 @@ public:
BucketTool();
virtual ~BucketTool() override;
virtual void on_mousedown(Layer&, GUI::MouseEvent&) override;
virtual void on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
private:
virtual const char* class_name() const override { return "BucketTool"; }

View File

@ -56,7 +56,7 @@ void EllipseTool::draw_using(GUI::Painter& painter)
}
}
void EllipseTool::on_mousedown(Layer&, GUI::MouseEvent& event)
void EllipseTool::on_mousedown(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
return;
@ -70,7 +70,7 @@ void EllipseTool::on_mousedown(Layer&, GUI::MouseEvent& event)
m_editor->update();
}
void EllipseTool::on_mouseup(Layer& layer, GUI::MouseEvent& event)
void EllipseTool::on_mouseup(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() == m_drawing_button) {
GUI::Painter painter(layer.bitmap());
@ -80,7 +80,7 @@ void EllipseTool::on_mouseup(Layer& layer, GUI::MouseEvent& event)
}
}
void EllipseTool::on_mousemove(Layer& layer, GUI::MouseEvent& event)
void EllipseTool::on_mousemove(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (m_drawing_button == GUI::MouseButton::None)
return;

View File

@ -37,9 +37,9 @@ public:
EllipseTool();
virtual ~EllipseTool() override;
virtual void on_mousedown(Layer&, GUI::MouseEvent&) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent&) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent&) override;
virtual void on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_contextmenu(GUI::ContextMenuEvent&) override;
virtual void on_second_paint(GUI::PaintEvent&) override;
virtual void on_keydown(GUI::KeyEvent&) override;

View File

@ -53,7 +53,7 @@ Gfx::Rect EraseTool::build_rect(const Gfx::Point& pos, const Gfx::Rect& widget_r
return Gfx::Rect(ex - eraser_radius, ey - eraser_radius, eraser_size, eraser_size).intersected(widget_rect);
}
void EraseTool::on_mousedown(Layer& layer, GUI::MouseEvent& event)
void EraseTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
return;
@ -63,7 +63,7 @@ void EraseTool::on_mousedown(Layer& layer, GUI::MouseEvent& event)
m_editor->update();
}
void EraseTool::on_mousemove(Layer& layer, GUI::MouseEvent& event)
void EraseTool::on_mousemove(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (!m_editor->rect().contains(event.position()))
return;

View File

@ -38,8 +38,8 @@ public:
EraseTool();
virtual ~EraseTool() override;
virtual void on_mousedown(Layer&, GUI::MouseEvent&) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent&) override;
virtual void on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_contextmenu(GUI::ContextMenuEvent&) override;
private:

View File

@ -79,7 +79,7 @@ void ImageEditor::mousedown_event(GUI::MouseEvent& event)
if (!m_active_layer || !m_active_tool)
return;
auto layer_event = event_adjusted_for_layer(event, *m_active_layer);
m_active_tool->on_mousedown(*m_active_layer, layer_event);
m_active_tool->on_mousedown(*m_active_layer, layer_event, event);
}
void ImageEditor::mousemove_event(GUI::MouseEvent& event)
@ -87,7 +87,7 @@ void ImageEditor::mousemove_event(GUI::MouseEvent& event)
if (!m_active_layer || !m_active_tool)
return;
auto layer_event = event_adjusted_for_layer(event, *m_active_layer);
m_active_tool->on_mousemove(*m_active_layer, layer_event);
m_active_tool->on_mousemove(*m_active_layer, layer_event, event);
}
void ImageEditor::mouseup_event(GUI::MouseEvent& event)
@ -95,7 +95,7 @@ void ImageEditor::mouseup_event(GUI::MouseEvent& event)
if (!m_active_layer || !m_active_tool)
return;
auto layer_event = event_adjusted_for_layer(event, *m_active_layer);
m_active_tool->on_mouseup(*m_active_layer, layer_event);
m_active_tool->on_mouseup(*m_active_layer, layer_event, event);
}
void ImageEditor::set_active_layer(Layer* layer)

View File

@ -56,7 +56,7 @@ LineTool::~LineTool()
{
}
void LineTool::on_mousedown(Layer&, GUI::MouseEvent& event)
void LineTool::on_mousedown(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
return;
@ -70,7 +70,7 @@ void LineTool::on_mousedown(Layer&, GUI::MouseEvent& event)
m_editor->update();
}
void LineTool::on_mouseup(Layer& layer, GUI::MouseEvent& event)
void LineTool::on_mouseup(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() == m_drawing_button) {
GUI::Painter painter(layer.bitmap());
@ -80,7 +80,7 @@ void LineTool::on_mouseup(Layer& layer, GUI::MouseEvent& event)
}
}
void LineTool::on_mousemove(Layer&, GUI::MouseEvent& event)
void LineTool::on_mousemove(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (m_drawing_button == GUI::MouseButton::None)
return;

View File

@ -37,9 +37,9 @@ public:
LineTool();
virtual ~LineTool() override;
virtual void on_mousedown(Layer&, GUI::MouseEvent&) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent&) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent&) override;
virtual void on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_contextmenu(GUI::ContextMenuEvent&) override;
virtual void on_second_paint(GUI::PaintEvent&) override;
virtual void on_keydown(GUI::KeyEvent&) override;

View File

@ -7,6 +7,7 @@ OBJS = \
Layer.o \
LayerModel.o \
LineTool.o \
MoveTool.o \
PaintableWidget.o \
PaletteWidget.o \
PenTool.o \

View File

@ -0,0 +1,70 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "MoveTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <LibGfx/Bitmap.h>
namespace PaintBrush {
MoveTool::MoveTool()
{
}
MoveTool::~MoveTool()
{
}
void MoveTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent& original_event)
{
if (event.button() != GUI::MouseButton::Left)
return;
if (!layer.rect().contains(event.position()))
return;
m_layer_being_moved = layer;
m_event_origin = original_event.position();
m_layer_origin = layer.location();
}
void MoveTool::on_mousemove(Layer&, GUI::MouseEvent&, GUI::MouseEvent& original_event)
{
if (!m_layer_being_moved)
return;
auto delta = original_event.position() - m_event_origin;
m_layer_being_moved->set_location(m_layer_origin.translated(delta));
m_editor->update();
}
void MoveTool::on_mouseup(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() != GUI::MouseButton::Left)
return;
m_layer_being_moved = nullptr;
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "Tool.h"
namespace PaintBrush {
class MoveTool final : public Tool {
public:
MoveTool();
virtual ~MoveTool() override;
virtual void on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
private:
virtual const char* class_name() const override { return "MoveTool"; }
RefPtr<Layer> m_layer_being_moved;
Gfx::Point m_event_origin;
Gfx::Point m_layer_origin;
};
}

View File

@ -42,7 +42,7 @@ PenTool::~PenTool()
{
}
void PenTool::on_mousedown(Layer& layer, GUI::MouseEvent& event)
void PenTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
return;
@ -53,13 +53,13 @@ void PenTool::on_mousedown(Layer& layer, GUI::MouseEvent& event)
m_last_drawing_event_position = event.position();
}
void PenTool::on_mouseup(Layer&, GUI::MouseEvent& event)
void PenTool::on_mouseup(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() == GUI::MouseButton::Left || event.button() == GUI::MouseButton::Right)
m_last_drawing_event_position = { -1, -1 };
}
void PenTool::on_mousemove(Layer& layer, GUI::MouseEvent& event)
void PenTool::on_mousemove(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (!layer.rect().contains(event.position()))
return;

View File

@ -37,9 +37,9 @@ public:
PenTool();
virtual ~PenTool() override;
virtual void on_mousedown(Layer&, GUI::MouseEvent&) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent&) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent&) override;
virtual void on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_contextmenu(GUI::ContextMenuEvent&) override;
private:

View File

@ -39,7 +39,7 @@ PickerTool::~PickerTool()
{
}
void PickerTool::on_mousedown(Layer& layer, GUI::MouseEvent& event)
void PickerTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (!layer.rect().contains(event.position()))
return;

View File

@ -35,7 +35,7 @@ public:
PickerTool();
virtual ~PickerTool() override;
virtual void on_mousedown(Layer&, GUI::MouseEvent&) override;
virtual void on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
private:
virtual const char* class_name() const override { return "PickerTool"; }

View File

@ -62,7 +62,7 @@ void RectangleTool::draw_using(GUI::Painter& painter)
}
}
void RectangleTool::on_mousedown(Layer&, GUI::MouseEvent& event)
void RectangleTool::on_mousedown(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
return;
@ -76,7 +76,7 @@ void RectangleTool::on_mousedown(Layer&, GUI::MouseEvent& event)
m_editor->update();
}
void RectangleTool::on_mouseup(Layer& layer, GUI::MouseEvent& event)
void RectangleTool::on_mouseup(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (event.button() == m_drawing_button) {
GUI::Painter painter(layer.bitmap());
@ -86,7 +86,7 @@ void RectangleTool::on_mouseup(Layer& layer, GUI::MouseEvent& event)
}
}
void RectangleTool::on_mousemove(Layer&, GUI::MouseEvent& event)
void RectangleTool::on_mousemove(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (m_drawing_button == GUI::MouseButton::None)
return;

View File

@ -37,9 +37,9 @@ public:
RectangleTool();
virtual ~RectangleTool() override;
virtual void on_mousedown(Layer&, GUI::MouseEvent&) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent&) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent&) override;
virtual void on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_contextmenu(GUI::ContextMenuEvent&) override;
virtual void on_second_paint(GUI::PaintEvent&) override;
virtual void on_keydown(GUI::KeyEvent&) override;

View File

@ -82,7 +82,7 @@ void SprayTool::paint_it()
}
}
void SprayTool::on_mousedown(Layer&, GUI::MouseEvent& event)
void SprayTool::on_mousedown(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
{
if (!m_editor->rect().contains(event.position()))
return;
@ -93,7 +93,7 @@ void SprayTool::on_mousedown(Layer&, GUI::MouseEvent& event)
paint_it();
}
void SprayTool::on_mousemove(Layer&, GUI::MouseEvent& event)
void SprayTool::on_mousemove(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
{
m_last_pos = event.position();
if (m_timer->is_active()) {
@ -102,7 +102,7 @@ void SprayTool::on_mousemove(Layer&, GUI::MouseEvent& event)
}
}
void SprayTool::on_mouseup(Layer&, GUI::MouseEvent&)
void SprayTool::on_mouseup(Layer&, GUI::MouseEvent&, GUI::MouseEvent&)
{
m_timer->stop();
}

View File

@ -38,9 +38,9 @@ public:
SprayTool();
virtual ~SprayTool() override;
virtual void on_mousedown(Layer&, GUI::MouseEvent&) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent&) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent&) override;
virtual void on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_contextmenu(GUI::ContextMenuEvent&) override;
private:

View File

@ -39,9 +39,9 @@ public:
virtual const char* class_name() const = 0;
virtual void on_mousedown(Layer&, GUI::MouseEvent&) {}
virtual void on_mousemove(Layer&, GUI::MouseEvent&) {}
virtual void on_mouseup(Layer&, GUI::MouseEvent&) {}
virtual void on_mousedown(Layer&, GUI::MouseEvent&, GUI::MouseEvent&) {}
virtual void on_mousemove(Layer&, GUI::MouseEvent&, GUI::MouseEvent&) {}
virtual void on_mouseup(Layer&, GUI::MouseEvent&, GUI::MouseEvent&) {}
virtual void on_contextmenu(GUI::ContextMenuEvent&) {}
virtual void on_second_paint(GUI::PaintEvent&) {}
virtual void on_keydown(GUI::KeyEvent&) {}

View File

@ -29,6 +29,7 @@
#include "EllipseTool.h"
#include "EraseTool.h"
#include "LineTool.h"
#include "MoveTool.h"
#include "PaintableWidget.h"
#include "PenTool.h"
#include "PickerTool.h"
@ -92,6 +93,7 @@ ToolboxWidget::ToolboxWidget()
};
};
add_tool("Move", "move", make<MoveTool>());
add_tool("Pen", "pen", make<PenTool>());
add_tool("Bucket Fill", "bucket", make<BucketTool>());
add_tool("Spray", "spray", make<SprayTool>());