WandSelectTool.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "WandSelectTool.h"
  9. #include "../ImageEditor.h"
  10. #include "../Layer.h"
  11. #include <AK/Queue.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Button.h>
  14. #include <LibGUI/ComboBox.h>
  15. #include <LibGUI/ItemListModel.h>
  16. #include <LibGUI/Label.h>
  17. #include <LibGUI/Model.h>
  18. #include <LibGUI/Painter.h>
  19. #include <LibGUI/ValueSlider.h>
  20. namespace PixelPaint {
  21. static void set_flood_selection(Gfx::Bitmap& bitmap, Image& image, Gfx::IntPoint const& start_position, Gfx::IntPoint const& selection_offset, int threshold, Selection::MergeMode merge_mode)
  22. {
  23. VERIFY(bitmap.bpp() == 32);
  24. Mask selection_mask = Mask::empty(bitmap.rect());
  25. auto pixel_reached = [&](Gfx::IntPoint location) {
  26. selection_mask.set(selection_offset.x() + location.x(), selection_offset.y() + location.y(), 0xFF);
  27. };
  28. bitmap.flood_visit_from_point(start_position, threshold, move(pixel_reached));
  29. selection_mask.shrink_to_fit();
  30. image.selection().merge(selection_mask, merge_mode);
  31. }
  32. void WandSelectTool::on_mousedown(Layer* layer, MouseEvent& event)
  33. {
  34. if (!layer)
  35. return;
  36. auto& layer_event = event.layer_event();
  37. if (!layer->rect().contains(layer_event.position()))
  38. return;
  39. auto selection_offset = layer->relative_rect().top_left();
  40. m_editor->image().selection().begin_interactive_selection();
  41. set_flood_selection(layer->currently_edited_bitmap(), m_editor->image(), layer_event.position(), selection_offset, m_threshold, m_merge_mode);
  42. m_editor->image().selection().end_interactive_selection();
  43. m_editor->update();
  44. m_editor->did_complete_action(tool_name());
  45. }
  46. GUI::Widget* WandSelectTool::get_properties_widget()
  47. {
  48. if (m_properties_widget) {
  49. return m_properties_widget.ptr();
  50. }
  51. m_properties_widget = GUI::Widget::construct();
  52. m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
  53. auto& threshold_container = m_properties_widget->add<GUI::Widget>();
  54. threshold_container.set_fixed_height(20);
  55. threshold_container.set_layout<GUI::HorizontalBoxLayout>();
  56. auto& threshold_label = threshold_container.add<GUI::Label>("Threshold:");
  57. threshold_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  58. threshold_label.set_fixed_size(80, 20);
  59. auto& threshold_slider = threshold_container.add<GUI::ValueSlider>(Orientation::Horizontal, "%");
  60. threshold_slider.set_range(0, 100);
  61. threshold_slider.set_value(m_threshold);
  62. threshold_slider.on_change = [&](int value) {
  63. m_threshold = value;
  64. };
  65. set_primary_slider(&threshold_slider);
  66. auto& mode_container = m_properties_widget->add<GUI::Widget>();
  67. mode_container.set_fixed_height(20);
  68. mode_container.set_layout<GUI::HorizontalBoxLayout>();
  69. auto& mode_label = mode_container.add<GUI::Label>();
  70. mode_label.set_text("Mode:");
  71. mode_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  72. mode_label.set_fixed_size(80, 20);
  73. for (int i = 0; i < (int)Selection::MergeMode::__Count; i++) {
  74. switch ((Selection::MergeMode)i) {
  75. case Selection::MergeMode::Set:
  76. m_merge_mode_names.append("Set");
  77. break;
  78. case Selection::MergeMode::Add:
  79. m_merge_mode_names.append("Add");
  80. break;
  81. case Selection::MergeMode::Subtract:
  82. m_merge_mode_names.append("Subtract");
  83. break;
  84. case Selection::MergeMode::Intersect:
  85. m_merge_mode_names.append("Intersect");
  86. break;
  87. default:
  88. VERIFY_NOT_REACHED();
  89. }
  90. }
  91. auto& mode_combo = mode_container.add<GUI::ComboBox>();
  92. mode_combo.set_only_allow_values_from_model(true);
  93. mode_combo.set_model(*GUI::ItemListModel<String>::create(m_merge_mode_names));
  94. mode_combo.set_selected_index((int)m_merge_mode);
  95. mode_combo.on_change = [this](auto&&, GUI::ModelIndex const& index) {
  96. VERIFY(index.row() >= 0);
  97. VERIFY(index.row() < (int)Selection::MergeMode::__Count);
  98. m_merge_mode = (Selection::MergeMode)index.row();
  99. };
  100. return m_properties_widget.ptr();
  101. }
  102. }