diff options
author | Mustafa Quraish <mustafaq9@gmail.com> | 2021-09-07 23:07:47 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-09 11:35:50 +0200 |
commit | fbfa6543ab210bc909ed6f1467a3e10612ee459f (patch) | |
tree | 9d0f72c3896de2ee26798532b01f575ea80e893e /Userland | |
parent | 30df74b015c1f8cd8904730eaa98014146b80ae2 (diff) | |
download | serenity-fbfa6543ab210bc909ed6f1467a3e10612ee459f.zip |
PixelPaint: Reoriganize drawing in BrushTool to allow more code reuse
The BrushTool is very cool, but it doesn't allow us to re-use any
of the code in other classes. Many of the other tools have duplicated
code for handling mouse events / keeping track of previous location,
etc.
This commit sets up BrushTool so that other tools can inherit from
it and override some virtual functions to allow similar behavior
without re-writing the code to keep track of mouse positions, etc.
In particular, we add public setters/getters for `size` and
`hardness` properties, and make `draw_point()` and `draw_line()`
virtual so that derived classes can override them.
Note: We still pass in `color` as a parameter for `draw_line()` and
`draw_point()` instead of using `color_for()` directly because it
doesn't really make sense to be constantly asking the ImageEditor
for the color when it's not really changing (for instance along all
the points of a line)
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/PixelPaint/BrushTool.cpp | 25 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/BrushTool.h | 17 |
2 files changed, 29 insertions, 13 deletions
diff --git a/Userland/Applications/PixelPaint/BrushTool.cpp b/Userland/Applications/PixelPaint/BrushTool.cpp index 7591506521..3388e84ebf 100644 --- a/Userland/Applications/PixelPaint/BrushTool.cpp +++ b/Userland/Applications/PixelPaint/BrushTool.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com> + * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -34,8 +35,9 @@ void BrushTool::on_mousedown(Layer* layer, MouseEvent& event) if (layer_event.button() != GUI::MouseButton::Left && layer_event.button() != GUI::MouseButton::Right) return; + // Shift+Click draws a line from the last position to current one. if (layer_event.shift() && m_has_clicked) { - draw_line(layer->bitmap(), m_editor->color_for(layer_event), m_last_position, layer_event.position()); + draw_line(layer->bitmap(), color_for(layer_event), m_last_position, layer_event.position()); auto modified_rect = Gfx::IntRect::from_two_points(m_last_position, layer_event.position()).inflated(m_size * 2, m_size * 2); layer->did_modify_bitmap(modified_rect); m_last_position = layer_event.position(); @@ -45,7 +47,7 @@ void BrushTool::on_mousedown(Layer* layer, MouseEvent& event) const int first_draw_opacity = 10; for (int i = 0; i < first_draw_opacity; ++i) - draw_point(layer->bitmap(), m_editor->color_for(layer_event), layer_event.position()); + draw_point(layer->bitmap(), color_for(layer_event), layer_event.position()); layer->did_modify_bitmap(Gfx::IntRect::centered_on(layer_event.position(), Gfx::IntSize { m_size * 2, m_size * 2 })); m_last_position = layer_event.position(); @@ -61,7 +63,7 @@ void BrushTool::on_mousemove(Layer* layer, MouseEvent& event) if (!(layer_event.buttons() & GUI::MouseButton::Left || layer_event.buttons() & GUI::MouseButton::Right)) return; - draw_line(layer->bitmap(), m_editor->color_for(layer_event), m_last_position, layer_event.position()); + draw_line(layer->bitmap(), color_for(layer_event), m_last_position, layer_event.position()); auto modified_rect = Gfx::IntRect::from_two_points(m_last_position, layer_event.position()).inflated(m_size * 2, m_size * 2); @@ -78,17 +80,22 @@ void BrushTool::on_mouseup(Layer*, MouseEvent&) } } +Color BrushTool::color_for(GUI::MouseEvent const& event) +{ + return m_editor->color_for(event); +} + void BrushTool::draw_point(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& point) { - for (int y = point.y() - m_size; y < point.y() + m_size; y++) { - for (int x = point.x() - m_size; x < point.x() + m_size; x++) { + for (int y = point.y() - size(); y < point.y() + size(); y++) { + for (int x = point.x() - size(); x < point.x() + size(); x++) { auto distance = point.distance_from({ x, y }); if (x < 0 || x >= bitmap.width() || y < 0 || y >= bitmap.height()) continue; - if (distance >= m_size) + if (distance >= size()) continue; - auto falloff = (1.0 - double { distance / m_size }) * (1.0 / (100 - m_hardness)); + auto falloff = (1.0 - double { distance / size() }) * (1.0 / (100 - hardness())); auto pixel_color = color; pixel_color.set_alpha(falloff * 255); bitmap.set_pixel(x, y, bitmap.get_pixel(x, y).blend(pixel_color)); @@ -148,7 +155,7 @@ GUI::Widget* BrushTool::get_properties_widget() size_slider.set_value(m_size); size_slider.on_change = [&](int value) { - m_size = value; + set_size(value); }; set_primary_slider(&size_slider); @@ -165,7 +172,7 @@ GUI::Widget* BrushTool::get_properties_widget() hardness_slider.set_value(m_hardness); hardness_slider.on_change = [&](int value) { - m_hardness = value; + set_hardness(value); }; set_secondary_slider(&hardness_slider); } diff --git a/Userland/Applications/PixelPaint/BrushTool.h b/Userland/Applications/PixelPaint/BrushTool.h index 0964d0d17e..ab6b95c0cd 100644 --- a/Userland/Applications/PixelPaint/BrushTool.h +++ b/Userland/Applications/PixelPaint/BrushTool.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com> + * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,7 +11,7 @@ namespace PixelPaint { -class BrushTool final : public Tool { +class BrushTool : public Tool { public: BrushTool(); virtual ~BrushTool() override; @@ -21,6 +22,17 @@ public: virtual GUI::Widget* get_properties_widget() override; virtual Gfx::StandardCursor cursor() override { return Gfx::StandardCursor::Crosshair; } + void set_size(int size) { m_size = size; } + int size() const { return m_size; } + + void set_hardness(int hardness) { m_hardness = hardness; } + int hardness() const { return m_hardness; } + +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); + private: RefPtr<GUI::Widget> m_properties_widget; int m_size { 20 }; @@ -28,9 +40,6 @@ private: bool m_was_drawing { false }; bool m_has_clicked { false }; Gfx::IntPoint m_last_position; - - void draw_line(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& start, Gfx::IntPoint const& end); - void draw_point(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& point); }; } |