From 3ca0ef1583244d4851ab2cf396592fb0aa5a31f5 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 17 Apr 2025 23:14:46 +0300 Subject: [PATCH] LibGUI: Add copy_rect API This allows moving parts of window around --- userspace/libraries/LibGUI/Window.cpp | 21 +++++++++++++++++++ .../libraries/LibGUI/include/LibGUI/Window.h | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/userspace/libraries/LibGUI/Window.cpp b/userspace/libraries/LibGUI/Window.cpp index bd9e330a..0af4cf5b 100644 --- a/userspace/libraries/LibGUI/Window.cpp +++ b/userspace/libraries/LibGUI/Window.cpp @@ -211,6 +211,27 @@ namespace LibGUI set_pixel(x, dst_y + fill_y_off + i, fill_color); } + void Window::copy_rect(int32_t dst_x, int32_t dst_y, int32_t src_x, int32_t src_y, uint32_t width, uint32_t height, uint32_t fill_color) + { + fill_rect(dst_x, dst_y, width, height, fill_color); + + if (!clamp_to_framebuffer(dst_x, dst_y, width, height)) + return; + if (!clamp_to_framebuffer(src_x, src_y, width, height)) + return; + + const bool copy_dir = dst_y < src_y; + for (uint32_t i = 0; i < height; i++) + { + const uint32_t y_off = copy_dir ? i : height - i - 1; + memmove( + &m_framebuffer[(dst_y + y_off) * this->width()], + &m_framebuffer[(src_y + y_off) * this->width()], + width * 4 + ); + } + } + bool Window::clamp_to_framebuffer(int32_t& signed_x, int32_t& signed_y, uint32_t& width, uint32_t& height) const { int32_t min_x = BAN::Math::max(signed_x, 0); diff --git a/userspace/libraries/LibGUI/include/LibGUI/Window.h b/userspace/libraries/LibGUI/include/LibGUI/Window.h index 4348c647..e84c683e 100644 --- a/userspace/libraries/LibGUI/include/LibGUI/Window.h +++ b/userspace/libraries/LibGUI/include/LibGUI/Window.h @@ -59,6 +59,10 @@ namespace LibGUI // fill_color is used when copying data outside of window bounds void copy_horizontal_slice(int32_t dst_y, int32_t src_y, uint32_t amount, uint32_t fill_color); + // copy rect (src_x, src_y, width, height) to (dst_x, dst_y, width, height) + // fill_color is used when copying data outside of window bounds + void copy_rect(int32_t dst_x, int32_t dst_y, int32_t src_x, int32_t src_y, uint32_t width, uint32_t height, uint32_t fill_color); + void invalidate(int32_t x, int32_t y, uint32_t width, uint32_t height); void invalidate() { return invalidate(0, 0, width(), height()); }