mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-03 21:10:30 +00:00
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.
This commit is contained in:
parent
c82825379a
commit
9115e99e4b
Notes:
sideshowbarker
2024-07-17 08:42:05 +09:00
Author: https://github.com/tcl3 Commit: https://github.com/SerenityOS/serenity/commit/9115e99e4b Pull-request: https://github.com/SerenityOS/serenity/pull/17211 Reviewed-by: https://github.com/MacDue ✅
2 changed files with 34 additions and 15 deletions
|
@ -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<Gfx::IntRect, 4> 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<Gfx::IntRect, 4> 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<void> MoveTool::update_cached_preview_bitmap(Layer const* layer)
|
|||
|
||||
Optional<ResizeAnchorLocation const> 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<int>();
|
||||
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<int>();
|
||||
|
||||
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)))
|
||||
|
|
|
@ -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<Gfx::IntRect, 4> 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<Gfx::IntRect, 4> 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<void> update_cached_preview_bitmap(Layer const* layer);
|
||||
Optional<ResizeAnchorLocation const> resize_anchor_location_from_cursor_position(Layer const*, MouseEvent&);
|
||||
|
|
Loading…
Reference in a new issue