summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJelle Raaijmakers <jelle@gmta.nl>2023-05-19 00:22:28 +0200
committerAndreas Kling <kling@serenityos.org>2023-05-19 06:16:14 +0200
commit88a7bb5ba7919ccd76839607891e4331868d2e5a (patch)
treeabb9408af643e5851eef9593df8e201ee033b2aa
parent902ceb167580199dc07d31fa7554171eb0294b82 (diff)
downloadserenity-88a7bb5ba7919ccd76839607891e4331868d2e5a.zip
LibGfx: Implement alternative `Rect` right/bottom edge calculations
For `IntRect`, we assume that the right/bottom edge is offset by minus one. This obviously will not work for `FloatRect`, since those edges are infinitely small. Specialize `right()` and `bottom()` and add a `FIXME` to get rid of the offset in the future.
-rw-r--r--Userland/Libraries/LibGfx/Rect.h50
1 files changed, 37 insertions, 13 deletions
diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h
index 7210ec17e1..b18598fe69 100644
--- a/Userland/Libraries/LibGfx/Rect.h
+++ b/Userland/Libraries/LibGfx/Rect.h
@@ -353,30 +353,54 @@ public:
return right();
}
+ // FIXME: for integral types of T, we assume that the right/bottom edges are offset by minus one.
+ // Although there are cases where this makes sense, for consistency it would be best if we
+ // can drop the `- 1` altogether and not specialize these methods.
[[nodiscard]] ALWAYS_INLINE T left() const { return x(); }
- [[nodiscard]] ALWAYS_INLINE T right() const { return x() + width() - 1; }
+ [[nodiscard]] ALWAYS_INLINE T right() const
+ requires(IsIntegral<T>)
+ {
+ return x() + width() - 1;
+ }
+ [[nodiscard]] ALWAYS_INLINE T right() const
+ requires(!IsIntegral<T>)
+ {
+ return x() + width();
+ }
[[nodiscard]] ALWAYS_INLINE T top() const { return y(); }
- [[nodiscard]] ALWAYS_INLINE T bottom() const { return y() + height() - 1; }
-
- ALWAYS_INLINE void set_left(T left)
+ [[nodiscard]] ALWAYS_INLINE T bottom() const
+ requires(IsIntegral<T>)
{
- set_x(left);
+ return y() + height() - 1;
}
-
- ALWAYS_INLINE void set_top(T top)
+ [[nodiscard]] ALWAYS_INLINE T bottom() const
+ requires(!IsIntegral<T>)
{
- set_y(top);
+ return y() + height();
}
+ ALWAYS_INLINE void set_left(T left) { set_x(left); }
ALWAYS_INLINE void set_right(T right)
+ requires(IsIntegral<T>)
{
set_width(right - x() + 1);
}
-
+ ALWAYS_INLINE void set_right(T right)
+ requires(!IsIntegral<T>)
+ {
+ set_width(right - x());
+ }
+ ALWAYS_INLINE void set_top(T top) { set_y(top); }
ALWAYS_INLINE void set_bottom(T bottom)
+ requires(IsIntegral<T>)
{
set_height(bottom - y() + 1);
}
+ ALWAYS_INLINE void set_bottom(T bottom)
+ requires(!IsIntegral<T>)
+ {
+ set_height(bottom - y());
+ }
void set_right_without_resize(T new_right)
{
@@ -505,10 +529,10 @@ public:
return;
}
- m_location.set_x(l);
- m_location.set_y(t);
- m_size.set_width((r - l) + 1);
- m_size.set_height((b - t) + 1);
+ set_x(l);
+ set_y(t);
+ set_right(r);
+ set_bottom(b);
}
[[nodiscard]] static Rect<T> centered_on(Point<T> const& center, Size<T> const& size)