MoveTool.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022-2023, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "MoveTool.h"
  8. #include "../Image.h"
  9. #include "../ImageEditor.h"
  10. #include "../Layer.h"
  11. #include <AK/String.h>
  12. #include <LibGUI/Action.h>
  13. #include <LibGUI/BoxLayout.h>
  14. #include <LibGUI/Label.h>
  15. #include <LibGUI/Menu.h>
  16. #include <LibGUI/MessageBox.h>
  17. #include <LibGUI/Painter.h>
  18. #include <LibGUI/Window.h>
  19. #include <LibGfx/Filters/ContrastFilter.h>
  20. namespace PixelPaint {
  21. constexpr int resize_anchor_min_size = 5;
  22. constexpr int resize_anchor_max_size = 20;
  23. void MoveTool::on_mousedown(Layer* layer, MouseEvent& event)
  24. {
  25. if (event.image_event().button() == GUI::MouseButton::Secondary) {
  26. m_editor->start_panning(event.raw_event().position());
  27. return;
  28. }
  29. if (!layer)
  30. return;
  31. auto& layer_event = event.layer_event();
  32. auto& image_event = event.image_event();
  33. if (layer_event.button() != GUI::MouseButton::Primary)
  34. return;
  35. if (!layer->rect().contains(layer_event.position()) && !m_resize_anchor_location.has_value())
  36. return;
  37. m_scaling = m_resize_anchor_location.has_value();
  38. m_layer_being_moved = *layer;
  39. m_event_origin = image_event.position();
  40. m_layer_origin = layer->location();
  41. m_new_layer_rect = m_editor->active_layer()->relative_rect();
  42. }
  43. void MoveTool::on_mousemove(Layer* layer, MouseEvent& event)
  44. {
  45. if (m_editor->is_panning()) {
  46. m_editor->pan_to(event.raw_event().position());
  47. return;
  48. }
  49. if (!layer)
  50. return;
  51. if (!m_scaling) {
  52. auto current_resize_anchor_location = resize_anchor_location_from_cursor_position(layer, event);
  53. if (m_resize_anchor_location != current_resize_anchor_location) {
  54. m_resize_anchor_location = current_resize_anchor_location;
  55. m_editor->update_tool_cursor();
  56. }
  57. }
  58. if (!m_layer_being_moved)
  59. return;
  60. auto cursor_position = event.image_event().position();
  61. auto delta = cursor_position - m_event_origin;
  62. auto rect_being_moved = m_layer_being_moved->relative_rect();
  63. Gfx::IntPoint scaling_origin;
  64. Gfx::IntPoint opposite_corner;
  65. if (m_scaling) {
  66. VERIFY(m_resize_anchor_location.has_value());
  67. switch (m_resize_anchor_location.value()) {
  68. case ResizeAnchorLocation::TopLeft:
  69. scaling_origin = rect_being_moved.top_left();
  70. opposite_corner = rect_being_moved.bottom_right().translated(1, 1);
  71. break;
  72. case ResizeAnchorLocation::BottomRight:
  73. scaling_origin = rect_being_moved.bottom_right().translated(1, 1);
  74. opposite_corner = rect_being_moved.top_left();
  75. break;
  76. case ResizeAnchorLocation::BottomLeft:
  77. scaling_origin = rect_being_moved.bottom_left().translated(0, 1);
  78. opposite_corner = rect_being_moved.top_right().translated(1, 0);
  79. break;
  80. case ResizeAnchorLocation::TopRight:
  81. scaling_origin = rect_being_moved.top_right().translated(1, 0);
  82. opposite_corner = rect_being_moved.bottom_left().translated(0, 1);
  83. break;
  84. }
  85. scaling_origin.translate_by(delta);
  86. if (m_keep_aspect_ratio) {
  87. auto aspect_ratio = m_layer_being_moved->size().aspect_ratio();
  88. scaling_origin = opposite_corner.end_point_for_aspect_ratio(scaling_origin, aspect_ratio);
  89. }
  90. auto scaled_rect = Gfx::IntRect::from_two_points(scaling_origin, opposite_corner);
  91. if (!scaled_rect.is_empty())
  92. m_new_layer_rect = scaled_rect;
  93. } else {
  94. m_layer_being_moved->set_location(m_layer_origin.translated(delta));
  95. }
  96. m_editor->update();
  97. }
  98. void MoveTool::on_mouseup(Layer* layer, MouseEvent& event)
  99. {
  100. if (event.image_event().button() == GUI::MouseButton::Secondary) {
  101. m_editor->stop_panning();
  102. m_editor->set_override_cursor(cursor());
  103. return;
  104. }
  105. if (!layer)
  106. return;
  107. auto& layer_event = event.layer_event();
  108. if (layer_event.button() != GUI::MouseButton::Primary)
  109. return;
  110. if (m_scaling) {
  111. auto resized_or_error = m_editor->active_layer()->resize(m_new_layer_rect, Gfx::Painter::ScalingMode::BilinearBlend);
  112. if (resized_or_error.is_error())
  113. GUI::MessageBox::show_error(m_editor->window(), MUST(String::formatted("Failed to resize layer: {}", resized_or_error.error().string_literal())));
  114. else
  115. m_editor->layers_did_change();
  116. }
  117. m_scaling = false;
  118. m_layer_being_moved = nullptr;
  119. m_cached_preview_bitmap = nullptr;
  120. m_editor->update_tool_cursor();
  121. m_editor->did_complete_action(tool_name());
  122. }
  123. bool MoveTool::on_keydown(GUI::KeyEvent& event)
  124. {
  125. if (event.key() == Key_Shift)
  126. m_keep_aspect_ratio = true;
  127. if (event.key() == Key_Alt)
  128. toggle_selection_mode();
  129. if (m_scaling)
  130. return true;
  131. if (!(event.modifiers() == Mod_None || event.modifiers() == Mod_Shift))
  132. return false;
  133. auto* layer = m_editor->active_layer();
  134. if (!layer)
  135. return false;
  136. auto new_location = layer->location();
  137. auto speed = event.shift() ? 10 : 1;
  138. switch (event.key()) {
  139. case Key_Up:
  140. new_location.translate_by(0, -speed);
  141. break;
  142. case Key_Down:
  143. new_location.translate_by(0, speed);
  144. break;
  145. case Key_Left:
  146. new_location.translate_by(-speed, 0);
  147. break;
  148. case Key_Right:
  149. new_location.translate_by(speed, 0);
  150. break;
  151. default:
  152. return false;
  153. }
  154. layer->set_location(new_location);
  155. m_editor->layers_did_change();
  156. return true;
  157. }
  158. void MoveTool::on_keyup(GUI::KeyEvent& event)
  159. {
  160. if (event.key() == Key_Shift)
  161. m_keep_aspect_ratio = false;
  162. if (event.key() == Key_Alt)
  163. toggle_selection_mode();
  164. }
  165. void MoveTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
  166. {
  167. VERIFY(m_editor);
  168. GUI::Painter painter(*m_editor);
  169. painter.add_clip_rect(event.rect());
  170. auto content_rect = m_scaling ? m_new_layer_rect : m_editor->active_layer()->relative_rect();
  171. auto rect_in_editor = m_editor->content_to_frame_rect(content_rect).to_type<int>();
  172. if (m_scaling && (!m_cached_preview_bitmap.is_null() || !update_cached_preview_bitmap(layer).is_error())) {
  173. Gfx::PainterStateSaver saver(painter);
  174. painter.add_clip_rect(m_editor->content_rect());
  175. painter.draw_scaled_bitmap(rect_in_editor, *m_cached_preview_bitmap, m_cached_preview_bitmap->rect(), 1.0f, Gfx::Painter::ScalingMode::BilinearBlend);
  176. }
  177. painter.draw_rect_with_thickness(rect_in_editor, Color::Black, 3);
  178. painter.draw_rect_with_thickness(rect_in_editor, Color::White, 1);
  179. auto size = resize_anchor_size(rect_in_editor);
  180. if (size < resize_anchor_min_size)
  181. return;
  182. auto resize_anchors = resize_anchor_rects(rect_in_editor, size);
  183. for (auto const& resize_anchor_rect : resize_anchors) {
  184. painter.draw_rect_with_thickness(resize_anchor_rect, Color::Black, 3);
  185. painter.draw_rect_with_thickness(resize_anchor_rect, Color::White, 1);
  186. }
  187. }
  188. Gfx::IntRect MoveTool::resize_anchor_rect_from_position(Gfx::IntPoint position, int size)
  189. {
  190. auto resize_anchor_rect_top_left = position.translated(-size / 2);
  191. return Gfx::IntRect(resize_anchor_rect_top_left, Gfx::IntSize(size, size));
  192. }
  193. int MoveTool::resize_anchor_size(Gfx::IntRect layer_rect_in_frame_coordinates)
  194. {
  195. auto shortest_side = min(layer_rect_in_frame_coordinates.width(), layer_rect_in_frame_coordinates.height());
  196. if (shortest_side <= 1)
  197. return 1;
  198. int x = ceilf(shortest_side / 3.0f);
  199. return min(resize_anchor_max_size, x);
  200. }
  201. Array<Gfx::IntRect, 4> MoveTool::resize_anchor_rects(Gfx::IntRect layer_rect_in_frame_coordinates, int resize_anchor_size)
  202. {
  203. return Array {
  204. resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.top_left(), resize_anchor_size),
  205. resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.top_right().translated(1, 0), resize_anchor_size),
  206. resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_left().translated(0, 1), resize_anchor_size),
  207. resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_right().translated(1), resize_anchor_size)
  208. };
  209. }
  210. ErrorOr<void> MoveTool::update_cached_preview_bitmap(Layer const* layer)
  211. {
  212. auto editor_rect_size = m_editor->frame_inner_rect().size();
  213. auto const& source_bitmap = layer->content_bitmap();
  214. auto preview_bitmap_size = editor_rect_size.contains(source_bitmap.size()) ? source_bitmap.size() : editor_rect_size;
  215. m_cached_preview_bitmap = TRY(Gfx::Bitmap::create(source_bitmap.format(), preview_bitmap_size));
  216. GUI::Painter preview_painter(*m_cached_preview_bitmap);
  217. preview_painter.draw_scaled_bitmap(m_cached_preview_bitmap->rect(), source_bitmap, source_bitmap.rect(), 0.8f, Gfx::Painter::ScalingMode::BilinearBlend);
  218. Gfx::ContrastFilter preview_filter(0.5f);
  219. preview_filter.apply(*m_cached_preview_bitmap, m_cached_preview_bitmap->rect(), *m_cached_preview_bitmap, m_cached_preview_bitmap->rect());
  220. return {};
  221. }
  222. Optional<ResizeAnchorLocation const> MoveTool::resize_anchor_location_from_cursor_position(Layer const* layer, MouseEvent& event)
  223. {
  224. auto layer_rect = m_editor->content_to_frame_rect(layer->relative_rect()).to_type<int>();
  225. auto size = max(resize_anchor_min_size, resize_anchor_size(layer_rect));
  226. auto cursor_within_resize_anchor_rect = [&event, size](Gfx::IntPoint layer_position_in_frame_coordinates) {
  227. auto resize_anchor_rect = resize_anchor_rect_from_position(layer_position_in_frame_coordinates, size);
  228. return resize_anchor_rect.contains(event.raw_event().position());
  229. };
  230. if (cursor_within_resize_anchor_rect(layer_rect.top_left()))
  231. return ResizeAnchorLocation::TopLeft;
  232. if (cursor_within_resize_anchor_rect(layer_rect.top_right().translated(1, 0)))
  233. return ResizeAnchorLocation::TopRight;
  234. if (cursor_within_resize_anchor_rect(layer_rect.bottom_left().translated(0, 1)))
  235. return ResizeAnchorLocation::BottomLeft;
  236. if (cursor_within_resize_anchor_rect(layer_rect.bottom_right().translated(1)))
  237. return ResizeAnchorLocation::BottomRight;
  238. return {};
  239. }
  240. Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> MoveTool::cursor()
  241. {
  242. if (m_resize_anchor_location.has_value()) {
  243. switch (m_resize_anchor_location.value()) {
  244. case ResizeAnchorLocation::TopLeft:
  245. case ResizeAnchorLocation::BottomRight:
  246. return Gfx::StandardCursor::ResizeDiagonalTLBR;
  247. case ResizeAnchorLocation::BottomLeft:
  248. case ResizeAnchorLocation::TopRight:
  249. return Gfx::StandardCursor::ResizeDiagonalBLTR;
  250. }
  251. }
  252. return Gfx::StandardCursor::Move;
  253. }
  254. ErrorOr<GUI::Widget*> MoveTool::get_properties_widget()
  255. {
  256. if (!m_properties_widget) {
  257. auto properties_widget = TRY(GUI::Widget::try_create());
  258. (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>());
  259. auto selection_mode_container = TRY(properties_widget->try_add<GUI::Widget>());
  260. (void)TRY(selection_mode_container->try_set_layout<GUI::HorizontalBoxLayout>());
  261. selection_mode_container->set_fixed_height(46);
  262. auto selection_mode_label = TRY(selection_mode_container->try_add<GUI::Label>("Selection Mode:"));
  263. selection_mode_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  264. selection_mode_label->set_fixed_size(80, 40);
  265. auto mode_radio_container = TRY(selection_mode_container->try_add<GUI::Widget>());
  266. (void)TRY(mode_radio_container->try_set_layout<GUI::VerticalBoxLayout>());
  267. m_selection_mode_foreground = TRY(mode_radio_container->try_add<GUI::RadioButton>(TRY(String::from_utf8("Foreground"sv))));
  268. m_selection_mode_active = TRY(mode_radio_container->try_add<GUI::RadioButton>(TRY(String::from_utf8("Active Layer"sv))));
  269. m_selection_mode_foreground->on_checked = [this](bool) {
  270. m_layer_selection_mode = LayerSelectionMode::ForegroundLayer;
  271. };
  272. m_selection_mode_active->on_checked = [this](bool) {
  273. m_layer_selection_mode = LayerSelectionMode::ActiveLayer;
  274. };
  275. m_selection_mode_foreground->set_checked(true);
  276. m_properties_widget = properties_widget;
  277. }
  278. return m_properties_widget.ptr();
  279. }
  280. void MoveTool::toggle_selection_mode()
  281. {
  282. if (m_selection_mode_foreground->is_checked())
  283. m_selection_mode_active->set_checked(true);
  284. else
  285. m_selection_mode_foreground->set_checked(true);
  286. }
  287. }