From f8903acea246e186bfa730342019fbe7c863d882 Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 14 Aug 2020 14:10:39 -0600 Subject: [PATCH] LibGfx: Add convenience helpers for Rect --- Libraries/LibGfx/Rect.h | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/Libraries/LibGfx/Rect.h b/Libraries/LibGfx/Rect.h index 6105491f82..50a6bad661 100644 --- a/Libraries/LibGfx/Rect.h +++ b/Libraries/LibGfx/Rect.h @@ -43,7 +43,7 @@ T abst(T value) template class Rect { public: - Rect() {} + Rect() { } Rect(T x, T y, T width, T height) : m_location(x, y) @@ -191,6 +191,18 @@ public: && bottom() >= other.bottom(); } + template + bool contains(const Container& others) const + { + bool have_any = false; + for (const auto& other : others) { + if (!contains(other)) + return false; + have_any = true; + } + return have_any; + } + int primary_offset_for_orientation(Orientation orientation) const { return m_location.primary_offset_for_orientation(orientation); } void set_primary_offset_for_orientation(Orientation orientation, int value) { m_location.set_primary_offset_for_orientation(orientation, value); } int secondary_offset_for_orientation(Orientation orientation) const { return m_location.secondary_offset_for_orientation(orientation); } @@ -270,6 +282,32 @@ public: && other.top() <= bottom(); } + template + bool intersects(const Container& others) const + { + for (const auto& other : others) { + if (intersects(other)) + return true; + } + return false; + } + + template + IterationDecision for_each_intersected(const Container& others, Function f) const + { + if (is_empty()) + return IterationDecision::Continue; + for (const auto& other : others) { + auto intersected_rect = intersected(other); + if (!intersected_rect.is_empty()) { + IterationDecision decision = f(intersected_rect); + if (decision != IterationDecision::Continue) + return decision; + } + } + return IterationDecision::Continue; + } + T x() const { return location().x(); } T y() const { return location().y(); } T width() const { return m_size.width(); }