WandSelectTool.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 start_position, Gfx::IntPoint 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. bool WandSelectTool::on_keydown(GUI::KeyEvent& key_event)
  33. {
  34. if (key_event.key() == KeyCode::Key_Escape) {
  35. m_editor->image().selection().clear();
  36. return true;
  37. }
  38. return Tool::on_keydown(key_event);
  39. }
  40. void WandSelectTool::on_mousedown(Layer* layer, MouseEvent& event)
  41. {
  42. if (!layer)
  43. return;
  44. auto& layer_event = event.layer_event();
  45. if (!layer->rect().contains(layer_event.position()))
  46. return;
  47. auto selection_offset = layer->relative_rect().top_left();
  48. m_editor->image().selection().begin_interactive_selection();
  49. set_flood_selection(layer->currently_edited_bitmap(), m_editor->image(), layer_event.position(), selection_offset, m_threshold, m_merge_mode);
  50. m_editor->image().selection().end_interactive_selection();
  51. m_editor->update();
  52. m_editor->did_complete_action(tool_name());
  53. }
  54. ErrorOr<GUI::Widget*> WandSelectTool::get_properties_widget()
  55. {
  56. if (m_properties_widget) {
  57. return m_properties_widget.ptr();
  58. }
  59. auto properties_widget = TRY(GUI::Widget::try_create());
  60. (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>());
  61. auto threshold_container = TRY(properties_widget->try_add<GUI::Widget>());
  62. threshold_container->set_fixed_height(20);
  63. (void)TRY(threshold_container->try_set_layout<GUI::HorizontalBoxLayout>());
  64. auto threshold_label = TRY(threshold_container->try_add<GUI::Label>("Threshold:"));
  65. threshold_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  66. threshold_label->set_fixed_size(80, 20);
  67. auto threshold_slider = TRY(threshold_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, String::from_utf8_short_string("%"sv)));
  68. threshold_slider->set_range(0, 100);
  69. threshold_slider->set_value(m_threshold);
  70. threshold_slider->on_change = [this](int value) {
  71. m_threshold = value;
  72. };
  73. set_primary_slider(threshold_slider);
  74. auto mode_container = TRY(properties_widget->try_add<GUI::Widget>());
  75. mode_container->set_fixed_height(20);
  76. (void)TRY(mode_container->try_set_layout<GUI::HorizontalBoxLayout>());
  77. auto mode_label = TRY(mode_container->try_add<GUI::Label>());
  78. mode_label->set_text("Mode:");
  79. mode_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  80. mode_label->set_fixed_size(80, 20);
  81. for (int i = 0; i < (int)Selection::MergeMode::__Count; i++) {
  82. switch ((Selection::MergeMode)i) {
  83. case Selection::MergeMode::Set:
  84. TRY(m_merge_mode_names.try_append("Set"));
  85. break;
  86. case Selection::MergeMode::Add:
  87. TRY(m_merge_mode_names.try_append("Add"));
  88. break;
  89. case Selection::MergeMode::Subtract:
  90. TRY(m_merge_mode_names.try_append("Subtract"));
  91. break;
  92. case Selection::MergeMode::Intersect:
  93. TRY(m_merge_mode_names.try_append("Intersect"));
  94. break;
  95. default:
  96. VERIFY_NOT_REACHED();
  97. }
  98. }
  99. auto mode_combo = TRY(mode_container->try_add<GUI::ComboBox>());
  100. mode_combo->set_only_allow_values_from_model(true);
  101. mode_combo->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_merge_mode_names));
  102. mode_combo->set_selected_index((int)m_merge_mode);
  103. mode_combo->on_change = [this](auto&&, GUI::ModelIndex const& index) {
  104. VERIFY(index.row() >= 0);
  105. VERIFY(index.row() < (int)Selection::MergeMode::__Count);
  106. m_merge_mode = (Selection::MergeMode)index.row();
  107. };
  108. m_properties_widget = properties_widget;
  109. return m_properties_widget.ptr();
  110. }
  111. }