1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:27:45 +00:00

PixelPaint: Rename Layer::create_foo() => Layer::try_create_foo()

This commit is contained in:
Andreas Kling 2021-06-11 23:06:46 +02:00
parent a9e98bad8a
commit 9c5de113b1
4 changed files with 29 additions and 21 deletions

View file

@ -55,15 +55,14 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect)
RefPtr<Image> Image::try_create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap)
{
auto image = try_create_with_size({ bitmap->width(), bitmap->height() });
if (image.is_null())
if (!image)
return nullptr;
auto layer = Layer::create_with_bitmap(*image, *bitmap, "Background");
if (layer.is_null())
auto layer = Layer::try_create_with_bitmap(*image, *bitmap, "Background");
if (!layer)
return nullptr;
image->add_layer(layer.release_nonnull());
return image;
}
@ -89,7 +88,8 @@ RefPtr<Image> Image::try_create_from_pixel_paint_file(String const& file_path)
auto width = json_layer_object.get("width").to_i32();
auto height = json_layer_object.get("height").to_i32();
auto name = json_layer_object.get("name").as_string();
auto layer = Layer::create_with_size(*image, { width, height }, name);
auto layer = Layer::try_create_with_size(*image, { width, height }, name);
VERIFY(layer);
layer->set_location({ json_layer_object.get("locationx").to_i32(), json_layer_object.get("locationy").to_i32() });
layer->set_opacity_percent(json_layer_object.get("opacity_percent").to_i32());
layer->set_visible(json_layer_object.get("visible").as_bool());
@ -188,8 +188,12 @@ RefPtr<Image> Image::take_snapshot() const
auto snapshot = try_create_with_size(m_size);
if (!snapshot)
return nullptr;
for (const auto& layer : m_layers)
snapshot->add_layer(*Layer::create_snapshot(*snapshot, layer));
for (const auto& layer : m_layers) {
auto layer_snapshot = Layer::try_create_snapshot(*snapshot, layer);
if (!layer_snapshot)
return nullptr;
snapshot->add_layer(layer_snapshot.release_nonnull());
}
return snapshot;
}
@ -198,7 +202,8 @@ void Image::restore_snapshot(Image const& snapshot)
m_layers.clear();
select_layer(nullptr);
for (const auto& snapshot_layer : snapshot.m_layers) {
auto layer = Layer::create_snapshot(*this, snapshot_layer);
auto layer = Layer::try_create_snapshot(*this, snapshot_layer);
VERIFY(layer);
if (layer->is_selected())
select_layer(layer.ptr());
add_layer(*layer);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,7 +10,7 @@
namespace PixelPaint {
RefPtr<Layer> Layer::create_with_size(Image& image, Gfx::IntSize const& size, String const& name)
RefPtr<Layer> Layer::try_create_with_size(Image& image, Gfx::IntSize const& size, String const& name)
{
if (size.is_empty())
return nullptr;
@ -21,7 +21,7 @@ RefPtr<Layer> Layer::create_with_size(Image& image, Gfx::IntSize const& size, St
return adopt_ref(*new Layer(image, size, name));
}
RefPtr<Layer> Layer::create_with_bitmap(Image& image, Gfx::Bitmap const& bitmap, String const& name)
RefPtr<Layer> Layer::try_create_with_bitmap(Image& image, Gfx::Bitmap const& bitmap, String const& name)
{
if (bitmap.size().is_empty())
return nullptr;
@ -32,9 +32,9 @@ RefPtr<Layer> Layer::create_with_bitmap(Image& image, Gfx::Bitmap const& bitmap,
return adopt_ref(*new Layer(image, bitmap, name));
}
RefPtr<Layer> Layer::create_snapshot(Image& image, Layer const& layer)
RefPtr<Layer> Layer::try_create_snapshot(Image& image, Layer const& layer)
{
auto snapshot = create_with_bitmap(image, *layer.bitmap().clone(), layer.name());
auto snapshot = try_create_with_bitmap(image, *layer.bitmap().clone(), layer.name());
/*
We set these properties directly because calling the setters might
notify the image of an update on the newly created layer, but this

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -24,9 +24,9 @@ class Layer
AK_MAKE_NONMOVABLE(Layer);
public:
static RefPtr<Layer> create_with_size(Image&, Gfx::IntSize const&, String const& name);
static RefPtr<Layer> create_with_bitmap(Image&, Gfx::Bitmap const&, String const& name);
static RefPtr<Layer> create_snapshot(Image&, Layer const&);
static RefPtr<Layer> try_create_with_size(Image&, Gfx::IntSize const&, String const& name);
static RefPtr<Layer> try_create_with_bitmap(Image&, Gfx::Bitmap const&, String const& name);
static RefPtr<Layer> try_create_snapshot(Image&, Layer const&);
~Layer() { }

View file

@ -88,7 +88,8 @@ int main(int argc, char** argv)
auto dialog = PixelPaint::CreateNewImageDialog::construct(window);
if (dialog->exec() == GUI::Dialog::ExecOK) {
auto image = PixelPaint::Image::try_create_with_size(dialog->image_size());
auto bg_layer = PixelPaint::Layer::create_with_size(*image, image->size(), "Background");
auto bg_layer = PixelPaint::Layer::try_create_with_size(*image, image->size(), "Background");
VERIFY(bg_layer);
image->add_layer(*bg_layer);
bg_layer->bitmap().fill(Color::White);
@ -170,7 +171,8 @@ int main(int argc, char** argv)
if (!bitmap)
return;
auto layer = PixelPaint::Layer::create_with_bitmap(*image_editor.image(), *bitmap, "Pasted layer");
auto layer = PixelPaint::Layer::try_create_with_bitmap(*image_editor.image(), *bitmap, "Pasted layer");
VERIFY(layer);
image_editor.image()->add_layer(layer.release_nonnull());
});
GUI::Clipboard::the().on_change = [&](auto& mime_type) {
@ -228,7 +230,7 @@ int main(int argc, char** argv)
"New &Layer...", { Mod_Ctrl | Mod_Shift, Key_N }, [&](auto&) {
auto dialog = PixelPaint::CreateNewLayerDialog::construct(image_editor.image()->size(), window);
if (dialog->exec() == GUI::Dialog::ExecOK) {
auto layer = PixelPaint::Layer::create_with_size(*image_editor.image(), dialog->layer_size(), dialog->layer_name());
auto layer = PixelPaint::Layer::try_create_with_size(*image_editor.image(), dialog->layer_size(), dialog->layer_name());
if (!layer) {
GUI::MessageBox::show_error(window, String::formatted("Unable to create layer with size {}", dialog->size().to_string()));
return;
@ -397,7 +399,8 @@ int main(int argc, char** argv)
} else {
auto image = PixelPaint::Image::try_create_with_size({ 480, 360 });
auto bg_layer = PixelPaint::Layer::create_with_size(*image, image->size(), "Background");
auto bg_layer = PixelPaint::Layer::try_create_with_size(*image, image->size(), "Background");
VERIFY(bg_layer);
image->add_layer(*bg_layer);
bg_layer->bitmap().fill(Color::White);