1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:07:34 +00:00

PixelPaint: Make PenTool inherit from BrushTool

Most of the logic implemented in PenTool was the same as BrushTool
anyway, with the only difference being how the actual lines were
drawn at the end. We now just override the `draw_line()` and
`draw_point()` methods instead.

We don't strictly need to override `draw_line()` here, but that
would just result in repeated calls to `draw_point()`, which is
wasteful.

Also renamed "thickness"->"size" to have consistent terminology.
This commit is contained in:
Mustafa Quraish 2021-09-07 23:16:13 -04:00 committed by Andreas Kling
parent fbfa6543ab
commit 160bd33874
2 changed files with 27 additions and 65 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -18,61 +19,23 @@ namespace PixelPaint {
PenTool::PenTool()
{
set_size(1);
}
PenTool::~PenTool()
{
}
void PenTool::on_mousedown(Layer* layer, MouseEvent& event)
void PenTool::draw_point(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& point)
{
if (!layer)
return;
auto& layer_event = event.layer_event();
if (layer_event.button() != GUI::MouseButton::Left && layer_event.button() != GUI::MouseButton::Right)
return;
GUI::Painter painter(layer->bitmap());
painter.draw_line(layer_event.position(), layer_event.position(), m_editor->color_for(layer_event), m_thickness);
layer->did_modify_bitmap(Gfx::IntRect::centered_on(layer_event.position(), Gfx::IntSize { m_thickness + 2, m_thickness + 2 }));
m_last_drawing_event_position = layer_event.position();
GUI::Painter painter(bitmap);
painter.draw_line(point, point, color, size());
}
void PenTool::on_mouseup(Layer* layer, MouseEvent& event)
void PenTool::draw_line(Gfx::Bitmap& bitmap, Gfx::Color const& color, Gfx::IntPoint const& start, Gfx::IntPoint const& end)
{
if (!layer)
return;
auto& layer_event = event.layer_event();
if (layer_event.button() == GUI::MouseButton::Left || layer_event.button() == GUI::MouseButton::Right) {
m_last_drawing_event_position = { -1, -1 };
m_editor->did_complete_action();
}
}
void PenTool::on_mousemove(Layer* layer, MouseEvent& event)
{
if (!layer)
return;
auto& layer_event = event.layer_event();
if (!(layer_event.buttons() & GUI::MouseButton::Left || layer_event.buttons() & GUI::MouseButton::Right))
return;
GUI::Painter painter(layer->bitmap());
Gfx::IntRect changed_rect;
if (m_last_drawing_event_position != Gfx::IntPoint(-1, -1)) {
painter.draw_line(m_last_drawing_event_position, layer_event.position(), m_editor->color_for(layer_event), m_thickness);
changed_rect = Gfx::IntRect::from_two_points(m_last_drawing_event_position, layer_event.position());
} else {
painter.draw_line(layer_event.position(), layer_event.position(), m_editor->color_for(layer_event), m_thickness);
changed_rect = Gfx::IntRect::from_two_points(layer_event.position(), layer_event.position());
}
changed_rect.inflate(m_thickness + 2, m_thickness + 2);
layer->did_modify_bitmap(changed_rect);
m_last_drawing_event_position = layer_event.position();
GUI::Painter painter(bitmap);
painter.draw_line(start, end, color, size());
}
GUI::Widget* PenTool::get_properties_widget()
@ -81,22 +44,22 @@ GUI::Widget* PenTool::get_properties_widget()
m_properties_widget = GUI::Widget::construct();
m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
auto& thickness_container = m_properties_widget->add<GUI::Widget>();
thickness_container.set_fixed_height(20);
thickness_container.set_layout<GUI::HorizontalBoxLayout>();
auto& size_container = m_properties_widget->add<GUI::Widget>();
size_container.set_fixed_height(20);
size_container.set_layout<GUI::HorizontalBoxLayout>();
auto& thickness_label = thickness_container.add<GUI::Label>("Thickness:");
thickness_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
thickness_label.set_fixed_size(80, 20);
auto& size_label = size_container.add<GUI::Label>("Thickness:");
size_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
size_label.set_fixed_size(80, 20);
auto& thickness_slider = thickness_container.add<GUI::ValueSlider>(Orientation::Horizontal, "px");
thickness_slider.set_range(1, 20);
thickness_slider.set_value(m_thickness);
auto& size_slider = size_container.add<GUI::ValueSlider>(Orientation::Horizontal, "px");
size_slider.set_range(1, 20);
size_slider.set_value(size());
thickness_slider.on_change = [&](int value) {
m_thickness = value;
size_slider.on_change = [&](int value) {
set_size(value);
};
set_primary_slider(&thickness_slider);
set_primary_slider(&size_slider);
}
return m_properties_widget.ptr();

View file

@ -1,32 +1,31 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "Tool.h"
#include "BrushTool.h"
#include <LibGUI/ActionGroup.h>
#include <LibGfx/Point.h>
namespace PixelPaint {
class PenTool final : public Tool {
class PenTool final : public BrushTool {
public:
PenTool();
virtual ~PenTool() override;
virtual void on_mousedown(Layer*, MouseEvent&) override;
virtual void on_mousemove(Layer*, MouseEvent&) override;
virtual void on_mouseup(Layer*, MouseEvent&) override;
virtual GUI::Widget* get_properties_widget() override;
virtual Gfx::StandardCursor cursor() override { return Gfx::StandardCursor::Crosshair; }
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;
private:
Gfx::IntPoint m_last_drawing_event_position { -1, -1 };
RefPtr<GUI::Widget> m_properties_widget;
int m_thickness { 1 };
};
}