diff options
author | Fausto Tommasi <mmasito123@yahoo.com> | 2022-10-13 21:32:23 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-10-17 09:39:57 +0200 |
commit | 813ca5ebbe3fce7a65f6a1f3ef990b07ac6929a2 (patch) | |
tree | 0ca2242eb74c5f31beb3833dae821d48e701f9af /Userland/Applications | |
parent | 66c039c66fab0bd32e2c5e8a2ccd51b3c9dd5b02 (diff) | |
download | serenity-813ca5ebbe3fce7a65f6a1f3ef990b07ac6929a2.zip |
PixelPaint: Make Alt-Clicking with all tool act like the PickerTool
Refactored PickerTool functionality to the ImageEditor level and added a
flag to Tool Base Class to allow for tools to override Alt+Click
ColorPicker functionality
Diffstat (limited to 'Userland/Applications')
-rw-r--r-- | Userland/Applications/PixelPaint/ImageEditor.cpp | 28 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/ImageEditor.h | 2 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Tools/CloneTool.h | 2 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Tools/LineTool.h | 2 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Tools/PickerTool.cpp | 21 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Tools/Tool.h | 3 |
6 files changed, 40 insertions, 18 deletions
diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp index 891755f661..7e9abeedb5 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.cpp +++ b/Userland/Applications/PixelPaint/ImageEditor.cpp @@ -305,6 +305,29 @@ GUI::MouseEvent ImageEditor::event_adjusted_for_layer(GUI::MouseEvent const& eve }; } +void ImageEditor::set_editor_color_to_color_at_mouse_position(GUI::MouseEvent const& event, bool sample_all_layers = false) +{ + auto position = event.position(); + Color color; + auto layer = active_layer(); + if (sample_all_layers) { + color = image().color_at(position); + } else { + if (!layer || !layer->rect().contains(position)) + return; + color = layer->currently_edited_bitmap().get_pixel(position); + } + + // We picked a transparent pixel, do nothing. + if (!color.alpha()) + return; + + if (event.button() == GUI::MouseButton::Primary) + set_primary_color(color); + else if (event.button() == GUI::MouseButton::Secondary) + set_secondary_color(color); +} + void ImageEditor::mousedown_event(GUI::MouseEvent& event) { if (event.button() == GUI::MouseButton::Middle) { @@ -313,6 +336,11 @@ void ImageEditor::mousedown_event(GUI::MouseEvent& event) return; } + if (event.alt() && !m_active_tool->is_overriding_alt()) { + set_editor_color_to_color_at_mouse_position(event); + return; // Pick Color instead of acivating active tool when holding alt. + } + if (!m_active_tool) return; diff --git a/Userland/Applications/PixelPaint/ImageEditor.h b/Userland/Applications/PixelPaint/ImageEditor.h index 3a2e9ff937..39d0df4f5b 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.h +++ b/Userland/Applications/PixelPaint/ImageEditor.h @@ -118,6 +118,8 @@ public: Core::EventLoop& gui_event_loop() { return m_gui_event_loop; } + void set_editor_color_to_color_at_mouse_position(GUI::MouseEvent const& event, bool sample_all_layers); + private: explicit ImageEditor(NonnullRefPtr<Image>); diff --git a/Userland/Applications/PixelPaint/Tools/CloneTool.h b/Userland/Applications/PixelPaint/Tools/CloneTool.h index 5b26f4cf4d..f380564cfb 100644 --- a/Userland/Applications/PixelPaint/Tools/CloneTool.h +++ b/Userland/Applications/PixelPaint/Tools/CloneTool.h @@ -18,6 +18,8 @@ public: virtual GUI::Widget* get_properties_widget() override; virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> cursor() override; + virtual bool is_overriding_alt() override { return true; } + protected: virtual void draw_point(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& point) override; virtual void draw_line(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& start, Gfx::IntPoint const& end) override; diff --git a/Userland/Applications/PixelPaint/Tools/LineTool.h b/Userland/Applications/PixelPaint/Tools/LineTool.h index 58c8db81cc..c423a4a47f 100644 --- a/Userland/Applications/PixelPaint/Tools/LineTool.h +++ b/Userland/Applications/PixelPaint/Tools/LineTool.h @@ -28,6 +28,8 @@ public: void draw_using(GUI::Painter&, Gfx::IntPoint const& start_position, Gfx::IntPoint const& end_position, Color color, int thickness); + virtual bool is_overriding_alt() override { return true; } + private: virtual StringView tool_name() const override { return "Line Tool"sv; } diff --git a/Userland/Applications/PixelPaint/Tools/PickerTool.cpp b/Userland/Applications/PixelPaint/Tools/PickerTool.cpp index 4de38c0afc..fff91c5054 100644 --- a/Userland/Applications/PixelPaint/Tools/PickerTool.cpp +++ b/Userland/Applications/PixelPaint/Tools/PickerTool.cpp @@ -16,25 +16,10 @@ namespace PixelPaint { void PickerTool::on_mousedown(Layer* layer, MouseEvent& event) { - auto& position = event.layer_event().position(); - - Color color; - if (m_sample_all_layers) { - color = m_editor->image().color_at(position); - } else { - if (!layer || !layer->rect().contains(position)) - return; - color = layer->currently_edited_bitmap().get_pixel(position); - } - - // We picked a transparent pixel, do nothing. - if (!color.alpha()) + if (!layer) return; - - if (event.layer_event().button() == GUI::MouseButton::Primary) - m_editor->set_primary_color(color); - else if (event.layer_event().button() == GUI::MouseButton::Secondary) - m_editor->set_secondary_color(color); + auto layer_event = event.layer_event(); + m_editor->set_editor_color_to_color_at_mouse_position(layer_event, m_sample_all_layers); } GUI::Widget* PickerTool::get_properties_widget() diff --git a/Userland/Applications/PixelPaint/Tools/Tool.h b/Userland/Applications/PixelPaint/Tools/Tool.h index 3b9ac32a7b..c3140e5ebc 100644 --- a/Userland/Applications/PixelPaint/Tools/Tool.h +++ b/Userland/Applications/PixelPaint/Tools/Tool.h @@ -77,6 +77,9 @@ public: virtual StringView tool_name() const = 0; + // We only set the override_alt_key flag to true since the override is false by default. If false is desired do not call method. + virtual bool is_overriding_alt() { return false; }; + protected: Tool() = default; WeakPtr<ImageEditor> m_editor; |