1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:37:35 +00:00

PixelPaint: Allow pasting a copied bitmap as a new layer :^)

This commit is contained in:
Andreas Kling 2020-09-05 16:53:51 +02:00
parent 00bdb74c84
commit 6aa7f59608
4 changed files with 35 additions and 2 deletions

View file

@ -71,6 +71,8 @@ void Image::add_layer(NonnullRefPtr<Layer> layer)
for (auto* client : m_clients)
client->image_did_add_layer(m_layers.size() - 1);
did_modify_layer_stack();
}
size_t Image::index_of(const Layer& layer) const

View file

@ -41,6 +41,17 @@ RefPtr<Layer> Layer::create_with_size(Image& image, const Gfx::IntSize& size, co
return adopt(*new Layer(image, size, name));
}
RefPtr<Layer> Layer::create_with_bitmap(Image& image, const Gfx::Bitmap& bitmap, const String& name)
{
if (bitmap.size().is_empty())
return nullptr;
if (bitmap.size().width() > 16384 || bitmap.size().height() > 16384)
return nullptr;
return adopt(*new Layer(image, bitmap, name));
}
Layer::Layer(Image& image, const Gfx::IntSize& size, const String& name)
: m_image(image)
, m_name(name)
@ -48,6 +59,13 @@ Layer::Layer(Image& image, const Gfx::IntSize& size, const String& name)
m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, size);
}
Layer::Layer(Image& image, const Gfx::Bitmap& bitmap, const String& name)
: m_image(image)
, m_name(name)
, m_bitmap(bitmap)
{
}
void Layer::did_modify_bitmap(Image& image)
{
image.layer_did_modify_bitmap({}, *this);

View file

@ -45,6 +45,7 @@ class Layer
public:
static RefPtr<Layer> create_with_size(Image&, const Gfx::IntSize&, const String& name);
static RefPtr<Layer> create_with_bitmap(Image&, const Gfx::Bitmap&, const String& name);
~Layer() { }
@ -75,7 +76,8 @@ public:
void set_opacity_percent(int);
private:
explicit Layer(Image&, const Gfx::IntSize&, const String& name);
Layer(Image&, const Gfx::IntSize&, const String& name);
Layer(Image&, const Gfx::Bitmap&, const String& name);
Image& m_image;

View file

@ -42,6 +42,7 @@
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/FilePicker.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Menu.h>
@ -126,7 +127,17 @@ int main(int argc, char** argv)
return;
}));
menubar->add_menu("Edit");
auto& edit_menu = menubar->add_menu("Edit");
edit_menu.add_action(GUI::CommonActions::make_paste_action([&](auto&) {
ASSERT(image_editor.image());
auto bitmap = GUI::Clipboard::the().bitmap();
if (!bitmap)
return;
auto layer = PixelPaint::Layer::create_with_bitmap(*image_editor.image(), *bitmap, "Pasted layer");
image_editor.image()->add_layer(layer.release_nonnull());
}));
auto& tool_menu = menubar->add_menu("Tool");
toolbox.for_each_tool([&](auto& tool) {