summaryrefslogtreecommitdiff
path: root/Userland/Applications/PixelPaint
diff options
context:
space:
mode:
authorTimothy Slater <tslater2006@gmail.com>2022-09-02 20:35:24 -0500
committerSam Atkins <atkinssj@gmail.com>2022-09-29 10:20:17 +0100
commite66763c223f337d0b349c075642a2b89831aa52d (patch)
tree12b33b7e8a27149d92796137d901b0f4e8ecb1b6 /Userland/Applications/PixelPaint
parent9d1336a1c98c3285848d4224b884708ff69a1177 (diff)
downloadserenity-e66763c223f337d0b349c075642a2b89831aa52d.zip
PixelPaint: Add function to make layer from selection to ImageEditor
Diffstat (limited to 'Userland/Applications/PixelPaint')
-rw-r--r--Userland/Applications/PixelPaint/ImageEditor.cpp25
-rw-r--r--Userland/Applications/PixelPaint/ImageEditor.h1
2 files changed, 26 insertions, 0 deletions
diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp
index 05ce2e857f..153f791916 100644
--- a/Userland/Applications/PixelPaint/ImageEditor.cpp
+++ b/Userland/Applications/PixelPaint/ImageEditor.cpp
@@ -3,6 +3,7 @@
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2022, Mustafa Quraish <mustafa@serenityos.org>
* Copyright (c) 2021, David Isaksson <davidisaksson93@gmail.com>
+ * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -427,6 +428,30 @@ void ImageEditor::set_active_layer(Layer* layer)
}
}
+ErrorOr<void> ImageEditor::add_new_layer_from_selection()
+{
+ auto current_layer_selection = image().selection();
+ if (current_layer_selection.is_empty())
+ return Error::from_string_literal("There is no active selection to create layer from.");
+
+ // save offsets of selection so we know where to place the new layer
+ auto selection_offset = current_layer_selection.bounding_rect().location();
+
+ auto selection_bitmap = active_layer()->try_copy_bitmap(current_layer_selection);
+ if (selection_bitmap.is_null())
+ return Error::from_string_literal("Unable to create bitmap from selection.");
+
+ auto layer_or_error = PixelPaint::Layer::try_create_with_bitmap(image(), selection_bitmap.release_nonnull(), "New Layer"sv);
+ if (layer_or_error.is_error())
+ return Error::from_string_literal("Unable to create layer from selection.");
+
+ auto new_layer = layer_or_error.release_value();
+ new_layer->set_location(selection_offset);
+ image().add_layer(new_layer);
+ layers_did_change();
+ return {};
+}
+
void ImageEditor::set_active_tool(Tool* tool)
{
if (m_active_tool == tool)
diff --git a/Userland/Applications/PixelPaint/ImageEditor.h b/Userland/Applications/PixelPaint/ImageEditor.h
index d0e2bc4cb2..7635be766b 100644
--- a/Userland/Applications/PixelPaint/ImageEditor.h
+++ b/Userland/Applications/PixelPaint/ImageEditor.h
@@ -38,6 +38,7 @@ public:
Layer* active_layer() { return m_active_layer; }
void set_active_layer(Layer*);
+ ErrorOr<void> add_new_layer_from_selection();
Tool* active_tool() { return m_active_tool; }
void set_active_tool(Tool*);
void update_tool_cursor();