From d3353ee0e7cb2b34651bd3ae7f52d8a20548fe11 Mon Sep 17 00:00:00 2001 From: snooze6214 Date: Tue, 18 Oct 2022 20:24:00 +0530 Subject: [PATCH] PixelPaint: Add action to invert selection --- Base/res/icons/pixelpaint/invert-selection.png | Bin 0 -> 165 bytes Userland/Applications/PixelPaint/IconBag.cpp | 1 + Userland/Applications/PixelPaint/IconBag.h | 1 + Userland/Applications/PixelPaint/MainWidget.cpp | 6 ++++++ Userland/Applications/PixelPaint/Selection.cpp | 7 +++++++ Userland/Applications/PixelPaint/Selection.h | 1 + 6 files changed, 16 insertions(+) create mode 100644 Base/res/icons/pixelpaint/invert-selection.png diff --git a/Base/res/icons/pixelpaint/invert-selection.png b/Base/res/icons/pixelpaint/invert-selection.png new file mode 100644 index 0000000000000000000000000000000000000000..d1bfe011411e6f59711901a63850b188fb0c9285 GIT binary patch literal 165 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd7G?$phPQVgfdu#id_r6q7#KWw{@+lv`2YXE zpP%2ep0lqR7#LVeg8YIR7#J8fPfnY~z`)?>>Eal|aXmSqfl*A{pdcZk@k5}5-Z`~% z3T+D(D=g+v_BhhW=(9mXQqxe=xY312LSlyGgrx#J!Yj&rCMQli#K0g}!}W^8V{$mi NP)}Dsmvv4FO#pzuFgpMM literal 0 HcmV?d00001 diff --git a/Userland/Applications/PixelPaint/IconBag.cpp b/Userland/Applications/PixelPaint/IconBag.cpp index c896106d30..12a1f3b65e 100644 --- a/Userland/Applications/PixelPaint/IconBag.cpp +++ b/Userland/Applications/PixelPaint/IconBag.cpp @@ -18,6 +18,7 @@ ErrorOr IconBag::try_create() icon_bag.close_image = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/close-tab.png"sv)); icon_bag.edit_copy = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-copy.png"sv)); icon_bag.clear_selection = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/clear-selection.png"sv)); + icon_bag.invert_selection = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/pixelpaint/invert-selection.png"sv)); icon_bag.swap_colors = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/pixelpaint/swap-colors.png"sv)); icon_bag.default_colors = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/pixelpaint/default-colors.png"sv)); icon_bag.load_color_palette = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/pixelpaint/load-color-palette.png"sv)); diff --git a/Userland/Applications/PixelPaint/IconBag.h b/Userland/Applications/PixelPaint/IconBag.h index 1f5d6d4e71..3fe1ae378a 100644 --- a/Userland/Applications/PixelPaint/IconBag.h +++ b/Userland/Applications/PixelPaint/IconBag.h @@ -19,6 +19,7 @@ struct IconBag final { RefPtr close_image { nullptr }; RefPtr edit_copy { nullptr }; RefPtr clear_selection { nullptr }; + RefPtr invert_selection { nullptr }; RefPtr swap_colors { nullptr }; RefPtr default_colors { nullptr }; RefPtr load_color_palette { nullptr }; diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp index 5f9c93e0eb..5897d5ce6a 100644 --- a/Userland/Applications/PixelPaint/MainWidget.cpp +++ b/Userland/Applications/PixelPaint/MainWidget.cpp @@ -365,6 +365,12 @@ void MainWidget::initialize_menubar(GUI::Window& window) VERIFY(editor); editor->image().selection().clear(); })); + m_edit_menu->add_action(GUI::Action::create( + "&Invert Selection", g_icon_bag.invert_selection, [&](auto&) { + auto* editor = current_image_editor(); + VERIFY(editor); + editor->image().selection().invert(); + })); m_edit_menu->add_separator(); m_edit_menu->add_action(GUI::Action::create( diff --git a/Userland/Applications/PixelPaint/Selection.cpp b/Userland/Applications/PixelPaint/Selection.cpp index 386ab9cbef..b936ac9420 100644 --- a/Userland/Applications/PixelPaint/Selection.cpp +++ b/Userland/Applications/PixelPaint/Selection.cpp @@ -22,6 +22,13 @@ void Selection::clear() client->selection_did_change(); } +void Selection::invert() +{ + auto new_mask = Mask::full(m_image.rect()); + new_mask.subtract(m_mask); + m_mask = new_mask; +} + void Selection::merge(Mask const& mask, MergeMode mode) { switch (mode) { diff --git a/Userland/Applications/PixelPaint/Selection.h b/Userland/Applications/PixelPaint/Selection.h index c70e49954e..5d7d9669af 100644 --- a/Userland/Applications/PixelPaint/Selection.h +++ b/Userland/Applications/PixelPaint/Selection.h @@ -38,6 +38,7 @@ public: bool is_empty() const { return m_mask.is_null(); } void clear(); + void invert(); 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(); }