2018-10-10 14:49:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-10 18:06:58 +00:00
|
|
|
#include "Point.h"
|
2019-01-09 01:06:04 +00:00
|
|
|
#include "Size.h"
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2019-01-14 13:21:51 +00:00
|
|
|
struct GUI_Rect;
|
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
class Rect {
|
|
|
|
public:
|
|
|
|
Rect() { }
|
|
|
|
Rect(int x, int y, int width, int height)
|
2018-10-10 18:06:58 +00:00
|
|
|
: m_location(x, y)
|
2019-01-09 01:06:04 +00:00
|
|
|
, m_size(width, height)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Rect(const Point& location, const Size& size)
|
|
|
|
: m_location(location)
|
|
|
|
, m_size(size)
|
2018-10-10 14:49:36 +00:00
|
|
|
{
|
|
|
|
}
|
2019-01-20 22:42:36 +00:00
|
|
|
Rect(const Rect& other)
|
|
|
|
: m_location(other.m_location)
|
|
|
|
, m_size(other.m_size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-14 13:21:51 +00:00
|
|
|
Rect(const GUI_Rect&);
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2019-01-18 03:37:49 +00:00
|
|
|
bool is_null() const
|
|
|
|
{
|
|
|
|
return width() == 0 && height() == 0;
|
|
|
|
}
|
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
bool is_empty() const
|
2018-10-12 00:24:05 +00:00
|
|
|
{
|
2019-01-18 03:37:49 +00:00
|
|
|
return width() <= 0 || height() <= 0;
|
2018-10-12 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 16:54:06 +00:00
|
|
|
void move_by(int dx, int dy)
|
2018-10-10 14:49:36 +00:00
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
m_location.move_by(dx, dy);
|
2018-10-10 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 16:54:06 +00:00
|
|
|
void move_by(const Point& delta)
|
2018-10-12 00:41:27 +00:00
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
m_location.move_by(delta);
|
2018-10-12 00:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 23:48:09 +00:00
|
|
|
Point center() const
|
|
|
|
{
|
|
|
|
return { x() + width() / 2, y() + height() / 2 };
|
|
|
|
}
|
|
|
|
|
2018-10-11 14:52:40 +00:00
|
|
|
void inflate(int w, int h)
|
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
set_x(x() - w / 2);
|
|
|
|
set_width(width() + w);
|
|
|
|
set_y(y() - h / 2);
|
|
|
|
set_height(height() + h);
|
2018-10-11 14:52:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 20:46:34 +00:00
|
|
|
void shrink(int w, int h)
|
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
set_x(x() + w / 2);
|
|
|
|
set_width(width() - w);
|
|
|
|
set_y(y() + h / 2);
|
|
|
|
set_height(height() - h);
|
2018-10-13 20:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
bool contains(int x, int y) const
|
|
|
|
{
|
2019-01-12 16:17:51 +00:00
|
|
|
return x >= m_location.x() && x <= right() && y >= m_location.y() && y <= bottom();
|
2018-10-10 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 18:06:58 +00:00
|
|
|
bool contains(const Point& point) const
|
|
|
|
{
|
|
|
|
return contains(point.x(), point.y());
|
|
|
|
}
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2019-01-12 02:07:23 +00:00
|
|
|
bool contains(const Rect& other) const
|
|
|
|
{
|
|
|
|
return left() <= other.left()
|
|
|
|
&& right() >= other.right()
|
|
|
|
&& top() <= other.top()
|
|
|
|
&& bottom() >= other.bottom();
|
|
|
|
}
|
|
|
|
|
2018-10-10 18:06:58 +00:00
|
|
|
int left() const { return x(); }
|
2019-01-12 00:00:24 +00:00
|
|
|
int right() const { return x() + width() - 1; }
|
2018-10-10 18:06:58 +00:00
|
|
|
int top() const { return y(); }
|
2019-01-12 00:00:24 +00:00
|
|
|
int bottom() const { return y() + height() - 1; }
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2019-01-12 05:39:34 +00:00
|
|
|
void set_left(int left)
|
2018-10-12 12:15:14 +00:00
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
set_x(left);
|
2018-10-12 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 05:39:34 +00:00
|
|
|
void set_top(int top)
|
2018-10-12 12:15:14 +00:00
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
set_y(top);
|
2018-10-12 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 05:39:34 +00:00
|
|
|
void set_right(int right)
|
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
set_width(right - x() + 1);
|
2019-01-12 05:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_bottom(int bottom)
|
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
set_height(bottom - y() + 1);
|
2019-01-12 05:39:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:39:48 +00:00
|
|
|
bool intersects(const Rect& other) const
|
|
|
|
{
|
2019-01-12 00:00:24 +00:00
|
|
|
return left() <= other.right()
|
|
|
|
&& other.left() <= right()
|
|
|
|
&& top() <= other.bottom()
|
|
|
|
&& other.top() <= bottom();
|
2018-10-12 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 18:06:58 +00:00
|
|
|
int x() const { return location().x(); }
|
|
|
|
int y() const { return location().y(); }
|
2019-01-09 01:06:04 +00:00
|
|
|
int width() const { return m_size.width(); }
|
|
|
|
int height() const { return m_size.height(); }
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2019-01-16 16:54:06 +00:00
|
|
|
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); }
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2018-10-10 18:06:58 +00:00
|
|
|
Point location() const { return m_location; }
|
2019-01-09 01:06:04 +00:00
|
|
|
Size size() const { return m_size; }
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2019-01-14 13:21:51 +00:00
|
|
|
operator GUI_Rect() const;
|
|
|
|
|
2018-10-11 23:03:22 +00:00
|
|
|
bool operator==(const Rect& other) const
|
|
|
|
{
|
|
|
|
return m_location == other.m_location
|
2019-01-09 01:06:04 +00:00
|
|
|
&& m_size == other.m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void intersect(const Rect&);
|
|
|
|
|
|
|
|
static Rect intersection(const Rect& a, const Rect& b)
|
|
|
|
{
|
|
|
|
Rect r(a);
|
|
|
|
r.intersect(b);
|
2019-01-12 05:39:34 +00:00
|
|
|
return r;
|
2018-10-11 23:03:22 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 23:04:23 +00:00
|
|
|
Rect united(const Rect&) const;
|
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
private:
|
2018-10-10 18:06:58 +00:00
|
|
|
Point m_location;
|
2019-01-09 01:06:04 +00:00
|
|
|
Size m_size;
|
2018-10-10 14:49:36 +00:00
|
|
|
};
|
2019-01-12 00:00:24 +00:00
|
|
|
|
|
|
|
inline void Point::constrain(const Rect& rect)
|
|
|
|
{
|
|
|
|
if (x() < rect.left())
|
2019-01-16 16:54:06 +00:00
|
|
|
set_x(rect.left());
|
2019-01-12 00:00:24 +00:00
|
|
|
else if (x() > rect.right())
|
2019-01-16 16:54:06 +00:00
|
|
|
set_x(rect.right());
|
2019-01-12 00:00:24 +00:00
|
|
|
if (y() < rect.top())
|
2019-01-16 16:54:06 +00:00
|
|
|
set_y(rect.top());
|
2019-01-12 00:00:24 +00:00
|
|
|
else if (y() > rect.bottom())
|
2019-01-16 16:54:06 +00:00
|
|
|
set_y(rect.bottom());
|
2019-01-12 00:00:24 +00:00
|
|
|
}
|