From 9115e99e4b6e7f7f3624f4a12160d9859c30bbd3 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Fri, 27 Jan 2023 23:00:00 +0000 Subject: [PATCH] PixelPaint: Scale move tool resize anchors The areas where the user must click to resize the image are now scaled to ensure they do not overlap. This allows us to display the correct cursor when zoomed out, as well as making the borders look nicer. --- .../PixelPaint/Tools/MoveTool.cpp | 44 +++++++++++++------ .../Applications/PixelPaint/Tools/MoveTool.h | 5 ++- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Userland/Applications/PixelPaint/Tools/MoveTool.cpp b/Userland/Applications/PixelPaint/Tools/MoveTool.cpp index 0816afccb5..16f8aa9e06 100644 --- a/Userland/Applications/PixelPaint/Tools/MoveTool.cpp +++ b/Userland/Applications/PixelPaint/Tools/MoveTool.cpp @@ -21,6 +21,9 @@ namespace PixelPaint { +constexpr int resize_anchor_min_size = 5; +constexpr int resize_anchor_max_size = 20; + void MoveTool::on_mousedown(Layer* layer, MouseEvent& event) { if (event.image_event().button() == GUI::MouseButton::Secondary) { @@ -200,27 +203,39 @@ void MoveTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event) } painter.draw_rect_with_thickness(rect_in_editor, Color::Black, 3); painter.draw_rect_with_thickness(rect_in_editor, Color::White, 1); - auto resize_anchors = resize_anchor_rects(rect_in_editor); + auto size = resize_anchor_size(rect_in_editor); + if (size < resize_anchor_min_size) + return; + + auto resize_anchors = resize_anchor_rects(rect_in_editor, size); for (auto const& resize_anchor_rect : resize_anchors) { painter.draw_rect_with_thickness(resize_anchor_rect, Color::Black, 3); painter.draw_rect_with_thickness(resize_anchor_rect, Color::White, 1); } } -Gfx::IntRect MoveTool::resize_anchor_rect_from_position(Gfx::IntPoint position) +Gfx::IntRect MoveTool::resize_anchor_rect_from_position(Gfx::IntPoint position, int size) { - constexpr int resize_anchor_size = 20; - auto resize_anchor_rect_top_left = position.translated(-resize_anchor_size / 2); - return Gfx::IntRect(resize_anchor_rect_top_left, Gfx::IntSize(resize_anchor_size, resize_anchor_size)); + auto resize_anchor_rect_top_left = position.translated(-size / 2); + return Gfx::IntRect(resize_anchor_rect_top_left, Gfx::IntSize(size, size)); } -Array MoveTool::resize_anchor_rects(Gfx::IntRect layer_rect_in_frame_coordinates) +int MoveTool::resize_anchor_size(Gfx::IntRect layer_rect_in_frame_coordinates) +{ + auto shortest_side = min(layer_rect_in_frame_coordinates.width(), layer_rect_in_frame_coordinates.height()); + if (shortest_side <= 1) + return 1; + int x = ceilf(shortest_side / 3.0f); + return min(resize_anchor_max_size, x); +} + +Array MoveTool::resize_anchor_rects(Gfx::IntRect layer_rect_in_frame_coordinates, int resize_anchor_size) { return Array { - resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.top_left()), - resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.top_right().translated(1, 0)), - resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_left().translated(0, 1)), - resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_right().translated(1)) + resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.top_left(), resize_anchor_size), + resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.top_right().translated(1, 0), resize_anchor_size), + resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_left().translated(0, 1), resize_anchor_size), + resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_right().translated(1), resize_anchor_size) }; } @@ -240,11 +255,14 @@ ErrorOr MoveTool::update_cached_preview_bitmap(Layer const* layer) Optional MoveTool::resize_anchor_location_from_cursor_position(Layer const* layer, MouseEvent& event) { - auto cursor_within_resize_anchor_rect = [&](Gfx::IntPoint layer_position_in_frame_coordinates) { - auto resize_anchor_rect = resize_anchor_rect_from_position(layer_position_in_frame_coordinates); + auto layer_rect = m_editor->content_to_frame_rect(layer->relative_rect()).to_rounded(); + auto size = max(resize_anchor_min_size, resize_anchor_size(layer_rect)); + + auto cursor_within_resize_anchor_rect = [&event, size](Gfx::IntPoint layer_position_in_frame_coordinates) { + auto resize_anchor_rect = resize_anchor_rect_from_position(layer_position_in_frame_coordinates, size); return resize_anchor_rect.contains(event.raw_event().position()); }; - auto layer_rect = m_editor->content_to_frame_rect(layer->relative_rect()).to_rounded(); + if (cursor_within_resize_anchor_rect(layer_rect.top_left())) return ResizeAnchorLocation::TopLeft; if (cursor_within_resize_anchor_rect(layer_rect.top_right().translated(1, 0))) diff --git a/Userland/Applications/PixelPaint/Tools/MoveTool.h b/Userland/Applications/PixelPaint/Tools/MoveTool.h index 71640a84e4..4a02025022 100644 --- a/Userland/Applications/PixelPaint/Tools/MoveTool.h +++ b/Userland/Applications/PixelPaint/Tools/MoveTool.h @@ -41,8 +41,9 @@ public: LayerSelectionMode layer_selection_mode() const { return m_layer_selection_mode; } private: - static Gfx::IntRect resize_anchor_rect_from_position(Gfx::IntPoint); - static Array resize_anchor_rects(Gfx::IntRect layer_rect_in_frame_coordinates); + static int resize_anchor_size(Gfx::IntRect layer_rect_in_frame_coordinates); + static Gfx::IntRect resize_anchor_rect_from_position(Gfx::IntPoint, int resize_anchor_size); + static Array resize_anchor_rects(Gfx::IntRect layer_rect_in_frame_coordinates, int resize_anchor_size); virtual StringView tool_name() const override { return "Move Tool"sv; } ErrorOr update_cached_preview_bitmap(Layer const* layer); Optional resize_anchor_location_from_cursor_position(Layer const*, MouseEvent&);