1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

PixelPaint: Undo and redo actions

The most used feature of any image editor, undo. Each tool now
notifies the ImageEditor that they completed an action, where
it'll take a snapshot if its current state.

For now, a snapshot is just a copy of the whole image and its
layers. There's a hard limit on the amount of actions it stores.
This commit is contained in:
BenJilks 2020-10-17 16:47:34 +00:00 committed by Andreas Kling
parent 5aeab9878e
commit 1c27568ab0
21 changed files with 256 additions and 2 deletions

View file

@ -75,6 +75,28 @@ void Image::add_layer(NonnullRefPtr<Layer> layer)
did_modify_layer_stack();
}
RefPtr<Image> Image::take_snapshot() const
{
auto snapshot = create_with_size(m_size);
for (const auto& layer : m_layers)
snapshot->add_layer(*Layer::create_snapshot(*snapshot, layer));
return snapshot;
}
void Image::restore_snapshot(const Image& snapshot)
{
m_layers.clear();
select_layer(nullptr);
for (const auto& snapshot_layer : snapshot.m_layers) {
auto layer = Layer::create_snapshot(*this, snapshot_layer);
if (layer->is_selected())
select_layer(layer.ptr());
add_layer(*layer);
}
did_modify_layer_stack();
}
size_t Image::index_of(const Layer& layer) const
{
for (size_t i = 0; i < m_layers.size(); ++i) {
@ -157,6 +179,12 @@ void Image::remove_layer(Layer& layer)
did_modify_layer_stack();
}
void Image::select_layer(Layer* layer)
{
for (auto* client : m_clients)
client->image_select_layer(layer);
}
void Image::add_client(ImageClient& client)
{
ASSERT(!m_clients.contains(&client));