PixelPaint: Expose more complex selection operations

Now that we use RectMask internally to store the selection, we can
expose more powerful APIs to allow for better control over the image
selection.
This commit is contained in:
Davipb 2021-06-20 10:53:41 -03:00 committed by Andreas Kling
parent d922e35579
commit 22585e2845
Notes: sideshowbarker 2024-07-18 11:55:55 +09:00
4 changed files with 38 additions and 3 deletions

View file

@ -60,7 +60,7 @@ void RectangleSelectTool::on_mouseup(Layer&, GUI::MouseEvent&, GUI::MouseEvent&
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);
m_editor->selection().merge(rect_in_image, Selection::MergeMode::Set);
}
void RectangleSelectTool::on_keydown(GUI::KeyEvent& key_event)

View file

@ -107,6 +107,26 @@ void Selection::clear()
m_editor.update();
}
void Selection::merge(Mask const& mask, MergeMode mode)
{
switch (mode) {
case MergeMode::Set:
m_mask = mask;
break;
case MergeMode::Add:
m_mask.add(mask);
break;
case MergeMode::Subtract:
m_mask.subtract(mask);
break;
case MergeMode::Intersect:
m_mask.intersect(mask);
break;
default:
VERIFY_NOT_REACHED();
}
}
void Selection::draw_marching_ants_pixel(Gfx::Painter& painter, int x, int y) const
{
int pattern_index = x + y + m_marching_ants_offset;

View file

@ -18,13 +18,28 @@ class ImageEditor;
// Coordinates are image-relative.
class Selection {
public:
enum class MergeMode {
Set,
Add,
Subtract,
Intersect,
__Count,
};
explicit Selection(ImageEditor&);
bool is_empty() const { return m_mask.is_null(); }
void clear();
void set(Gfx::IntRect const& rect) { m_mask = Mask::full(rect); }
void merge(Mask const&, MergeMode);
void merge(Gfx::IntRect const& rect, MergeMode mode) { merge(Mask::full(rect), mode); }
Gfx::IntRect bounding_rect() const { return m_mask.bounding_rect(); }
[[nodiscard]] bool is_selected(int x, int y) const { return m_mask.get(x, y) > 0; }
[[nodiscard]] bool is_selected(Gfx::IntPoint const& point) const { return is_selected(point.x(), point.y()); }
[[nodiscard]] u8 get_selection_alpha(int x, int y) const { return m_mask.get(x, y); }
[[nodiscard]] u8 get_selection_alpha(Gfx::IntPoint const& point) const { return get_selection_alpha(point.x(), point.y()); }
void paint(Gfx::Painter&);
void draw_marching_ants(Gfx::Painter&, Gfx::IntRect const&) const;

View file

@ -237,7 +237,7 @@ int main(int argc, char** argv)
auto* editor = current_image_editor();
if (!editor->active_layer())
return;
editor->selection().set(editor->active_layer()->relative_rect());
editor->selection().merge(editor->active_layer()->relative_rect(), PixelPaint::Selection::MergeMode::Set);
}));
edit_menu.add_action(GUI::Action::create(
"Clear &Selection", { Mod_Ctrl | Mod_Shift, Key_A }, [&](auto&) {