Browse Source

PixelPaint: Add method to flatten image layers

This adds Image::flatten_all_layers() that allows the user to flatten
all the visible layers into one.
Marcus Nilsson 4 years ago
parent
commit
9be08e33ea

+ 18 - 0
Userland/Applications/PixelPaint/Image.cpp

@@ -366,6 +366,24 @@ void Image::remove_layer(Layer& layer)
     did_modify_layer_stack();
 }
 
+void Image::flatten_all_layers()
+{
+    if (m_layers.size() < 2)
+        return;
+
+    auto& bottom_layer = m_layers.at(0);
+
+    GUI::Painter painter(bottom_layer.bitmap());
+    paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
+
+    for (size_t index = m_layers.size() - 1; index > 0; index--) {
+        auto& layer = m_layers.at(index);
+        remove_layer(layer);
+    }
+    bottom_layer.set_name("Background");
+    select_layer(&bottom_layer);
+}
+
 void Image::select_layer(Layer* layer)
 {
     for (auto* client : m_clients)

+ 1 - 0
Userland/Applications/PixelPaint/Image.h

@@ -68,6 +68,7 @@ public:
     void change_layer_index(size_t old_index, size_t new_index);
     void remove_layer(Layer&);
     void select_layer(Layer*);
+    void flatten_all_layers();
 
     void add_client(ImageClient&);
     void remove_client(ImageClient&);

+ 1 - 0
Userland/Applications/PixelPaint/LayerListWidget.cpp

@@ -217,6 +217,7 @@ void LayerListWidget::image_did_remove_layer(size_t layer_index)
         m_moving_gadget_index = {};
     }
     m_gadgets.remove(layer_index);
+    m_selected_layer_index = 0;
     relayout_gadgets();
 }
 

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

@@ -408,6 +408,16 @@ int main(int argc, char** argv)
     layer_list_widget.on_context_menu_request = [&](auto& event) {
         layer_menu.popup(event.screen_position());
     };
+    layer_menu.add_separator();
+    layer_menu.add_action(GUI::Action::create(
+        "&Flatten Image", { Mod_Ctrl, Key_F }, [&](auto&) {
+            auto* editor = current_image_editor();
+            if (!editor)
+                return;
+            editor->image().flatten_all_layers();
+            editor->did_complete_action();
+        },
+        window));
 
     auto& filter_menu = menubar->add_menu("&Filter");
     auto& spatial_filters_menu = filter_menu.add_submenu("&Spatial");