From e66763c223f337d0b349c075642a2b89831aa52d Mon Sep 17 00:00:00 2001 From: Timothy Slater Date: Fri, 2 Sep 2022 20:35:24 -0500 Subject: [PATCH] PixelPaint: Add function to make layer from selection to ImageEditor --- .../Applications/PixelPaint/ImageEditor.cpp | 25 +++++++++++++++++++ .../Applications/PixelPaint/ImageEditor.h | 1 + 2 files changed, 26 insertions(+) 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 * Copyright (c) 2021-2022, Mustafa Quraish * Copyright (c) 2021, David Isaksson + * Copyright (c) 2022, Timothy Slater * * SPDX-License-Identifier: BSD-2-Clause */ @@ -427,6 +428,30 @@ void ImageEditor::set_active_layer(Layer* layer) } } +ErrorOr 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 add_new_layer_from_selection(); Tool* active_tool() { return m_active_tool; } void set_active_tool(Tool*); void update_tool_cursor();