From 160bd33874c60048f7e14a40edbb66b242649b0d Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Tue, 7 Sep 2021 23:16:13 -0400 Subject: 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. --- Userland/Applications/PixelPaint/PenTool.cpp | 77 ++++++++-------------------- Userland/Applications/PixelPaint/PenTool.h | 15 +++--- 2 files changed, 27 insertions(+), 65 deletions(-) (limited to 'Userland/Applications') diff --git a/Userland/Applications/PixelPaint/PenTool.cpp b/Userland/Applications/PixelPaint/PenTool.cpp index 976255bee2..df1e4027ce 100644 --- a/Userland/Applications/PixelPaint/PenTool.cpp +++ b/Userland/Applications/PixelPaint/PenTool.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Mustafa Quraish * * 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(); - auto& thickness_container = m_properties_widget->add(); - thickness_container.set_fixed_height(20); - thickness_container.set_layout(); + auto& size_container = m_properties_widget->add(); + size_container.set_fixed_height(20); + size_container.set_layout(); - auto& thickness_label = thickness_container.add("Thickness:"); - thickness_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); - thickness_label.set_fixed_size(80, 20); + auto& size_label = size_container.add("Thickness:"); + size_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); + size_label.set_fixed_size(80, 20); - auto& thickness_slider = thickness_container.add(Orientation::Horizontal, "px"); - thickness_slider.set_range(1, 20); - thickness_slider.set_value(m_thickness); + auto& size_slider = size_container.add(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(); diff --git a/Userland/Applications/PixelPaint/PenTool.h b/Userland/Applications/PixelPaint/PenTool.h index e6430c5f9b..8cf1285914 100644 --- a/Userland/Applications/PixelPaint/PenTool.h +++ b/Userland/Applications/PixelPaint/PenTool.h @@ -1,32 +1,31 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Mustafa Quraish * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once -#include "Tool.h" +#include "BrushTool.h" #include #include 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 m_properties_widget; - int m_thickness { 1 }; }; } -- cgit v1.2.3