Serenity Operating System
at master 162 lines 6.8 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org> 4 * Copyright (c) 2022, the SerenityOS developers. 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 9#include "EraseTool.h" 10#include "../ImageEditor.h" 11#include "../Layer.h" 12#include <LibGUI/Action.h> 13#include <LibGUI/BoxLayout.h> 14#include <LibGUI/CheckBox.h> 15#include <LibGUI/Label.h> 16#include <LibGUI/Menu.h> 17#include <LibGUI/Painter.h> 18#include <LibGUI/RadioButton.h> 19#include <LibGUI/ValueSlider.h> 20#include <LibGfx/Bitmap.h> 21 22namespace PixelPaint { 23 24Color EraseTool::color_for(GUI::MouseEvent const&) 25{ 26 if (m_use_secondary_color) 27 return m_editor->secondary_color(); 28 return Color(255, 255, 255, 0); 29} 30 31void EraseTool::draw_point(Gfx::Bitmap& bitmap, Gfx::Color color, Gfx::IntPoint point) 32{ 33 if (m_draw_mode == DrawMode::Pencil) { 34 int radius = size() / 2; 35 Gfx::IntRect rect { point.x() - radius, point.y() - radius, size(), size() }; 36 GUI::Painter painter(bitmap); 37 painter.clear_rect(rect, color); 38 } else { 39 for (int y = point.y() - size(); y < point.y() + size(); y++) { 40 for (int x = point.x() - size(); x < point.x() + size(); x++) { 41 auto distance = point.distance_from({ x, y }); 42 if (x < 0 || x >= bitmap.width() || y < 0 || y >= bitmap.height()) 43 continue; 44 if (distance >= size()) 45 continue; 46 47 auto old_color = bitmap.get_pixel(x, y); 48 auto falloff = get_falloff(distance); 49 auto new_color = old_color.interpolate(color, falloff); 50 bitmap.set_pixel(x, y, new_color); 51 } 52 } 53 } 54} 55 56ErrorOr<GUI::Widget*> EraseTool::get_properties_widget() 57{ 58 if (!m_properties_widget) { 59 auto properties_widget = TRY(GUI::Widget::try_create()); 60 (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>()); 61 62 auto size_container = TRY(properties_widget->try_add<GUI::Widget>()); 63 size_container->set_fixed_height(20); 64 (void)TRY(size_container->try_set_layout<GUI::HorizontalBoxLayout>()); 65 66 auto size_label = TRY(size_container->try_add<GUI::Label>("Size:")); 67 size_label->set_text_alignment(Gfx::TextAlignment::CenterLeft); 68 size_label->set_fixed_size(80, 20); 69 70 auto size_slider = TRY(size_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "px"_short_string)); 71 size_slider->set_range(1, 100); 72 size_slider->set_value(size()); 73 74 size_slider->on_change = [this, size_slider](int value) { 75 set_size(value); 76 size_slider->set_override_cursor(cursor()); 77 }; 78 set_primary_slider(size_slider); 79 80 auto hardness_container = TRY(properties_widget->try_add<GUI::Widget>()); 81 hardness_container->set_fixed_height(20); 82 (void)TRY(hardness_container->try_set_layout<GUI::HorizontalBoxLayout>()); 83 84 auto hardness_label = TRY(hardness_container->try_add<GUI::Label>("Hardness:")); 85 hardness_label->set_text_alignment(Gfx::TextAlignment::CenterLeft); 86 hardness_label->set_fixed_size(80, 20); 87 88 auto hardness_slider = TRY(hardness_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "%"_short_string)); 89 hardness_slider->set_range(1, 100); 90 hardness_slider->set_value(hardness()); 91 92 hardness_slider->on_change = [this](int value) { 93 set_hardness(value); 94 }; 95 set_secondary_slider(hardness_slider); 96 97 auto secondary_color_container = TRY(properties_widget->try_add<GUI::Widget>()); 98 secondary_color_container->set_fixed_height(20); 99 (void)TRY(secondary_color_container->try_set_layout<GUI::HorizontalBoxLayout>()); 100 101 auto use_secondary_color_checkbox = TRY(secondary_color_container->try_add<GUI::CheckBox>()); 102 use_secondary_color_checkbox->set_checked(m_use_secondary_color); 103 use_secondary_color_checkbox->set_text(TRY("Use secondary color"_string)); 104 use_secondary_color_checkbox->on_checked = [this](bool checked) { 105 m_use_secondary_color = checked; 106 }; 107 108 auto mode_container = TRY(properties_widget->try_add<GUI::Widget>()); 109 mode_container->set_fixed_height(46); 110 (void)TRY(mode_container->try_set_layout<GUI::HorizontalBoxLayout>()); 111 auto mode_label = TRY(mode_container->try_add<GUI::Label>("Draw Mode:")); 112 mode_label->set_text_alignment(Gfx::TextAlignment::CenterLeft); 113 mode_label->set_fixed_size(80, 20); 114 115 auto mode_radio_container = TRY(mode_container->try_add<GUI::Widget>()); 116 (void)TRY(mode_radio_container->try_set_layout<GUI::VerticalBoxLayout>()); 117 auto pencil_mode_radio = TRY(mode_radio_container->try_add<GUI::RadioButton>("Pencil"_short_string)); 118 auto brush_mode_radio = TRY(mode_radio_container->try_add<GUI::RadioButton>("Brush"_short_string)); 119 120 pencil_mode_radio->on_checked = [this, hardness_slider, size_slider](bool) { 121 m_draw_mode = DrawMode::Pencil; 122 hardness_slider->set_enabled(false); 123 refresh_editor_cursor(); 124 size_slider->set_override_cursor(cursor()); 125 }; 126 brush_mode_radio->on_checked = [this, hardness_slider, size_slider](bool) { 127 m_draw_mode = DrawMode::Brush; 128 hardness_slider->set_enabled(true); 129 refresh_editor_cursor(); 130 size_slider->set_override_cursor(cursor()); 131 }; 132 133 pencil_mode_radio->set_checked(true); 134 m_properties_widget = properties_widget; 135 } 136 137 return m_properties_widget.ptr(); 138} 139 140NonnullRefPtr<Gfx::Bitmap> EraseTool::build_cursor() 141{ 142 if (m_draw_mode == DrawMode::Brush) 143 return BrushTool::build_cursor(); 144 145 m_scale_last_created_cursor = m_editor ? m_editor->scale() : 1; 146 int scaled_size = size() * m_scale_last_created_cursor; 147 148 NonnullRefPtr<Gfx::Bitmap> new_cursor = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::IntSize(scaled_size, scaled_size)).release_value_but_fixme_should_propagate_errors(); 149 150 Gfx::IntRect rect { 0, 0, scaled_size, scaled_size }; 151 Gfx::Painter painter { new_cursor }; 152 153 painter.draw_rect(rect, Color::LightGray); 154 painter.draw_line({ scaled_size / 2 - 5, scaled_size / 2 }, { scaled_size / 2 + 5, scaled_size / 2 }, Color::LightGray, 3); 155 painter.draw_line({ scaled_size / 2, scaled_size / 2 - 5 }, { scaled_size / 2, scaled_size / 2 + 5 }, Color::LightGray, 3); 156 painter.draw_line({ scaled_size / 2 - 5, scaled_size / 2 }, { scaled_size / 2 + 5, scaled_size / 2 }, Color::MidGray, 1); 157 painter.draw_line({ scaled_size / 2, scaled_size / 2 - 5 }, { scaled_size / 2, scaled_size / 2 + 5 }, Color::MidGray, 1); 158 159 return new_cursor; 160} 161 162}