BucketTool.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Aaron Yoder <aaronjyoder@gmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "BucketTool.h"
  10. #include "../ImageEditor.h"
  11. #include "../Layer.h"
  12. #include "../Mask.h"
  13. #include <AK/HashTable.h>
  14. #include <AK/Queue.h>
  15. #include <LibGUI/BoxLayout.h>
  16. #include <LibGUI/Label.h>
  17. #include <LibGUI/Painter.h>
  18. #include <LibGUI/ValueSlider.h>
  19. #include <LibGfx/Bitmap.h>
  20. #include <LibGfx/Rect.h>
  21. namespace PixelPaint {
  22. BucketTool::BucketTool()
  23. {
  24. m_cursor = Gfx::Bitmap::load_from_file("/res/icons/pixelpaint/bucket.png"sv).release_value_but_fixme_should_propagate_errors();
  25. }
  26. static void flood_fill(Gfx::Bitmap& bitmap, Gfx::IntPoint start_position, Color fill_color, int threshold)
  27. {
  28. VERIFY(bitmap.bpp() == 32);
  29. if (!bitmap.rect().contains(start_position))
  30. return;
  31. auto pixel_visited = [&](Gfx::IntPoint location) {
  32. bitmap.set_pixel<Gfx::StorageFormat::BGRA8888>(location.x(), location.y(), fill_color);
  33. };
  34. bitmap.flood_visit_from_point(start_position, threshold, move(pixel_visited));
  35. }
  36. void BucketTool::on_mousedown(Layer* layer, MouseEvent& event)
  37. {
  38. if (!layer)
  39. return;
  40. auto& layer_event = event.layer_event();
  41. if (!layer->rect().contains(layer_event.position()))
  42. return;
  43. if (auto selection = layer->image().selection(); !selection.is_empty() && !selection.is_selected(event.image_event().position()))
  44. return;
  45. GUI::Painter painter(layer->get_scratch_edited_bitmap());
  46. flood_fill(layer->get_scratch_edited_bitmap(), layer_event.position(), m_editor->color_for(layer_event), m_threshold);
  47. layer->did_modify_bitmap(layer->get_scratch_edited_bitmap().rect());
  48. m_editor->did_complete_action(tool_name());
  49. }
  50. ErrorOr<GUI::Widget*> BucketTool::get_properties_widget()
  51. {
  52. if (!m_properties_widget) {
  53. auto properties_widget = TRY(GUI::Widget::try_create());
  54. (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>());
  55. auto threshold_container = TRY(properties_widget->try_add<GUI::Widget>());
  56. threshold_container->set_fixed_height(20);
  57. (void)TRY(threshold_container->try_set_layout<GUI::HorizontalBoxLayout>());
  58. auto threshold_label = TRY(threshold_container->try_add<GUI::Label>("Threshold:"));
  59. threshold_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  60. threshold_label->set_fixed_size(80, 20);
  61. auto threshold_slider = TRY(threshold_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, String::from_utf8_short_string("%"sv)));
  62. threshold_slider->set_range(0, 100);
  63. threshold_slider->set_value(m_threshold);
  64. threshold_slider->on_change = [this](int value) {
  65. m_threshold = value;
  66. };
  67. set_primary_slider(threshold_slider);
  68. m_properties_widget = properties_widget;
  69. }
  70. return m_properties_widget.ptr();
  71. }
  72. }