Serenity Operating System
1/*
2 * Copyright (c) 2022-2023, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include "PolygonalSelectTool.h"
8#include "../ImageEditor.h"
9#include "../Layer.h"
10#include <AK/Queue.h>
11#include <LibGUI/BoxLayout.h>
12#include <LibGUI/Button.h>
13#include <LibGUI/ComboBox.h>
14#include <LibGUI/ItemListModel.h>
15#include <LibGUI/Label.h>
16#include <LibGUI/Model.h>
17#include <LibGUI/Painter.h>
18#include <LibGUI/ValueSlider.h>
19
20namespace PixelPaint {
21
22void PolygonalSelectTool::flood_polygon_selection(Gfx::Bitmap& polygon_bitmap, Gfx::IntPoint polygon_delta)
23{
24 VERIFY(polygon_bitmap.bpp() == 32);
25
26 // Create Mask which will track already-processed pixels.
27 Mask selection_mask = Mask::full(polygon_bitmap.rect().translated(polygon_delta));
28
29 auto pixel_reached = [&](Gfx::IntPoint location) {
30 selection_mask.set(Gfx::IntPoint(location.x(), location.y()).translated(polygon_delta), 0);
31 };
32
33 polygon_bitmap.flood_visit_from_point({ polygon_bitmap.width() - 1, polygon_bitmap.height() - 1 }, 0, move(pixel_reached));
34
35 selection_mask.shrink_to_fit();
36 m_editor->image().selection().merge(selection_mask, m_merge_mode);
37}
38
39void PolygonalSelectTool::process_polygon()
40{
41 // Determine minimum bounding box that can hold the polygon.
42 auto min_x_seen = m_polygon_points.at(0).x();
43 auto max_x_seen = m_polygon_points.at(0).x();
44 auto min_y_seen = m_polygon_points.at(0).y();
45 auto max_y_seen = m_polygon_points.at(0).y();
46
47 for (auto point : m_polygon_points) {
48 if (point.x() < min_x_seen)
49 min_x_seen = point.x();
50 if (point.x() > max_x_seen)
51 max_x_seen = point.x();
52 if (point.y() < min_y_seen)
53 min_y_seen = point.y();
54 if (point.y() > max_y_seen)
55 max_y_seen = point.y();
56 }
57
58 // We create a bitmap that is bigger by 1 pixel on each side (+2) and need to account for the 0 indexed
59 // pixel positions (+1) so we make the bitmap size the delta of x/y min/max + 3.
60 auto polygon_bitmap_or_error = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { (max_x_seen - min_x_seen) + 3, (max_y_seen - min_y_seen) + 3 });
61 if (polygon_bitmap_or_error.is_error())
62 return;
63
64 auto polygon_bitmap = polygon_bitmap_or_error.release_value();
65
66 auto polygon_painter = Gfx::Painter(polygon_bitmap);
67 // We want to paint the polygon into the bitmap such that there is an empty 1px border all the way around it
68 // this ensures that we have a known pixel (0,0) that is outside the polygon. Since the coordinates are relative
69 // to the layer but the bitmap is cropped to the bounding rect of the polygon we need to offset our
70 // points by the the negative of min x/y. And because we want a 1 px offset to the right and down, we + 1 this.
71 auto polygon_bitmap_delta = Gfx::IntPoint(-min_x_seen + 1, -min_y_seen + 1);
72 polygon_painter.translate(polygon_bitmap_delta);
73 for (size_t i = 0; i < m_polygon_points.size() - 1; i++) {
74 polygon_painter.draw_line(m_polygon_points.at(i), m_polygon_points.at(i + 1), Color::Black);
75 }
76 polygon_painter.draw_line(m_polygon_points.at(m_polygon_points.size() - 1), m_polygon_points.at(0), Color::Black);
77
78 // Delta to use for mapping the bitmap back to layer coordinates. -1 to account for the right and down offset.
79 auto bitmap_to_layer_delta = Gfx::IntPoint(min_x_seen + m_editor->active_layer()->location().x() - 1, min_y_seen + m_editor->active_layer()->location().y() - 1);
80 flood_polygon_selection(polygon_bitmap, bitmap_to_layer_delta);
81}
82void PolygonalSelectTool::on_mousedown(Layer*, MouseEvent& event)
83{
84 auto& image_event = event.image_event();
85 if (image_event.button() != GUI::MouseButton::Primary)
86 return;
87 if (!m_selecting) {
88 m_polygon_points.clear();
89 m_last_selecting_cursor_position = event.layer_event().position();
90 }
91
92 m_selecting = true;
93
94 auto new_point = event.layer_event().position();
95 if (!m_polygon_points.is_empty() && event.layer_event().shift())
96 new_point = Tool::constrain_line_angle(m_polygon_points.last(), new_point);
97
98 // This point matches the first point exactly. Consider this polygon finished.
99 if (m_polygon_points.size() > 0 && new_point == m_polygon_points.at(0)) {
100 m_selecting = false;
101 m_editor->image().selection().end_interactive_selection();
102 process_polygon();
103 m_editor->did_complete_action(tool_name());
104 m_editor->update();
105 return;
106 }
107
108 // Avoid adding the same point multiple times if the user clicks again without moving the mouse.
109 if (m_polygon_points.size() > 0 && m_polygon_points.at(m_polygon_points.size() - 1) == new_point)
110 return;
111
112 m_polygon_points.append(new_point);
113 m_editor->image().selection().begin_interactive_selection();
114
115 m_editor->update();
116}
117
118void PolygonalSelectTool::on_mousemove(Layer*, MouseEvent& event)
119{
120 if (!m_selecting)
121 return;
122
123 if (event.layer_event().shift())
124 m_last_selecting_cursor_position = Tool::constrain_line_angle(m_polygon_points.last(), event.layer_event().position());
125 else
126 m_last_selecting_cursor_position = event.layer_event().position();
127
128 m_editor->update();
129}
130
131void PolygonalSelectTool::on_doubleclick(Layer*, MouseEvent&)
132{
133 m_selecting = false;
134 m_editor->image().selection().end_interactive_selection();
135 process_polygon();
136 m_editor->did_complete_action(tool_name());
137 m_editor->update();
138}
139
140void PolygonalSelectTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
141{
142 if (!m_selecting)
143 return;
144
145 GUI::Painter painter(*m_editor);
146 painter.add_clip_rect(event.rect());
147
148 painter.translate(editor_layer_location(*layer));
149
150 auto draw_preview_lines = [&](auto color, auto thickness) {
151 for (size_t i = 0; i < m_polygon_points.size() - 1; i++) {
152 auto preview_start = editor_stroke_position(m_polygon_points.at(i), 1);
153 auto preview_end = editor_stroke_position(m_polygon_points.at(i + 1), 1);
154 painter.draw_line(preview_start, preview_end, color, thickness);
155 }
156
157 auto last_line_start = editor_stroke_position(m_polygon_points.at(m_polygon_points.size() - 1), 1);
158 auto last_line_stop = editor_stroke_position(m_last_selecting_cursor_position, 1);
159 painter.draw_line(last_line_start, last_line_stop, color, thickness);
160 };
161
162 draw_preview_lines(Gfx::Color::Black, 3);
163 draw_preview_lines(Gfx::Color::White, 1);
164}
165
166bool PolygonalSelectTool::on_keydown(GUI::KeyEvent& key_event)
167{
168 if (key_event.key() == KeyCode::Key_Escape) {
169 if (m_selecting) {
170 m_selecting = false;
171 m_polygon_points.clear();
172 } else {
173 m_editor->image().selection().clear();
174 }
175 return true;
176 }
177 return Tool::on_keydown(key_event);
178}
179
180ErrorOr<GUI::Widget*> PolygonalSelectTool::get_properties_widget()
181{
182 if (m_properties_widget)
183 return m_properties_widget.ptr();
184
185 auto properties_widget = TRY(GUI::Widget::try_create());
186 (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>());
187
188 auto mode_container = TRY(properties_widget->try_add<GUI::Widget>());
189 mode_container->set_fixed_height(20);
190 (void)TRY(mode_container->try_set_layout<GUI::HorizontalBoxLayout>());
191
192 auto mode_label = TRY(mode_container->try_add<GUI::Label>());
193 mode_label->set_text("Mode:");
194 mode_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
195 mode_label->set_fixed_size(80, 20);
196
197 static constexpr auto s_merge_mode_names = [] {
198 Array<StringView, (int)Selection::MergeMode::__Count> names;
199 for (size_t i = 0; i < names.size(); i++) {
200 switch ((Selection::MergeMode)i) {
201 case Selection::MergeMode::Set:
202 names[i] = "Set"sv;
203 break;
204 case Selection::MergeMode::Add:
205 names[i] = "Add"sv;
206 break;
207 case Selection::MergeMode::Subtract:
208 names[i] = "Subtract"sv;
209 break;
210 case Selection::MergeMode::Intersect:
211 names[i] = "Intersect"sv;
212 break;
213 default:
214 break;
215 }
216 }
217 return names;
218 }();
219
220 auto mode_combo = TRY(mode_container->try_add<GUI::ComboBox>());
221 mode_combo->set_only_allow_values_from_model(true);
222 mode_combo->set_model(*GUI::ItemListModel<StringView, decltype(s_merge_mode_names)>::create(s_merge_mode_names));
223 mode_combo->set_selected_index((int)m_merge_mode);
224 mode_combo->on_change = [this](auto&&, GUI::ModelIndex const& index) {
225 VERIFY(index.row() >= 0);
226 VERIFY(index.row() < (int)Selection::MergeMode::__Count);
227
228 m_merge_mode = (Selection::MergeMode)index.row();
229 };
230
231 m_properties_widget = properties_widget;
232 return m_properties_widget.ptr();
233}
234
235Gfx::IntPoint PolygonalSelectTool::point_position_to_preferred_cell(Gfx::FloatPoint position) const
236{
237 return position.to_type<int>();
238}
239
240}