EraseTool.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include "EraseTool.h"
  9. #include "../ImageEditor.h"
  10. #include "../Layer.h"
  11. #include <LibGUI/Action.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/CheckBox.h>
  14. #include <LibGUI/Label.h>
  15. #include <LibGUI/Menu.h>
  16. #include <LibGUI/Painter.h>
  17. #include <LibGUI/RadioButton.h>
  18. #include <LibGUI/ValueSlider.h>
  19. #include <LibGfx/Bitmap.h>
  20. namespace PixelPaint {
  21. Color EraseTool::color_for(GUI::MouseEvent const&)
  22. {
  23. if (m_use_secondary_color)
  24. return m_editor->secondary_color();
  25. return Color(255, 255, 255, 0);
  26. }
  27. void EraseTool::draw_point(Gfx::Bitmap& bitmap, Gfx::Color color, Gfx::IntPoint point)
  28. {
  29. if (m_draw_mode == DrawMode::Pencil) {
  30. int radius = size() / 2;
  31. Gfx::IntRect rect { point.x() - radius, point.y() - radius, size(), size() };
  32. GUI::Painter painter(bitmap);
  33. painter.clear_rect(rect, color);
  34. } else {
  35. for (int y = point.y() - size(); y < point.y() + size(); y++) {
  36. for (int x = point.x() - size(); x < point.x() + size(); x++) {
  37. auto distance = point.distance_from({ x, y });
  38. if (x < 0 || x >= bitmap.width() || y < 0 || y >= bitmap.height())
  39. continue;
  40. if (distance >= size())
  41. continue;
  42. auto old_color = bitmap.get_pixel(x, y);
  43. auto falloff = get_falloff(distance);
  44. auto new_color = old_color.interpolate(color, falloff);
  45. bitmap.set_pixel(x, y, new_color);
  46. }
  47. }
  48. }
  49. }
  50. ErrorOr<GUI::Widget*> EraseTool::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 size_container = TRY(properties_widget->try_add<GUI::Widget>());
  56. size_container->set_fixed_height(20);
  57. (void)TRY(size_container->try_set_layout<GUI::HorizontalBoxLayout>());
  58. auto size_label = TRY(size_container->try_add<GUI::Label>("Size:"_short_string));
  59. size_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  60. size_label->set_fixed_size(80, 20);
  61. auto size_slider = TRY(size_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "px"_short_string));
  62. size_slider->set_range(1, 100);
  63. size_slider->set_value(size());
  64. size_slider->on_change = [this, size_slider](int value) {
  65. set_size(value);
  66. size_slider->set_override_cursor(cursor());
  67. };
  68. set_primary_slider(size_slider);
  69. auto hardness_container = TRY(properties_widget->try_add<GUI::Widget>());
  70. hardness_container->set_fixed_height(20);
  71. (void)TRY(hardness_container->try_set_layout<GUI::HorizontalBoxLayout>());
  72. auto hardness_label = TRY(hardness_container->try_add<GUI::Label>(TRY("Hardness:"_string)));
  73. hardness_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  74. hardness_label->set_fixed_size(80, 20);
  75. auto hardness_slider = TRY(hardness_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "%"_short_string));
  76. hardness_slider->set_range(1, 100);
  77. hardness_slider->set_value(hardness());
  78. hardness_slider->on_change = [this](int value) {
  79. set_hardness(value);
  80. };
  81. set_secondary_slider(hardness_slider);
  82. auto secondary_color_container = TRY(properties_widget->try_add<GUI::Widget>());
  83. secondary_color_container->set_fixed_height(20);
  84. (void)TRY(secondary_color_container->try_set_layout<GUI::HorizontalBoxLayout>());
  85. auto use_secondary_color_checkbox = TRY(secondary_color_container->try_add<GUI::CheckBox>());
  86. use_secondary_color_checkbox->set_checked(m_use_secondary_color);
  87. use_secondary_color_checkbox->set_text(TRY("Use secondary color"_string));
  88. use_secondary_color_checkbox->on_checked = [this](bool checked) {
  89. m_use_secondary_color = checked;
  90. };
  91. auto mode_container = TRY(properties_widget->try_add<GUI::Widget>());
  92. mode_container->set_fixed_height(46);
  93. (void)TRY(mode_container->try_set_layout<GUI::HorizontalBoxLayout>());
  94. auto mode_label = TRY(mode_container->try_add<GUI::Label>(TRY("Draw Mode:"_string)));
  95. mode_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  96. mode_label->set_fixed_size(80, 20);
  97. auto mode_radio_container = TRY(mode_container->try_add<GUI::Widget>());
  98. (void)TRY(mode_radio_container->try_set_layout<GUI::VerticalBoxLayout>());
  99. auto pencil_mode_radio = TRY(mode_radio_container->try_add<GUI::RadioButton>("Pencil"_short_string));
  100. auto brush_mode_radio = TRY(mode_radio_container->try_add<GUI::RadioButton>("Brush"_short_string));
  101. pencil_mode_radio->on_checked = [this, hardness_slider, size_slider](bool) {
  102. m_draw_mode = DrawMode::Pencil;
  103. hardness_slider->set_enabled(false);
  104. refresh_editor_cursor();
  105. size_slider->set_override_cursor(cursor());
  106. };
  107. brush_mode_radio->on_checked = [this, hardness_slider, size_slider](bool) {
  108. m_draw_mode = DrawMode::Brush;
  109. hardness_slider->set_enabled(true);
  110. refresh_editor_cursor();
  111. size_slider->set_override_cursor(cursor());
  112. };
  113. pencil_mode_radio->set_checked(true);
  114. m_properties_widget = properties_widget;
  115. }
  116. return m_properties_widget.ptr();
  117. }
  118. NonnullRefPtr<Gfx::Bitmap> EraseTool::build_cursor()
  119. {
  120. if (m_draw_mode == DrawMode::Brush)
  121. return BrushTool::build_cursor();
  122. m_scale_last_created_cursor = m_editor ? m_editor->scale() : 1;
  123. int scaled_size = size() * m_scale_last_created_cursor;
  124. NonnullRefPtr<Gfx::Bitmap> new_cursor = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::IntSize(scaled_size, scaled_size)).release_value_but_fixme_should_propagate_errors();
  125. Gfx::IntRect rect { 0, 0, scaled_size, scaled_size };
  126. Gfx::Painter painter { new_cursor };
  127. painter.draw_rect(rect, Color::LightGray);
  128. painter.draw_line({ scaled_size / 2 - 5, scaled_size / 2 }, { scaled_size / 2 + 5, scaled_size / 2 }, Color::LightGray, 3);
  129. painter.draw_line({ scaled_size / 2, scaled_size / 2 - 5 }, { scaled_size / 2, scaled_size / 2 + 5 }, Color::LightGray, 3);
  130. painter.draw_line({ scaled_size / 2 - 5, scaled_size / 2 }, { scaled_size / 2 + 5, scaled_size / 2 }, Color::MidGray, 1);
  131. painter.draw_line({ scaled_size / 2, scaled_size / 2 - 5 }, { scaled_size / 2, scaled_size / 2 + 5 }, Color::MidGray, 1);
  132. return new_cursor;
  133. }
  134. }