PixelPaint: Add a Selection class (ImageEditor has a Selection)

This will represent a complex, region-based selection in the future.
For now though, it's just a simple rectangle. :^)
This commit is contained in:
Andreas Kling 2021-06-14 17:36:18 +02:00
parent 96b52f13e4
commit 1b897ec561
Notes: sideshowbarker 2024-07-18 12:14:42 +09:00
6 changed files with 61 additions and 0 deletions

View file

@ -21,6 +21,7 @@ set(SOURCES
PixelPaintWindowGML.h
RectangleTool.cpp
RectangleSelectTool.cpp
Selection.cpp
SprayTool.cpp
ToolboxWidget.cpp
ToolPropertiesWidget.cpp

View file

@ -95,6 +95,9 @@ void ImageEditor::paint_event(GUI::PaintEvent& event)
if (m_active_layer) {
painter.draw_rect(enclosing_int_rect(image_rect_to_editor_rect(m_active_layer->relative_rect())).inflated(2, 2), Color::Black);
}
if (!m_selection.is_empty())
m_selection.paint(painter, *this);
}
Gfx::FloatRect ImageEditor::layer_rect_to_editor_rect(Layer const& layer, Gfx::IntRect const& layer_rect) const

View file

@ -7,6 +7,7 @@
#pragma once
#include "Image.h"
#include "Selection.h"
#include <LibGUI/Frame.h>
#include <LibGUI/UndoStack.h>
#include <LibGfx/Point.h>
@ -53,6 +54,9 @@ public:
Color secondary_color() const { return m_secondary_color; }
void set_secondary_color(Color);
Selection& selection() { return m_selection; }
Selection const& selection() const { return m_selection; }
Color color_for(GUI::MouseEvent const&) const;
Color color_for(GUI::MouseButton) const;
@ -105,6 +109,8 @@ private:
Gfx::FloatPoint m_pan_origin;
Gfx::FloatPoint m_saved_pan_origin;
Gfx::IntPoint m_click_position;
Selection m_selection;
};
}

View file

@ -58,6 +58,9 @@ void RectangleSelectTool::on_mouseup(Layer&, GUI::MouseEvent&, GUI::MouseEvent&
m_selecting = false;
m_editor->update();
auto rect_in_image = Gfx::IntRect::from_two_points(m_selection_start, m_selection_end);
m_editor->selection().set(rect_in_image);
}
void RectangleSelectTool::draw_marching_ants(Gfx::Painter& painter, Gfx::IntRect const& rect) const

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Selection.h"
#include "ImageEditor.h"
#include <LibGfx/Painter.h>
namespace PixelPaint {
void Selection::paint(Gfx::Painter& painter, ImageEditor const& editor)
{
painter.draw_rect(editor.image_rect_to_editor_rect(m_rect).to_type<int>(), Color::Magenta);
}
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGfx/Rect.h>
namespace PixelPaint {
class ImageEditor;
// Coordinates are image-relative.
class Selection {
public:
Selection() { }
bool is_empty() const { return m_rect.is_empty(); }
void clear() { m_rect = {}; }
void set(Gfx::IntRect const& rect) { m_rect = rect; }
void paint(Gfx::Painter&, ImageEditor const&);
private:
Gfx::IntRect m_rect;
};
}