mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 08:27:46 +00:00
LibGfx: Templatize Point, Size, and Rect
This commit is contained in:
parent
7a1c328417
commit
335916d8db
33 changed files with 404 additions and 835 deletions
|
@ -26,30 +26,58 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <LibGfx/Orientation.h>
|
||||
#include <LibGfx/Point.h>
|
||||
#include <LibGfx/Size.h>
|
||||
#include <LibGfx/TextAlignment.h>
|
||||
#include <LibIPC/Forward.h>
|
||||
#include <stdlib.h>
|
||||
#include <LibM/math.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
class IntRect {
|
||||
template<typename T>
|
||||
T abst(T value)
|
||||
{
|
||||
return value < 0 ? -value : value;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
class Rect {
|
||||
public:
|
||||
IntRect() {}
|
||||
IntRect(int x, int y, int width, int height)
|
||||
Rect() {}
|
||||
|
||||
Rect(T x, T y, T width, T height)
|
||||
: m_location(x, y)
|
||||
, m_size(width, height)
|
||||
{
|
||||
}
|
||||
IntRect(const IntPoint& location, const IntSize& size)
|
||||
|
||||
template<typename U>
|
||||
Rect(U x, U y, U width, U height)
|
||||
: m_location(x, y)
|
||||
, m_size(width, height)
|
||||
{
|
||||
}
|
||||
|
||||
Rect(const Point<T>& location, const Size<T>& size)
|
||||
: m_location(location)
|
||||
, m_size(size)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Rect(const Point<U>& location, const Size<U>& size)
|
||||
: m_location(location)
|
||||
, m_size(size)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
explicit Rect(const Rect<U>& other)
|
||||
: m_location(other.location())
|
||||
, m_size(other.size())
|
||||
{
|
||||
}
|
||||
|
||||
bool is_null() const
|
||||
{
|
||||
return width() == 0 && height() == 0;
|
||||
|
@ -60,38 +88,38 @@ public:
|
|||
return width() <= 0 || height() <= 0;
|
||||
}
|
||||
|
||||
void move_by(int dx, int dy)
|
||||
void move_by(T dx, T dy)
|
||||
{
|
||||
m_location.move_by(dx, dy);
|
||||
}
|
||||
|
||||
void move_by(const IntPoint& delta)
|
||||
void move_by(const Point<T>& delta)
|
||||
{
|
||||
m_location.move_by(delta);
|
||||
}
|
||||
|
||||
IntPoint center() const
|
||||
Point<T> center() const
|
||||
{
|
||||
return { x() + width() / 2, y() + height() / 2 };
|
||||
}
|
||||
|
||||
void set_location(const IntPoint& location)
|
||||
void set_location(const Point<T>& location)
|
||||
{
|
||||
m_location = location;
|
||||
}
|
||||
|
||||
void set_size(const IntSize& size)
|
||||
void set_size(const Size<T>& size)
|
||||
{
|
||||
m_size = size;
|
||||
}
|
||||
|
||||
void set_size(int width, int height)
|
||||
void set_size(T width, T height)
|
||||
{
|
||||
m_size.set_width(width);
|
||||
m_size.set_height(height);
|
||||
}
|
||||
|
||||
void inflate(int w, int h)
|
||||
void inflate(T w, T h)
|
||||
{
|
||||
set_x(x() - w / 2);
|
||||
set_width(width() + w);
|
||||
|
@ -99,7 +127,7 @@ public:
|
|||
set_height(height() + h);
|
||||
}
|
||||
|
||||
void shrink(int w, int h)
|
||||
void shrink(T w, T h)
|
||||
{
|
||||
set_x(x() + w / 2);
|
||||
set_width(width() - w);
|
||||
|
@ -107,55 +135,55 @@ public:
|
|||
set_height(height() - h);
|
||||
}
|
||||
|
||||
IntRect shrunken(int w, int h) const
|
||||
Rect<T> shrunken(T w, T h) const
|
||||
{
|
||||
IntRect rect = *this;
|
||||
Rect<T> rect = *this;
|
||||
rect.shrink(w, h);
|
||||
return rect;
|
||||
}
|
||||
|
||||
IntRect inflated(int w, int h) const
|
||||
Rect<T> inflated(T w, T h) const
|
||||
{
|
||||
IntRect rect = *this;
|
||||
Rect<T> rect = *this;
|
||||
rect.inflate(w, h);
|
||||
return rect;
|
||||
}
|
||||
|
||||
IntRect translated(int dx, int dy) const
|
||||
Rect<T> translated(T dx, T dy) const
|
||||
{
|
||||
IntRect rect = *this;
|
||||
Rect<T> rect = *this;
|
||||
rect.move_by(dx, dy);
|
||||
return rect;
|
||||
}
|
||||
|
||||
IntRect translated(const IntPoint& delta) const
|
||||
Rect<T> translated(const Point<T>& delta) const
|
||||
{
|
||||
IntRect rect = *this;
|
||||
Rect<T> rect = *this;
|
||||
rect.move_by(delta);
|
||||
return rect;
|
||||
}
|
||||
|
||||
bool contains_vertically(int y) const
|
||||
bool contains_vertically(T y) const
|
||||
{
|
||||
return y >= top() && y <= bottom();
|
||||
}
|
||||
|
||||
bool contains_horizontally(int x) const
|
||||
bool contains_horizontally(T x) const
|
||||
{
|
||||
return x >= left() && x <= right();
|
||||
}
|
||||
|
||||
bool contains(int x, int y) const
|
||||
bool contains(T x, T y) const
|
||||
{
|
||||
return x >= m_location.x() && x <= right() && y >= m_location.y() && y <= bottom();
|
||||
}
|
||||
|
||||
bool contains(const IntPoint& point) const
|
||||
bool contains(const Point<T>& point) const
|
||||
{
|
||||
return contains(point.x(), point.y());
|
||||
}
|
||||
|
||||
bool contains(const IntRect& other) const
|
||||
bool contains(const Rect<T>& other) const
|
||||
{
|
||||
return left() <= other.left()
|
||||
&& right() >= other.right()
|
||||
|
@ -173,70 +201,68 @@ public:
|
|||
void set_primary_size_for_orientation(Orientation orientation, int value) { m_size.set_primary_size_for_orientation(orientation, value); }
|
||||
void set_secondary_size_for_orientation(Orientation orientation, int value) { m_size.set_secondary_size_for_orientation(orientation, value); }
|
||||
|
||||
int first_edge_for_orientation(Orientation orientation) const
|
||||
T first_edge_for_orientation(Orientation orientation) const
|
||||
{
|
||||
if (orientation == Orientation::Vertical)
|
||||
return top();
|
||||
return left();
|
||||
}
|
||||
|
||||
int last_edge_for_orientation(Orientation orientation) const
|
||||
T last_edge_for_orientation(Orientation orientation) const
|
||||
{
|
||||
if (orientation == Orientation::Vertical)
|
||||
return bottom();
|
||||
return right();
|
||||
}
|
||||
|
||||
int left() const { return x(); }
|
||||
int right() const { return x() + width() - 1; }
|
||||
int top() const { return y(); }
|
||||
int bottom() const { return y() + height() - 1; }
|
||||
T left() const { return x(); }
|
||||
T right() const { return x() + width() - 1; }
|
||||
T top() const { return y(); }
|
||||
T bottom() const { return y() + height() - 1; }
|
||||
|
||||
void set_left(int left)
|
||||
void set_left(T left)
|
||||
{
|
||||
set_x(left);
|
||||
}
|
||||
|
||||
void set_top(int top)
|
||||
void set_top(T top)
|
||||
{
|
||||
set_y(top);
|
||||
}
|
||||
|
||||
void set_right(int right)
|
||||
void set_right(T right)
|
||||
{
|
||||
set_width(right - x() + 1);
|
||||
}
|
||||
|
||||
void set_bottom(int bottom)
|
||||
void set_bottom(T bottom)
|
||||
{
|
||||
set_height(bottom - y() + 1);
|
||||
}
|
||||
|
||||
void set_right_without_resize(int new_right)
|
||||
void set_right_without_resize(T new_right)
|
||||
{
|
||||
int delta = new_right - right();
|
||||
move_by(delta, 0);
|
||||
}
|
||||
|
||||
void set_bottom_without_resize(int new_bottom)
|
||||
void set_bottom_without_resize(T new_bottom)
|
||||
{
|
||||
int delta = new_bottom - bottom();
|
||||
move_by(0, delta);
|
||||
}
|
||||
|
||||
bool intersects_vertically(const IntRect& other) const
|
||||
bool intersects_vertically(const Rect<T>& other) const
|
||||
{
|
||||
return top() <= other.bottom()
|
||||
&& other.top() <= bottom();
|
||||
return top() <= other.bottom() && other.top() <= bottom();
|
||||
}
|
||||
|
||||
bool intersects_horizontally(const IntRect& other) const
|
||||
bool intersects_horizontally(const Rect<T>& other) const
|
||||
{
|
||||
return left() <= other.right()
|
||||
&& other.left() <= right();
|
||||
return left() <= other.right() && other.left() <= right();
|
||||
}
|
||||
|
||||
bool intersects(const IntRect& other) const
|
||||
bool intersects(const Rect<T>& other) const
|
||||
{
|
||||
return left() <= other.right()
|
||||
&& other.left() <= right()
|
||||
|
@ -244,100 +270,112 @@ public:
|
|||
&& other.top() <= bottom();
|
||||
}
|
||||
|
||||
int x() const { return location().x(); }
|
||||
int y() const { return location().y(); }
|
||||
int width() const { return m_size.width(); }
|
||||
int height() const { return m_size.height(); }
|
||||
T x() const { return location().x(); }
|
||||
T y() const { return location().y(); }
|
||||
T width() const { return m_size.width(); }
|
||||
T height() const { return m_size.height(); }
|
||||
|
||||
void set_x(int x) { m_location.set_x(x); }
|
||||
void set_y(int y) { m_location.set_y(y); }
|
||||
void set_width(int width) { m_size.set_width(width); }
|
||||
void set_height(int height) { m_size.set_height(height); }
|
||||
void set_x(T x) { m_location.set_x(x); }
|
||||
void set_y(T y) { m_location.set_y(y); }
|
||||
void set_width(T width) { m_size.set_width(width); }
|
||||
void set_height(T height) { m_size.set_height(height); }
|
||||
|
||||
IntPoint location() const { return m_location; }
|
||||
IntSize size() const { return m_size; }
|
||||
const Point<T>& location() const { return m_location; }
|
||||
const Size<T>& size() const { return m_size; }
|
||||
|
||||
Vector<IntRect, 4> shatter(const IntRect& hammer) const;
|
||||
Vector<Rect<T>, 4> shatter(const Rect<T>& hammer) const;
|
||||
|
||||
bool operator==(const IntRect& other) const
|
||||
bool operator==(const Rect<T>& other) const
|
||||
{
|
||||
return m_location == other.m_location
|
||||
&& m_size == other.m_size;
|
||||
return m_location == other.m_location && m_size == other.m_size;
|
||||
}
|
||||
|
||||
bool operator!=(const IntRect& other) const
|
||||
bool operator!=(const Rect<T>& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
void intersect(const IntRect&);
|
||||
void intersect(const Rect<T>&);
|
||||
|
||||
static IntRect from_two_points(const IntPoint& a, const IntPoint& b)
|
||||
static Rect<T> from_two_points(const Point<T>& a, const Point<T>& b)
|
||||
{
|
||||
return { min(a.x(), b.x()), min(a.y(), b.y()), abs(a.x() - b.x()), abs(a.y() - b.y()) };
|
||||
return { min(a.x(), b.x()), min(a.y(), b.y()), abst(a.x() - b.x()), abst(a.y() - b.y()) };
|
||||
}
|
||||
|
||||
static IntRect intersection(const IntRect& a, const IntRect& b)
|
||||
static Rect<T> intersection(const Rect<T>& a, const Rect<T>& b)
|
||||
{
|
||||
IntRect r(a);
|
||||
Rect<T> r = a;
|
||||
r.intersect(b);
|
||||
return r;
|
||||
}
|
||||
|
||||
IntRect intersected(const IntRect& other) const
|
||||
Rect<T> intersected(const Rect<T>& other) const
|
||||
{
|
||||
return intersection(*this, other);
|
||||
}
|
||||
|
||||
IntRect united(const IntRect&) const;
|
||||
Rect<T> united(const Rect<T>&) const;
|
||||
|
||||
IntPoint top_left() const { return { left(), top() }; }
|
||||
IntPoint top_right() const { return { right(), top() }; }
|
||||
IntPoint bottom_left() const { return { left(), bottom() }; }
|
||||
IntPoint bottom_right() const { return { right(), bottom() }; }
|
||||
Point<T> top_left() const { return { left(), top() }; }
|
||||
Point<T> top_right() const { return { right(), top() }; }
|
||||
Point<T> bottom_left() const { return { left(), bottom() }; }
|
||||
Point<T> bottom_right() const { return { right(), bottom() }; }
|
||||
|
||||
void align_within(const IntRect&, TextAlignment);
|
||||
void align_within(const Rect<T>&, TextAlignment);
|
||||
|
||||
void center_within(const IntRect& other)
|
||||
void center_within(const Rect<T>& other)
|
||||
{
|
||||
center_horizontally_within(other);
|
||||
center_vertically_within(other);
|
||||
}
|
||||
|
||||
void center_horizontally_within(const IntRect& other)
|
||||
void center_horizontally_within(const Rect<T>& other)
|
||||
{
|
||||
set_x(other.center().x() - width() / 2);
|
||||
}
|
||||
|
||||
void center_vertically_within(const IntRect& other)
|
||||
void center_vertically_within(const Rect<T>& other)
|
||||
{
|
||||
set_y(other.center().y() - height() / 2);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Rect<U> to() const
|
||||
{
|
||||
return Rect<U>(*this);
|
||||
}
|
||||
|
||||
String to_string() const;
|
||||
|
||||
private:
|
||||
IntPoint m_location;
|
||||
IntSize m_size;
|
||||
Point<T> m_location;
|
||||
Size<T> m_size;
|
||||
};
|
||||
|
||||
inline void IntPoint::constrain(const IntRect& rect)
|
||||
template<typename T>
|
||||
const LogStream& operator<<(const LogStream& stream, const Rect<T>& rect)
|
||||
{
|
||||
if (x() < rect.left())
|
||||
set_x(rect.left());
|
||||
else if (x() > rect.right())
|
||||
set_x(rect.right());
|
||||
if (y() < rect.top())
|
||||
set_y(rect.top());
|
||||
else if (y() > rect.bottom())
|
||||
set_y(rect.bottom());
|
||||
return stream << rect.to_string();
|
||||
}
|
||||
|
||||
const LogStream& operator<<(const LogStream&, const IntRect&);
|
||||
using IntRect = Rect<int>;
|
||||
using FloatRect = Rect<float>;
|
||||
|
||||
ALWAYS_INLINE IntRect enclosing_int_rect(const FloatRect& float_rect)
|
||||
{
|
||||
return {
|
||||
(int)float_rect.x(),
|
||||
(int)float_rect.y(),
|
||||
(int)ceilf(float_rect.width()),
|
||||
(int)ceilf(float_rect.height()),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace IPC {
|
||||
|
||||
bool decode(Decoder&, Gfx::IntRect&);
|
||||
bool encode(Encoder&, const Gfx::IntRect&);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue