diff options
author | Torstennator <engelTorsten@gmx.de> | 2022-10-08 16:55:27 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-11-07 20:58:02 +0000 |
commit | e520b9c3a388e7703dc7ae4ca72290f71398a1fb (patch) | |
tree | 971d75589dc6ff0a585c94d414b6438833d8eb60 /Userland/Applications | |
parent | e9ca641d45340775509a4efea4a7ebdc9d7b6498 (diff) | |
download | serenity-e520b9c3a388e7703dc7ae4ca72290f71398a1fb.zip |
PixelPaint: Relate cursor to brush tool size
This patch changes the cursor for the brush tool to a circle of dynamic
size to indicate the region where the tool will apply color changes.
Diffstat (limited to 'Userland/Applications')
6 files changed, 87 insertions, 6 deletions
diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp index accc41ab15..02b1878cc9 100644 --- a/Userland/Applications/PixelPaint/MainWidget.cpp +++ b/Userland/Applications/PixelPaint/MainWidget.cpp @@ -17,6 +17,7 @@ #include "ResizeImageDialog.h" #include <Applications/PixelPaint/PixelPaintWindowGML.h> #include <LibConfig/Client.h> +#include <LibCore/Debounce.h> #include <LibCore/File.h> #include <LibCore/MimeData.h> #include <LibFileSystemAccessClient/Client.h> @@ -1100,9 +1101,11 @@ ImageEditor& MainWidget::create_new_editor(NonnullRefPtr<Image> image) m_show_rulers_action->set_checked(show_rulers); }; - image_editor.on_scale_change = [this](float scale) { + image_editor.on_scale_change = Core::debounce([this](float scale) { m_zoom_combobox->set_text(String::formatted("{}%", roundf(scale * 100))); - }; + current_image_editor()->update_tool_cursor(); + }, + 100); if (image->layer_count()) image_editor.set_active_layer(&image->layer(0)); diff --git a/Userland/Applications/PixelPaint/Tools/BrushTool.cpp b/Userland/Applications/PixelPaint/Tools/BrushTool.cpp index 88568da916..a784bdd12e 100644 --- a/Userland/Applications/PixelPaint/Tools/BrushTool.cpp +++ b/Userland/Applications/PixelPaint/Tools/BrushTool.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com> * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org> + * Copyright (c) 2022, Torsten Engelmann <engelTorsten@gmx.de> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -13,11 +14,20 @@ #include <LibGUI/Label.h> #include <LibGUI/Painter.h> #include <LibGUI/ValueSlider.h> +#include <LibGfx/AntiAliasingPainter.h> #include <LibGfx/Color.h> #include <LibGfx/Rect.h> namespace PixelPaint { +void BrushTool::set_size(int size) +{ + if (size == m_size) + return; + m_size = size; + refresh_editor_cursor(); +} + void BrushTool::on_mousedown(Layer* layer, MouseEvent& event) { if (!layer) @@ -145,9 +155,12 @@ GUI::Widget* BrushTool::get_properties_widget() auto& size_slider = size_container.add<GUI::ValueSlider>(Orientation::Horizontal, "px"); size_slider.set_range(1, 100); size_slider.set_value(m_size); + size_slider.set_override_cursor(cursor()); size_slider.on_change = [&](int value) { set_size(value); + // Update cursor to provide an instant preview for the selected size. + size_slider.set_override_cursor(cursor()); }; set_primary_slider(&size_slider); @@ -172,4 +185,30 @@ GUI::Widget* BrushTool::get_properties_widget() return m_properties_widget.ptr(); } +NonnullRefPtr<Gfx::Bitmap> BrushTool::build_cursor() +{ + m_scale_last_created_cursor = m_editor ? m_editor->scale() : 1; + auto scaled_size = size() * m_scale_last_created_cursor; + auto containing_box_size = 2 * scaled_size; + NonnullRefPtr<Gfx::Bitmap> new_cursor = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, Gfx::IntSize(containing_box_size, containing_box_size)).release_value_but_fixme_should_propagate_errors(); + + Gfx::Painter painter { new_cursor }; + Gfx::AntiAliasingPainter aa_painter { painter }; + + painter.draw_line({ scaled_size - 5, scaled_size }, { scaled_size + 5, scaled_size }, Color::LightGray, 3); + painter.draw_line({ scaled_size, scaled_size - 5 }, { scaled_size, scaled_size + 5 }, Color::LightGray, 3); + painter.draw_line({ scaled_size - 5, scaled_size }, { scaled_size + 5, scaled_size }, Color::MidGray, 1); + painter.draw_line({ scaled_size, scaled_size - 5 }, { scaled_size, scaled_size + 5 }, Color::MidGray, 1); + aa_painter.draw_ellipse(Gfx::IntRect(0, 0, containing_box_size, containing_box_size), Color::LightGray, 1); + + return new_cursor; +} + +void BrushTool::refresh_editor_cursor() +{ + m_cursor = build_cursor(); + if (m_editor) + m_editor->update_tool_cursor(); +} + } diff --git a/Userland/Applications/PixelPaint/Tools/BrushTool.h b/Userland/Applications/PixelPaint/Tools/BrushTool.h index 27139bdf9f..0f23b93dfc 100644 --- a/Userland/Applications/PixelPaint/Tools/BrushTool.h +++ b/Userland/Applications/PixelPaint/Tools/BrushTool.h @@ -2,12 +2,14 @@ * Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com> * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org> * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2022, Torsten Engelmann <engelTorsten@gmx.de> * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once +#include "../ImageEditor.h" #include "Tool.h" namespace PixelPaint { @@ -21,9 +23,14 @@ public: virtual void on_mousemove(Layer*, MouseEvent&) override; virtual void on_mouseup(Layer*, MouseEvent&) override; virtual GUI::Widget* get_properties_widget() override; - virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> cursor() override { return Gfx::StandardCursor::Crosshair; } + virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> cursor() override + { + if (m_editor && m_editor->scale() != m_scale_last_created_cursor) + refresh_editor_cursor(); + return m_cursor; + } - void set_size(int size) { m_size = size; } + void set_size(int size); int size() const { return m_size; } void set_hardness(int hardness) { m_hardness = hardness; } @@ -41,6 +48,9 @@ protected: virtual Color color_for(GUI::MouseEvent const& event); virtual void draw_point(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& point); virtual void draw_line(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& start, Gfx::IntPoint const& end); + virtual NonnullRefPtr<Gfx::Bitmap> build_cursor(); + void refresh_editor_cursor(); + float m_scale_last_created_cursor = 0; private: RefPtr<GUI::Widget> m_properties_widget; @@ -49,6 +59,7 @@ private: bool m_was_drawing { false }; bool m_has_clicked { false }; Gfx::IntPoint m_last_position; + NonnullRefPtr<Gfx::Bitmap> m_cursor = build_cursor(); }; } diff --git a/Userland/Applications/PixelPaint/Tools/EraseTool.cpp b/Userland/Applications/PixelPaint/Tools/EraseTool.cpp index 01fb66b855..48d489186f 100644 --- a/Userland/Applications/PixelPaint/Tools/EraseTool.cpp +++ b/Userland/Applications/PixelPaint/Tools/EraseTool.cpp @@ -73,6 +73,7 @@ GUI::Widget* EraseTool::get_properties_widget() size_slider.on_change = [&](int value) { set_size(value); + size_slider.set_override_cursor(cursor()); }; set_primary_slider(&size_slider); @@ -119,10 +120,14 @@ GUI::Widget* EraseTool::get_properties_widget() pencil_mode_radio.on_checked = [&](bool) { m_draw_mode = DrawMode::Pencil; hardness_slider.set_enabled(false); + refresh_editor_cursor(); + size_slider.set_override_cursor(cursor()); }; brush_mode_radio.on_checked = [&](bool) { m_draw_mode = DrawMode::Brush; hardness_slider.set_enabled(true); + refresh_editor_cursor(); + size_slider.set_override_cursor(cursor()); }; pencil_mode_radio.set_checked(true); @@ -131,4 +136,26 @@ GUI::Widget* EraseTool::get_properties_widget() return m_properties_widget.ptr(); } +NonnullRefPtr<Gfx::Bitmap> EraseTool::build_cursor() +{ + if (m_draw_mode == DrawMode::Brush) + return BrushTool::build_cursor(); + + m_scale_last_created_cursor = m_editor ? m_editor->scale() : 1; + int scaled_size = size() * m_scale_last_created_cursor; + + NonnullRefPtr<Gfx::Bitmap> new_cursor = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, Gfx::IntSize(scaled_size, scaled_size)).release_value_but_fixme_should_propagate_errors(); + + Gfx::IntRect rect { 0, 0, scaled_size, scaled_size }; + Gfx::Painter painter { new_cursor }; + + painter.draw_rect(rect, Color::LightGray); + painter.draw_line({ scaled_size / 2 - 5, scaled_size / 2 }, { scaled_size / 2 + 5, scaled_size / 2 }, Color::LightGray, 3); + painter.draw_line({ scaled_size / 2, scaled_size / 2 - 5 }, { scaled_size / 2, scaled_size / 2 + 5 }, Color::LightGray, 3); + painter.draw_line({ scaled_size / 2 - 5, scaled_size / 2 }, { scaled_size / 2 + 5, scaled_size / 2 }, Color::MidGray, 1); + painter.draw_line({ scaled_size / 2, scaled_size / 2 - 5 }, { scaled_size / 2, scaled_size / 2 + 5 }, Color::MidGray, 1); + + return new_cursor; +} + } diff --git a/Userland/Applications/PixelPaint/Tools/EraseTool.h b/Userland/Applications/PixelPaint/Tools/EraseTool.h index 6797c0211f..41b6371d46 100644 --- a/Userland/Applications/PixelPaint/Tools/EraseTool.h +++ b/Userland/Applications/PixelPaint/Tools/EraseTool.h @@ -25,6 +25,7 @@ public: protected: virtual Color color_for(GUI::MouseEvent const& event) override; virtual void draw_point(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& point) override; + virtual NonnullRefPtr<Gfx::Bitmap> build_cursor() override; private: virtual StringView tool_name() const override { return "Erase Tool"sv; } @@ -35,7 +36,7 @@ private: Pencil, Brush, }; - DrawMode m_draw_mode { DrawMode::Brush }; + DrawMode m_draw_mode { DrawMode::Pencil }; bool m_use_secondary_color { false }; }; diff --git a/Userland/Applications/PixelPaint/Tools/PenTool.h b/Userland/Applications/PixelPaint/Tools/PenTool.h index fb94242eb5..1799a66c86 100644 --- a/Userland/Applications/PixelPaint/Tools/PenTool.h +++ b/Userland/Applications/PixelPaint/Tools/PenTool.h @@ -18,7 +18,7 @@ class PenTool final : public BrushTool { public: PenTool(); virtual ~PenTool() override = default; - + virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> cursor() override { return Gfx::StandardCursor::Crosshair; } virtual GUI::Widget* get_properties_widget() override; protected: |