/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2022, Aaron Yoder * Copyright (c) 2022, the SerenityOS developers. * Copyright (c) 2022, Timothy Slater . * * SPDX-License-Identifier: BSD-2-Clause */ #include "BucketTool.h" #include "../ImageEditor.h" #include "../Layer.h" #include "../Mask.h" #include #include #include #include #include #include #include #include namespace PixelPaint { BucketTool::BucketTool() { m_cursor = NonnullRefPtr { Gfx::Bitmap::load_from_file("/res/icons/pixelpaint/bucket.png"sv).release_value_but_fixme_should_propagate_errors() }; } static void flood_fill(Gfx::Bitmap& bitmap, Gfx::IntPoint start_position, Color fill_color, int threshold) { VERIFY(bitmap.bpp() == 32); if (!bitmap.rect().contains(start_position)) return; auto pixel_visited = [&](Gfx::IntPoint location) { bitmap.set_pixel(location.x(), location.y(), fill_color); }; bitmap.flood_visit_from_point(start_position, threshold, move(pixel_visited)); } void BucketTool::on_mousedown(Layer* layer, MouseEvent& event) { if (!layer) return; auto& layer_event = event.layer_event(); if (!layer->rect().contains(layer_event.position())) return; if (auto selection = layer->image().selection(); !selection.is_empty() && !selection.is_selected(event.image_event().position())) return; GUI::Painter painter(layer->get_scratch_edited_bitmap()); flood_fill(layer->get_scratch_edited_bitmap(), layer_event.position(), m_editor->color_for(layer_event), m_threshold); layer->did_modify_bitmap(layer->get_scratch_edited_bitmap().rect()); m_editor->did_complete_action(tool_name()); } ErrorOr BucketTool::get_properties_widget() { if (!m_properties_widget) { auto properties_widget = TRY(GUI::Widget::try_create()); (void)TRY(properties_widget->try_set_layout()); auto threshold_container = TRY(properties_widget->try_add()); threshold_container->set_fixed_height(20); (void)TRY(threshold_container->try_set_layout()); auto threshold_label = TRY(threshold_container->try_add("Threshold:")); threshold_label->set_text_alignment(Gfx::TextAlignment::CenterLeft); threshold_label->set_fixed_size(80, 20); auto threshold_slider = TRY(threshold_container->try_add(Orientation::Horizontal, "%"_short_string)); threshold_slider->set_range(0, 100); threshold_slider->set_value(m_threshold); threshold_slider->on_change = [this](int value) { m_threshold = value; }; set_primary_slider(threshold_slider); m_properties_widget = properties_widget; } return m_properties_widget.ptr(); } }