CloneTool.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CloneTool.h"
  7. #include "../ImageEditor.h"
  8. #include "../Layer.h"
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/Label.h>
  11. #include <LibGUI/Menu.h>
  12. #include <LibGUI/Painter.h>
  13. #include <LibGUI/ValueSlider.h>
  14. #include <LibGfx/Bitmap.h>
  15. namespace PixelPaint {
  16. void CloneTool::draw_point(Gfx::Bitmap& bitmap, Gfx::Color, Gfx::IntPoint point)
  17. {
  18. if (!m_sample_location.has_value())
  19. return;
  20. auto source_point = point - m_cursor_offset.value();
  21. for (int y = -size(); y < size(); y++) {
  22. for (int x = -size(); x < size(); x++) {
  23. auto target_x = point.x() + x;
  24. auto target_y = point.y() + y;
  25. auto distance = point.distance_from({ target_x, target_y });
  26. if (target_x < 0 || target_x >= bitmap.width() || target_y < 0 || target_y >= bitmap.height())
  27. continue;
  28. if (distance >= size())
  29. continue;
  30. auto source_x = source_point.x() + x;
  31. auto source_y = source_point.y() + y;
  32. if (source_x < 0 || source_x >= bitmap.width() || source_y < 0 || source_y >= bitmap.height())
  33. continue;
  34. auto falloff = get_falloff(distance);
  35. auto pixel_color = bitmap.get_pixel(source_x, source_y);
  36. pixel_color.set_alpha(falloff * pixel_color.alpha());
  37. bitmap.set_pixel(target_x, target_y, bitmap.get_pixel(target_x, target_y).blend(pixel_color));
  38. }
  39. }
  40. }
  41. void CloneTool::draw_line(Gfx::Bitmap& bitmap, Gfx::Color color, Gfx::IntPoint start, Gfx::IntPoint end)
  42. {
  43. if (!m_sample_location.has_value())
  44. return;
  45. BrushTool::draw_line(bitmap, color, start, end);
  46. }
  47. Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> CloneTool::cursor()
  48. {
  49. if (m_is_selecting_location)
  50. return Gfx::StandardCursor::Eyedropper;
  51. return Gfx::StandardCursor::Crosshair;
  52. }
  53. void CloneTool::on_mousemove(Layer* layer, MouseEvent& event)
  54. {
  55. auto& image_event = event.image_event();
  56. if (image_event.alt())
  57. return;
  58. if (m_cursor_offset.has_value()) {
  59. auto old_sample_marker_rect = sample_marker_rect();
  60. m_sample_location = image_event.position() - m_cursor_offset.value();
  61. update_sample_marker(old_sample_marker_rect);
  62. }
  63. BrushTool::on_mousemove(layer, event);
  64. }
  65. void CloneTool::on_mousedown(Layer* layer, MouseEvent& event)
  66. {
  67. auto& image_event = event.image_event();
  68. if (image_event.alt()) {
  69. auto old_sample_marker_rect = sample_marker_rect();
  70. m_sample_location = image_event.position();
  71. m_cursor_offset = {};
  72. update_sample_marker(old_sample_marker_rect);
  73. return;
  74. }
  75. if (!m_sample_location.has_value())
  76. return;
  77. if (!m_cursor_offset.has_value())
  78. m_cursor_offset = event.image_event().position() - m_sample_location.value();
  79. BrushTool::on_mousedown(layer, event);
  80. }
  81. void CloneTool::on_second_paint(Layer const*, GUI::PaintEvent& event)
  82. {
  83. if (!m_sample_location.has_value())
  84. return;
  85. GUI::Painter painter(*m_editor);
  86. painter.add_clip_rect(event.rect());
  87. auto rect = sample_marker_rect();
  88. painter.draw_ellipse_intersecting(rect.value(), m_marker_color, 1);
  89. }
  90. bool CloneTool::on_keydown(GUI::KeyEvent& event)
  91. {
  92. if (event.key() == KeyCode::Key_Alt && !m_is_selecting_location) {
  93. m_is_selecting_location = true;
  94. m_editor->update_tool_cursor();
  95. return true;
  96. }
  97. return Tool::on_keydown(event);
  98. }
  99. void CloneTool::on_keyup(GUI::KeyEvent& event)
  100. {
  101. if (m_is_selecting_location && event.key() == KeyCode::Key_Alt) {
  102. m_is_selecting_location = false;
  103. m_editor->update_tool_cursor();
  104. return;
  105. }
  106. }
  107. ErrorOr<GUI::Widget*> CloneTool::get_properties_widget()
  108. {
  109. if (!m_properties_widget) {
  110. auto properties_widget = TRY(GUI::Widget::try_create());
  111. (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>());
  112. auto size_container = TRY(properties_widget->try_add<GUI::Widget>());
  113. size_container->set_fixed_height(20);
  114. (void)TRY(size_container->try_set_layout<GUI::HorizontalBoxLayout>());
  115. auto size_label = TRY(size_container->try_add<GUI::Label>("Size:"_short_string));
  116. size_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  117. size_label->set_fixed_size(80, 20);
  118. auto size_slider = TRY(size_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "px"_short_string));
  119. size_slider->set_range(1, 100);
  120. size_slider->set_value(size());
  121. size_slider->on_change = [this](int value) {
  122. auto old_sample_marker_rect = sample_marker_rect();
  123. set_size(value);
  124. update_sample_marker(old_sample_marker_rect);
  125. };
  126. set_primary_slider(size_slider);
  127. auto hardness_container = TRY(properties_widget->try_add<GUI::Widget>());
  128. hardness_container->set_fixed_height(20);
  129. (void)TRY(hardness_container->try_set_layout<GUI::HorizontalBoxLayout>());
  130. auto hardness_label = TRY(hardness_container->try_add<GUI::Label>(TRY("Hardness:"_string)));
  131. hardness_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  132. hardness_label->set_fixed_size(80, 20);
  133. auto hardness_slider = TRY(hardness_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "%"_short_string));
  134. hardness_slider->set_range(1, 100);
  135. hardness_slider->on_change = [&](int value) {
  136. set_hardness(value);
  137. };
  138. hardness_slider->set_value(100);
  139. set_secondary_slider(hardness_slider);
  140. m_properties_widget = properties_widget;
  141. }
  142. return m_properties_widget.ptr();
  143. }
  144. Optional<Gfx::IntRect> CloneTool::sample_marker_rect()
  145. {
  146. if (!m_sample_location.has_value())
  147. return {};
  148. auto offset = AK::max(2, size());
  149. Gfx::IntRect content_rect = {
  150. m_sample_location.value().x() - offset,
  151. m_sample_location.value().y() - offset,
  152. offset * 2,
  153. offset * 2
  154. };
  155. return m_editor->content_to_frame_rect(content_rect).to_type<int>();
  156. }
  157. void CloneTool::update_sample_marker(Optional<Gfx::IntRect> old_rect)
  158. {
  159. if (old_rect.has_value())
  160. m_editor->update(old_rect.value().inflated(2, 2));
  161. auto current_rect = sample_marker_rect();
  162. if (current_rect.has_value())
  163. m_editor->update(current_rect.value().inflated(2, 2));
  164. }
  165. }