diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-25 22:49:50 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-26 10:18:03 +0200 |
commit | dc3de47b039ffa84c397991a386fb54786bfe53e (patch) | |
tree | 880b21ea18a63be2292a2668e64dd0184d1048bb | |
parent | de85cd09073034eb4e5df653b1cc17c5870c4afb (diff) | |
download | serenity-dc3de47b039ffa84c397991a386fb54786bfe53e.zip |
PixelPaint: Have layers and images send out notifications on changes
We use this to automatically update the thumbnail in LayerListWidget
when you draw into a layer. We also use it to repaint the ImageEditor
when the image changes somehow. :^)
-rw-r--r-- | Applications/PixelPaint/BucketTool.cpp | 2 | ||||
-rw-r--r-- | Applications/PixelPaint/EraseTool.cpp | 4 | ||||
-rw-r--r-- | Applications/PixelPaint/Image.cpp | 15 | ||||
-rw-r--r-- | Applications/PixelPaint/Image.h | 7 | ||||
-rw-r--r-- | Applications/PixelPaint/ImageEditor.cpp | 5 | ||||
-rw-r--r-- | Applications/PixelPaint/ImageEditor.h | 8 | ||||
-rw-r--r-- | Applications/PixelPaint/Layer.cpp | 6 | ||||
-rw-r--r-- | Applications/PixelPaint/Layer.h | 4 | ||||
-rw-r--r-- | Applications/PixelPaint/LayerListWidget.cpp | 2 | ||||
-rw-r--r-- | Applications/PixelPaint/LayerListWidget.h | 2 | ||||
-rw-r--r-- | Applications/PixelPaint/LineTool.cpp | 2 | ||||
-rw-r--r-- | Applications/PixelPaint/PenTool.cpp | 4 | ||||
-rw-r--r-- | Applications/PixelPaint/RectangleTool.cpp | 2 | ||||
-rw-r--r-- | Applications/PixelPaint/SprayTool.cpp | 2 |
14 files changed, 53 insertions, 12 deletions
diff --git a/Applications/PixelPaint/BucketTool.cpp b/Applications/PixelPaint/BucketTool.cpp index 6b18fb4fc2..9795f5b440 100644 --- a/Applications/PixelPaint/BucketTool.cpp +++ b/Applications/PixelPaint/BucketTool.cpp @@ -87,7 +87,7 @@ void BucketTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEv flood_fill(layer.bitmap(), event.position(), target_color, m_editor->color_for(event)); - m_editor->update(); + layer.did_modify_bitmap(*m_editor->image()); } } diff --git a/Applications/PixelPaint/EraseTool.cpp b/Applications/PixelPaint/EraseTool.cpp index 4d91580f09..6efab8c13f 100644 --- a/Applications/PixelPaint/EraseTool.cpp +++ b/Applications/PixelPaint/EraseTool.cpp @@ -59,7 +59,7 @@ void EraseTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEve Gfx::Rect r = build_rect(event.position(), layer.rect()); GUI::Painter painter(layer.bitmap()); painter.clear_rect(r, get_color()); - m_editor->update(); + layer.did_modify_bitmap(*m_editor->image()); } void EraseTool::on_mousemove(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&) @@ -68,7 +68,7 @@ void EraseTool::on_mousemove(Layer& layer, GUI::MouseEvent& event, GUI::MouseEve Gfx::Rect r = build_rect(event.position(), layer.rect()); GUI::Painter painter(layer.bitmap()); painter.clear_rect(r, get_color()); - m_editor->update(); + layer.did_modify_bitmap(*m_editor->image()); } } diff --git a/Applications/PixelPaint/Image.cpp b/Applications/PixelPaint/Image.cpp index e1a906df11..fa55c563d6 100644 --- a/Applications/PixelPaint/Image.cpp +++ b/Applications/PixelPaint/Image.cpp @@ -146,4 +146,19 @@ void Image::remove_client(ImageClient& client) m_clients.remove(&client); } +void Image::layer_did_modify_bitmap(Badge<Layer>, const Layer& layer) +{ + auto layer_index = index_of(layer); + for (auto* client : m_clients) + client->image_did_modify_layer(layer_index); + + did_change(); +} + +void Image::did_change() +{ + for (auto* client : m_clients) + client->image_did_change(); +} + } diff --git a/Applications/PixelPaint/Image.h b/Applications/PixelPaint/Image.h index d8c0b86a58..56adb3a089 100644 --- a/Applications/PixelPaint/Image.h +++ b/Applications/PixelPaint/Image.h @@ -44,7 +44,8 @@ class ImageClient { public: virtual void image_did_add_layer(size_t) { } virtual void image_did_remove_layer(size_t) { } - virtual void image_did_update_layer(size_t) { } + virtual void image_did_modify_layer(size_t) { } + virtual void image_did_change() { } }; class Image : public RefCounted<Image> { @@ -72,9 +73,13 @@ public: void add_client(ImageClient&); void remove_client(ImageClient&); + void layer_did_modify_bitmap(Badge<Layer>, const Layer&); + private: explicit Image(const Gfx::Size&); + void did_change(); + size_t index_of(const Layer&) const; Gfx::Size m_size; diff --git a/Applications/PixelPaint/ImageEditor.cpp b/Applications/PixelPaint/ImageEditor.cpp index 4d82ad0015..6a503d3bb3 100644 --- a/Applications/PixelPaint/ImageEditor.cpp +++ b/Applications/PixelPaint/ImageEditor.cpp @@ -353,4 +353,9 @@ void ImageEditor::relayout() update(); } +void ImageEditor::image_did_change() +{ + update(); +} + } diff --git a/Applications/PixelPaint/ImageEditor.h b/Applications/PixelPaint/ImageEditor.h index e9f9b9c182..ce28d3b38e 100644 --- a/Applications/PixelPaint/ImageEditor.h +++ b/Applications/PixelPaint/ImageEditor.h @@ -26,16 +26,18 @@ #pragma once +#include "Image.h" #include <LibGUI/Frame.h> #include <LibGfx/FloatPoint.h> namespace PixelPaint { -class Image; class Layer; class Tool; -class ImageEditor final : public GUI::Frame { +class ImageEditor final + : public GUI::Frame + , public ImageClient { C_OBJECT(ImageEditor); public: @@ -91,6 +93,8 @@ private: virtual void context_menu_event(GUI::ContextMenuEvent&) override; virtual void resize_event(GUI::ResizeEvent&) override; + virtual void image_did_change() override; + GUI::MouseEvent event_adjusted_for_layer(const GUI::MouseEvent&, const Layer&) const; GUI::MouseEvent event_with_pan_and_scale_applied(const GUI::MouseEvent&) const; diff --git a/Applications/PixelPaint/Layer.cpp b/Applications/PixelPaint/Layer.cpp index e633aaaf09..9c54eb7313 100644 --- a/Applications/PixelPaint/Layer.cpp +++ b/Applications/PixelPaint/Layer.cpp @@ -25,6 +25,7 @@ */ #include "Layer.h" +#include "Image.h" #include <LibGfx/Bitmap.h> namespace PixelPaint { @@ -46,4 +47,9 @@ Layer::Layer(const Gfx::Size& size, const String& name) m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, size); } +void Layer::did_modify_bitmap(Image& image) +{ + image.layer_did_modify_bitmap({}, *this); +} + } diff --git a/Applications/PixelPaint/Layer.h b/Applications/PixelPaint/Layer.h index 5eb8ed2d12..cc192cbe3f 100644 --- a/Applications/PixelPaint/Layer.h +++ b/Applications/PixelPaint/Layer.h @@ -33,6 +33,8 @@ namespace PixelPaint { +class Image; + class Layer : public RefCounted<Layer> { AK_MAKE_NONCOPYABLE(Layer); AK_MAKE_NONMOVABLE(Layer); @@ -55,6 +57,8 @@ public: const String& name() const { return m_name; } void set_name(const String& name) { m_name = name; } + void did_modify_bitmap(Image&); + private: explicit Layer(const Gfx::Size&, const String& name); diff --git a/Applications/PixelPaint/LayerListWidget.cpp b/Applications/PixelPaint/LayerListWidget.cpp index a819c911f4..42b14aee84 100644 --- a/Applications/PixelPaint/LayerListWidget.cpp +++ b/Applications/PixelPaint/LayerListWidget.cpp @@ -125,7 +125,7 @@ void LayerListWidget::image_did_remove_layer(size_t layer_index) relayout_gadgets(); } -void LayerListWidget::image_did_update_layer(size_t layer_index) +void LayerListWidget::image_did_modify_layer(size_t layer_index) { update(m_gadgets[layer_index].rect); } diff --git a/Applications/PixelPaint/LayerListWidget.h b/Applications/PixelPaint/LayerListWidget.h index 1f24883526..a1f86dc37c 100644 --- a/Applications/PixelPaint/LayerListWidget.h +++ b/Applications/PixelPaint/LayerListWidget.h @@ -52,7 +52,7 @@ private: virtual void image_did_add_layer(size_t) override; virtual void image_did_remove_layer(size_t) override; - virtual void image_did_update_layer(size_t); + virtual void image_did_modify_layer(size_t); void relayout_gadgets(); diff --git a/Applications/PixelPaint/LineTool.cpp b/Applications/PixelPaint/LineTool.cpp index 04b14b8a46..bffa6f11f1 100644 --- a/Applications/PixelPaint/LineTool.cpp +++ b/Applications/PixelPaint/LineTool.cpp @@ -77,7 +77,7 @@ void LineTool::on_mouseup(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent& GUI::Painter painter(layer.bitmap()); painter.draw_line(m_line_start_position, m_line_end_position, m_editor->color_for(m_drawing_button), m_thickness); m_drawing_button = GUI::MouseButton::None; - m_editor->update(); + layer.did_modify_bitmap(*m_editor->image()); } } diff --git a/Applications/PixelPaint/PenTool.cpp b/Applications/PixelPaint/PenTool.cpp index bf50ed1b36..0f1b7d8814 100644 --- a/Applications/PixelPaint/PenTool.cpp +++ b/Applications/PixelPaint/PenTool.cpp @@ -48,7 +48,7 @@ void PenTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent GUI::Painter painter(layer.bitmap()); painter.draw_line(event.position(), event.position(), m_editor->color_for(event), m_thickness); - m_editor->update(); + layer.did_modify_bitmap(*m_editor->image()); m_last_drawing_event_position = event.position(); } @@ -68,7 +68,7 @@ void PenTool::on_mousemove(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent painter.draw_line(m_last_drawing_event_position, event.position(), m_editor->color_for(event), m_thickness); else painter.draw_line(event.position(), event.position(), m_editor->color_for(event), m_thickness); - m_editor->update(); + layer.did_modify_bitmap(*m_editor->image()); m_last_drawing_event_position = event.position(); } diff --git a/Applications/PixelPaint/RectangleTool.cpp b/Applications/PixelPaint/RectangleTool.cpp index 2b238fa2d8..1207dc7888 100644 --- a/Applications/PixelPaint/RectangleTool.cpp +++ b/Applications/PixelPaint/RectangleTool.cpp @@ -81,7 +81,7 @@ void RectangleTool::on_mouseup(Layer& layer, GUI::MouseEvent& event, GUI::MouseE auto rect = Gfx::Rect::from_two_points(m_rectangle_start_position, m_rectangle_end_position); draw_using(painter, rect); m_drawing_button = GUI::MouseButton::None; - m_editor->update(); + layer.did_modify_bitmap(*m_editor->image()); } } diff --git a/Applications/PixelPaint/SprayTool.cpp b/Applications/PixelPaint/SprayTool.cpp index 36e92dbd85..0a685e36e0 100644 --- a/Applications/PixelPaint/SprayTool.cpp +++ b/Applications/PixelPaint/SprayTool.cpp @@ -79,6 +79,8 @@ void SprayTool::paint_it() continue; bitmap.set_pixel<Gfx::BitmapFormat::RGB32>(xpos, ypos, m_color); } + + layer->did_modify_bitmap(*m_editor->image()); } void SprayTool::on_mousedown(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&) |