mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:17:45 +00:00
PixelPaint: Rename Image::create_foo() => Image::try_create_foo()
Factory functions that may fail should be called try_create().
This commit is contained in:
parent
29e80178a8
commit
a9e98bad8a
3 changed files with 22 additions and 23 deletions
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
namespace PixelPaint {
|
namespace PixelPaint {
|
||||||
|
|
||||||
RefPtr<Image> Image::create_with_size(Gfx::IntSize const& size)
|
RefPtr<Image> Image::try_create_with_size(Gfx::IntSize const& size)
|
||||||
{
|
{
|
||||||
if (size.is_empty())
|
if (size.is_empty())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -52,9 +52,9 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Image> Image::create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap)
|
RefPtr<Image> Image::try_create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap)
|
||||||
{
|
{
|
||||||
auto image = create_with_size({ bitmap->width(), bitmap->height() });
|
auto image = try_create_with_size({ bitmap->width(), bitmap->height() });
|
||||||
if (image.is_null())
|
if (image.is_null())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ RefPtr<Image> Image::create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap)
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Image> Image::create_from_pixel_paint_file(String const& file_path)
|
RefPtr<Image> Image::try_create_from_pixel_paint_file(String const& file_path)
|
||||||
{
|
{
|
||||||
auto file = fopen(file_path.characters(), "r");
|
auto file = fopen(file_path.characters(), "r");
|
||||||
fseek(file, 0L, SEEK_END);
|
fseek(file, 0L, SEEK_END);
|
||||||
|
@ -83,7 +83,7 @@ RefPtr<Image> Image::create_from_pixel_paint_file(String const& file_path)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto json = json_or_error.value().as_object();
|
auto json = json_or_error.value().as_object();
|
||||||
auto image = create_with_size({ json.get("width").to_i32(), json.get("height").to_i32() });
|
auto image = try_create_with_size({ json.get("width").to_i32(), json.get("height").to_i32() });
|
||||||
json.get("layers").as_array().for_each([&](JsonValue json_layer) {
|
json.get("layers").as_array().for_each([&](JsonValue json_layer) {
|
||||||
auto json_layer_object = json_layer.as_object();
|
auto json_layer_object = json_layer.as_object();
|
||||||
auto width = json_layer_object.get("width").to_i32();
|
auto width = json_layer_object.get("width").to_i32();
|
||||||
|
@ -105,14 +105,11 @@ RefPtr<Image> Image::create_from_pixel_paint_file(String const& file_path)
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Image> Image::create_from_file(String const& file_path)
|
RefPtr<Image> Image::try_create_from_file(String const& file_path)
|
||||||
{
|
{
|
||||||
auto bitmap = Gfx::Bitmap::load_from_file(file_path);
|
if (auto bitmap = Gfx::Bitmap::load_from_file(file_path))
|
||||||
if (bitmap) {
|
return try_create_from_bitmap(bitmap);
|
||||||
return create_from_bitmap(bitmap);
|
return try_create_from_pixel_paint_file(file_path);
|
||||||
}
|
|
||||||
|
|
||||||
return create_from_pixel_paint_file(file_path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Image::save(String const& file_path) const
|
void Image::save(String const& file_path) const
|
||||||
|
@ -188,7 +185,9 @@ void Image::add_layer(NonnullRefPtr<Layer> layer)
|
||||||
|
|
||||||
RefPtr<Image> Image::take_snapshot() const
|
RefPtr<Image> Image::take_snapshot() const
|
||||||
{
|
{
|
||||||
auto snapshot = create_with_size(m_size);
|
auto snapshot = try_create_with_size(m_size);
|
||||||
|
if (!snapshot)
|
||||||
|
return nullptr;
|
||||||
for (const auto& layer : m_layers)
|
for (const auto& layer : m_layers)
|
||||||
snapshot->add_layer(*Layer::create_snapshot(*snapshot, layer));
|
snapshot->add_layer(*Layer::create_snapshot(*snapshot, layer));
|
||||||
return snapshot;
|
return snapshot;
|
||||||
|
|
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -36,9 +36,9 @@ protected:
|
||||||
|
|
||||||
class Image : public RefCounted<Image> {
|
class Image : public RefCounted<Image> {
|
||||||
public:
|
public:
|
||||||
static RefPtr<Image> create_with_size(Gfx::IntSize const&);
|
static RefPtr<Image> try_create_with_size(Gfx::IntSize const&);
|
||||||
static RefPtr<Image> create_from_file(String const& file_path);
|
static RefPtr<Image> try_create_from_file(String const& file_path);
|
||||||
static RefPtr<Image> create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap);
|
static RefPtr<Image> try_create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap);
|
||||||
|
|
||||||
size_t layer_count() const { return m_layers.size(); }
|
size_t layer_count() const { return m_layers.size(); }
|
||||||
Layer const& layer(size_t index) const { return m_layers.at(index); }
|
Layer const& layer(size_t index) const { return m_layers.at(index); }
|
||||||
|
@ -75,7 +75,7 @@ public:
|
||||||
private:
|
private:
|
||||||
explicit Image(Gfx::IntSize const&);
|
explicit Image(Gfx::IntSize const&);
|
||||||
|
|
||||||
static RefPtr<Image> create_from_pixel_paint_file(String const& file_path);
|
static RefPtr<Image> try_create_from_pixel_paint_file(String const& file_path);
|
||||||
|
|
||||||
void did_change();
|
void did_change();
|
||||||
void did_modify_layer_stack();
|
void did_modify_layer_stack();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -87,7 +87,7 @@ int main(int argc, char** argv)
|
||||||
"&New Image...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](auto&) {
|
"&New Image...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](auto&) {
|
||||||
auto dialog = PixelPaint::CreateNewImageDialog::construct(window);
|
auto dialog = PixelPaint::CreateNewImageDialog::construct(window);
|
||||||
if (dialog->exec() == GUI::Dialog::ExecOK) {
|
if (dialog->exec() == GUI::Dialog::ExecOK) {
|
||||||
auto image = PixelPaint::Image::create_with_size(dialog->image_size());
|
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::create_with_size(*image, image->size(), "Background");
|
||||||
image->add_layer(*bg_layer);
|
image->add_layer(*bg_layer);
|
||||||
bg_layer->bitmap().fill(Color::White);
|
bg_layer->bitmap().fill(Color::White);
|
||||||
|
@ -100,7 +100,7 @@ int main(int argc, char** argv)
|
||||||
window);
|
window);
|
||||||
|
|
||||||
auto open_image_file = [&](auto& path) {
|
auto open_image_file = [&](auto& path) {
|
||||||
auto image = PixelPaint::Image::create_from_file(path);
|
auto image = PixelPaint::Image::try_create_from_file(path);
|
||||||
if (!image) {
|
if (!image) {
|
||||||
GUI::MessageBox::show_error(window, String::formatted("Invalid image file: {}", path));
|
GUI::MessageBox::show_error(window, String::formatted("Invalid image file: {}", path));
|
||||||
return;
|
return;
|
||||||
|
@ -395,7 +395,7 @@ int main(int argc, char** argv)
|
||||||
if (Core::File::exists(image_file_real_path)) {
|
if (Core::File::exists(image_file_real_path)) {
|
||||||
open_image_file(image_file_real_path);
|
open_image_file(image_file_real_path);
|
||||||
} else {
|
} else {
|
||||||
auto image = PixelPaint::Image::create_with_size({ 480, 360 });
|
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::create_with_size(*image, image->size(), "Background");
|
||||||
image->add_layer(*bg_layer);
|
image->add_layer(*bg_layer);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue