mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:37:45 +00:00
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)
This commit is contained in:
parent
30df74b015
commit
fbfa6543ab
2 changed files with 29 additions and 13 deletions
|
@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue