CloneTool.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 const&, Gfx::IntPoint const& 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 * 255);
  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 const& color, Gfx::IntPoint const& start, Gfx::IntPoint const& 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>> 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. m_sample_location = image_event.position() - m_cursor_offset.value();
  60. // FIXME: This is a really inefficient way to update the marker's location
  61. m_editor->update();
  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. m_sample_location = image_event.position();
  70. m_cursor_offset = {};
  71. // FIXME: This is a really dumb way to get the marker to show up
  72. m_editor->update();
  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 sample_pos = m_editor->image_position_to_editor_position(m_sample_location.value());
  88. // We don't want the marker to be a single pixel and hide the color.
  89. auto offset = AK::max(2, size() / 2);
  90. Gfx::IntRect rect = {
  91. (int)sample_pos.x() - offset,
  92. (int)sample_pos.y() - offset,
  93. offset * 2,
  94. offset * 2
  95. };
  96. painter.draw_ellipse_intersecting(rect, m_marker_color, 1);
  97. }
  98. void CloneTool::on_keydown(GUI::KeyEvent& event)
  99. {
  100. Tool::on_keydown(event);
  101. if (event.key() == KeyCode::Key_Alt && !m_is_selecting_location) {
  102. m_is_selecting_location = true;
  103. m_editor->update_tool_cursor();
  104. return;
  105. }
  106. }
  107. void CloneTool::on_keyup(GUI::KeyEvent& event)
  108. {
  109. if (m_is_selecting_location && event.key() == KeyCode::Key_Alt) {
  110. m_is_selecting_location = false;
  111. m_editor->update_tool_cursor();
  112. return;
  113. }
  114. }
  115. GUI::Widget* CloneTool::get_properties_widget()
  116. {
  117. if (!m_properties_widget) {
  118. m_properties_widget = GUI::Widget::construct();
  119. m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
  120. auto& size_container = m_properties_widget->add<GUI::Widget>();
  121. size_container.set_fixed_height(20);
  122. size_container.set_layout<GUI::HorizontalBoxLayout>();
  123. auto& size_label = size_container.add<GUI::Label>("Size:");
  124. size_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  125. size_label.set_fixed_size(80, 20);
  126. auto& size_slider = size_container.add<GUI::ValueSlider>(Orientation::Horizontal, "px");
  127. size_slider.set_range(1, 100);
  128. size_slider.set_value(size());
  129. size_slider.on_change = [&](int value) {
  130. set_size(value);
  131. };
  132. set_primary_slider(&size_slider);
  133. auto& hardness_container = m_properties_widget->add<GUI::Widget>();
  134. hardness_container.set_fixed_height(20);
  135. hardness_container.set_layout<GUI::HorizontalBoxLayout>();
  136. auto& hardness_label = hardness_container.add<GUI::Label>("Hardness:");
  137. hardness_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  138. hardness_label.set_fixed_size(80, 20);
  139. auto& hardness_slider = hardness_container.add<GUI::ValueSlider>(Orientation::Horizontal, "%");
  140. hardness_slider.set_range(1, 100);
  141. hardness_slider.on_change = [&](int value) {
  142. set_hardness(value);
  143. };
  144. hardness_slider.set_value(100);
  145. set_secondary_slider(&hardness_slider);
  146. }
  147. return m_properties_widget.ptr();
  148. }
  149. }