Serenity Operating System
at master 92 lines 3.1 kB view raw
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 10#include "BucketTool.h" 11#include "../ImageEditor.h" 12#include "../Layer.h" 13#include "../Mask.h" 14#include <AK/HashTable.h> 15#include <AK/Queue.h> 16#include <LibGUI/BoxLayout.h> 17#include <LibGUI/Label.h> 18#include <LibGUI/Painter.h> 19#include <LibGUI/ValueSlider.h> 20#include <LibGfx/Bitmap.h> 21#include <LibGfx/Rect.h> 22 23namespace PixelPaint { 24 25BucketTool::BucketTool() 26{ 27 m_cursor = NonnullRefPtr<Gfx::Bitmap const> { Gfx::Bitmap::load_from_file("/res/icons/pixelpaint/bucket.png"sv).release_value_but_fixme_should_propagate_errors() }; 28} 29 30static void flood_fill(Gfx::Bitmap& bitmap, Gfx::IntPoint start_position, Color fill_color, int threshold) 31{ 32 VERIFY(bitmap.bpp() == 32); 33 34 if (!bitmap.rect().contains(start_position)) 35 return; 36 37 auto pixel_visited = [&](Gfx::IntPoint location) { 38 bitmap.set_pixel<Gfx::StorageFormat::BGRA8888>(location.x(), location.y(), fill_color); 39 }; 40 41 bitmap.flood_visit_from_point(start_position, threshold, move(pixel_visited)); 42} 43 44void BucketTool::on_mousedown(Layer* layer, MouseEvent& event) 45{ 46 if (!layer) 47 return; 48 49 auto& layer_event = event.layer_event(); 50 if (!layer->rect().contains(layer_event.position())) 51 return; 52 53 if (auto selection = layer->image().selection(); !selection.is_empty() && !selection.is_selected(event.image_event().position())) 54 return; 55 56 GUI::Painter painter(layer->get_scratch_edited_bitmap()); 57 58 flood_fill(layer->get_scratch_edited_bitmap(), layer_event.position(), m_editor->color_for(layer_event), m_threshold); 59 60 layer->did_modify_bitmap(layer->get_scratch_edited_bitmap().rect()); 61 m_editor->did_complete_action(tool_name()); 62} 63 64ErrorOr<GUI::Widget*> BucketTool::get_properties_widget() 65{ 66 if (!m_properties_widget) { 67 auto properties_widget = TRY(GUI::Widget::try_create()); 68 (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>()); 69 70 auto threshold_container = TRY(properties_widget->try_add<GUI::Widget>()); 71 threshold_container->set_fixed_height(20); 72 (void)TRY(threshold_container->try_set_layout<GUI::HorizontalBoxLayout>()); 73 74 auto threshold_label = TRY(threshold_container->try_add<GUI::Label>("Threshold:")); 75 threshold_label->set_text_alignment(Gfx::TextAlignment::CenterLeft); 76 threshold_label->set_fixed_size(80, 20); 77 78 auto threshold_slider = TRY(threshold_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "%"_short_string)); 79 threshold_slider->set_range(0, 100); 80 threshold_slider->set_value(m_threshold); 81 82 threshold_slider->on_change = [this](int value) { 83 m_threshold = value; 84 }; 85 set_primary_slider(threshold_slider); 86 m_properties_widget = properties_widget; 87 } 88 89 return m_properties_widget.ptr(); 90} 91 92}