PolygonalSelectTool.cpp 8.9 KB

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