From e8997e1de9448aecf6e3ef44d9928738a8a54465 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 23 Jan 2021 01:16:06 +0100 Subject: [PATCH] LibGfx: Implement Rect resizing around a fixed point --- Userland/Libraries/LibGfx/Rect.cpp | 10 ++++++++++ Userland/Libraries/LibGfx/Rect.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/Userland/Libraries/LibGfx/Rect.cpp b/Userland/Libraries/LibGfx/Rect.cpp index 1f0d39d440..0a9ce4fc3b 100644 --- a/Userland/Libraries/LibGfx/Rect.cpp +++ b/Userland/Libraries/LibGfx/Rect.cpp @@ -141,6 +141,16 @@ void Rect::align_within(const Rect& other, TextAlignment alignment) } } +template +void Rect::set_size_around(const Size& new_size, const Point& fixed_point) +{ + const T new_x = fixed_point.x() - (T)(new_size.width() * ((float)(fixed_point.x() - x()) / width())); + const T new_y = fixed_point.y() - (T)(new_size.height() * ((float)(fixed_point.y() - y()) / height())); + + set_location({ new_x, new_y }); + set_size(new_size); +} + template<> String IntRect::to_string() const { diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index e9ad4b136e..a6997a4bc6 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -114,6 +114,8 @@ public: m_size = size; } + void set_size_around(const Size&, const Point& fixed_point); + void set_size(T width, T height) { m_size.set_width(width);