1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:27:45 +00:00

PixelPaint: Add Polygonal Select Tool

Polygonal selection tool allows for the drawing of any arbitrary
polygonal shape. It tracks clicked points in a vector, upon double
clicking we finalize the polygon and generate the selection mask. The
user can press the escape key during selection to cancel.

The mask is generated as follows:

- First we calculate the size of the bounding rect needed to hold the
  polygon
- We add 2 pixels to height/width to allow us a 1 pixel border, the
  polygon will be centered in this bitmap
- Draw the polygon into the bitmap via Gfx::Painter, making sure to
  connect final polygon point to the first to ensure an enclosed shape
- Generate a selection mask the size of the bitmap, with all pixels
  initially selected
- Perform a flood fill from (0,0) which is guaranteed to be outside the
  polygon
- For every pixel reached by the flood fill, we clear the selected pixel
  from the selection mask
- Finally we merge the selection mask like other selection tools.
This commit is contained in:
Timothy Slater 2022-09-02 15:50:47 -05:00 committed by Linus Groh
parent 20f6485311
commit 038a833f0c
5 changed files with 269 additions and 0 deletions

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "../Selection.h"
#include "Tool.h"
#include <AK/Vector.h>
#include <LibGUI/Widget.h>
namespace PixelPaint {
class PolygonalSelectTool final : public Tool {
public:
PolygonalSelectTool() = default;
virtual ~PolygonalSelectTool() = default;
virtual void on_doubleclick(Layer*, MouseEvent& event) override;
virtual void on_mousedown(Layer*, MouseEvent& event) override;
virtual void on_mousemove(Layer*, MouseEvent& event) override;
virtual void on_keydown(GUI::KeyEvent&) override;
virtual void on_second_paint(Layer const*, GUI::PaintEvent&) override;
virtual GUI::Widget* get_properties_widget() override;
virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> cursor() override { return Gfx::StandardCursor::Crosshair; }
virtual Gfx::IntPoint point_position_to_preferred_cell(Gfx::FloatPoint const& position) const override;
private:
virtual void flood_polygon_selection(Gfx::Bitmap&, Gfx::IntPoint);
virtual void process_polygon();
virtual StringView tool_name() const override { return "Polygonal Select Tool"sv; }
RefPtr<GUI::Widget> m_properties_widget;
Selection::MergeMode m_merge_mode { Selection::MergeMode::Set };
bool m_selecting { false };
Gfx::IntPoint m_last_selecting_cursor_position;
Vector<Gfx::IntPoint> m_polygon_points {};
};
}