Browse Source

PixelPaint: Add delete selection behavior

The delete key can now be used to erase the pixels on the active layer
contained within the selection rectangle.

Closes #11861
Olivier De Cannière 3 năm trước cách đây
mục cha
commit
d3dfb957a6

+ 5 - 0
Userland/Applications/PixelPaint/ImageEditor.cpp

@@ -349,6 +349,11 @@ void ImageEditor::context_menu_event(GUI::ContextMenuEvent& event)
 
 void ImageEditor::keydown_event(GUI::KeyEvent& event)
 {
+    if (event.key() == Key_Delete && !selection().is_empty() && active_layer()) {
+        active_layer()->erase_selection(selection());
+        return;
+    }
+
     if (m_active_tool)
         m_active_tool->on_keydown(event);
 }

+ 10 - 0
Userland/Applications/PixelPaint/Layer.cpp

@@ -10,6 +10,7 @@
 #include <AK/RefPtr.h>
 #include <AK/Try.h>
 #include <LibGfx/Bitmap.h>
+#include <LibGfx/Painter.h>
 
 namespace PixelPaint {
 
@@ -129,4 +130,13 @@ RefPtr<Gfx::Bitmap> Layer::try_copy_bitmap(Selection const& selection) const
     return result;
 }
 
+void Layer::erase_selection(Selection const& selection)
+{
+    Gfx::Painter painter { bitmap() };
+    auto const image_and_selection_intersection = m_image.rect().intersected(selection.bounding_rect());
+    auto const translated_to_layer_space = image_and_selection_intersection.translated(-location());
+    painter.clear_rect(translated_to_layer_space, Color::Transparent);
+    did_modify_bitmap(translated_to_layer_space);
+}
+
 }

+ 2 - 0
Userland/Applications/PixelPaint/Layer.h

@@ -61,6 +61,8 @@ public:
 
     Image const& image() const { return m_image; }
 
+    void erase_selection(Selection const&);
+
 private:
     Layer(Image&, NonnullRefPtr<Gfx::Bitmap>, String name);