summaryrefslogtreecommitdiff
path: root/Userland/Applications
diff options
context:
space:
mode:
authorMustafa Quraish <mustafaq9@gmail.com>2021-09-02 19:29:03 -0400
committerAndreas Kling <kling@serenityos.org>2021-09-03 01:50:11 +0200
commitca6c9be94cd1e62467803c108aba81fa98e9bf97 (patch)
tree1e9998133762614c50238e1c96686e2f6d504b97 /Userland/Applications
parent6a8c408856aa96215ca9c00f82c363b4908ea3d4 (diff)
downloadserenity-ca6c9be94cd1e62467803c108aba81fa98e9bf97.zip
PixelPaint: Add actions to rotate image left/right
This also required adding a new hook to `ImageClient`, since there wasn't a way of telling the ImageEditor that the full rect of the image has changed (as when we rotate).
Diffstat (limited to 'Userland/Applications')
-rw-r--r--Userland/Applications/PixelPaint/Image.cpp19
-rw-r--r--Userland/Applications/PixelPaint/Image.h4
-rw-r--r--Userland/Applications/PixelPaint/ImageEditor.cpp6
-rw-r--r--Userland/Applications/PixelPaint/ImageEditor.h1
-rw-r--r--Userland/Applications/PixelPaint/main.cpp16
5 files changed, 46 insertions, 0 deletions
diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp
index 5334f2da0c..f3e73b32d0 100644
--- a/Userland/Applications/PixelPaint/Image.cpp
+++ b/Userland/Applications/PixelPaint/Image.cpp
@@ -544,6 +544,12 @@ void Image::did_change(Gfx::IntRect const& a_modified_rect)
client->image_did_change(modified_rect);
}
+void Image::did_change_rect()
+{
+ for (auto* client : m_clients)
+ client->image_did_change_rect(rect());
+}
+
ImageUndoCommand::ImageUndoCommand(Image& image)
: m_snapshot(image.take_snapshot())
, m_image(image)
@@ -585,4 +591,17 @@ void Image::flip(Gfx::Orientation orientation)
did_change();
}
+void Image::rotate(Gfx::RotationDirection direction)
+{
+ for (auto& layer : m_layers) {
+ auto rotated = layer.bitmap().rotated(direction);
+ VERIFY(rotated);
+ layer.set_bitmap(*rotated);
+ layer.did_modify_bitmap(rect());
+ }
+
+ m_size = { m_size.height(), m_size.width() };
+ did_change_rect();
+}
+
}
diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h
index 64dc43ea6f..b681c10c13 100644
--- a/Userland/Applications/PixelPaint/Image.h
+++ b/Userland/Applications/PixelPaint/Image.h
@@ -16,6 +16,7 @@
#include <LibCore/File.h>
#include <LibGUI/Command.h>
#include <LibGUI/Forward.h>
+#include <LibGfx/Bitmap.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
@@ -32,6 +33,7 @@ public:
virtual void image_did_modify_layer_bitmap(size_t) { }
virtual void image_did_modify_layer_stack() { }
virtual void image_did_change(Gfx::IntRect const&) { }
+ virtual void image_did_change_rect(Gfx::IntRect const&) { }
virtual void image_select_layer(Layer*) { }
virtual void image_did_change_title(String const&) { }
@@ -92,6 +94,7 @@ public:
void set_title(String);
void flip(Gfx::Orientation orientation);
+ void rotate(Gfx::RotationDirection direction);
private:
explicit Image(Gfx::IntSize const&);
@@ -101,6 +104,7 @@ private:
static Result<NonnullRefPtr<Image>, String> try_create_from_pixel_paint_file(Core::File& file, String const& file_path);
void did_change(Gfx::IntRect const& modified_rect = {});
+ void did_change_rect();
void did_modify_layer_stack();
String m_path;
diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp
index 79e8c357ce..7e9d011e70 100644
--- a/Userland/Applications/PixelPaint/ImageEditor.cpp
+++ b/Userland/Applications/PixelPaint/ImageEditor.cpp
@@ -453,6 +453,12 @@ void ImageEditor::image_did_change(Gfx::IntRect const& modified_image_rect)
update(m_editor_image_rect.intersected(enclosing_int_rect(image_rect_to_editor_rect(modified_image_rect))));
}
+void ImageEditor::image_did_change_rect(Gfx::IntRect const& new_image_rect)
+{
+ m_editor_image_rect = enclosing_int_rect(image_rect_to_editor_rect(new_image_rect));
+ update(m_editor_image_rect);
+}
+
void ImageEditor::image_did_change_title(String const& path)
{
if (on_image_title_change)
diff --git a/Userland/Applications/PixelPaint/ImageEditor.h b/Userland/Applications/PixelPaint/ImageEditor.h
index 9e2de93e09..a86cd4f154 100644
--- a/Userland/Applications/PixelPaint/ImageEditor.h
+++ b/Userland/Applications/PixelPaint/ImageEditor.h
@@ -106,6 +106,7 @@ private:
virtual void leave_event(Core::Event&) override;
virtual void image_did_change(Gfx::IntRect const&) override;
+ virtual void image_did_change_rect(Gfx::IntRect const&) override;
virtual void image_select_layer(Layer*) override;
virtual void image_did_change_title(String const&) override;
diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp
index 4d212b120e..b495cbe35a 100644
--- a/Userland/Applications/PixelPaint/main.cpp
+++ b/Userland/Applications/PixelPaint/main.cpp
@@ -424,6 +424,22 @@ int main(int argc, char** argv)
editor->image().flip(Gfx::Orientation::Horizontal);
},
window));
+ image_menu.add_action(GUI::Action::create(
+ "Rotate &Left", [&](auto&) {
+ auto* editor = current_image_editor();
+ if (!editor)
+ return;
+ editor->image().rotate(Gfx::RotationDirection::CounterClockwise);
+ },
+ window));
+ image_menu.add_action(GUI::Action::create(
+ "Rotate &Right", [&](auto&) {
+ auto* editor = current_image_editor();
+ if (!editor)
+ return;
+ editor->image().rotate(Gfx::RotationDirection::Clockwise);
+ },
+ window));
auto& layer_menu = window->add_menu("&Layer");
layer_menu.add_action(GUI::Action::create(