2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-10-09 13:44:52 +00:00
|
|
|
#include <AK/Format.h>
|
2020-02-06 11:04:00 +00:00
|
|
|
#include <LibGfx/Orientation.h>
|
|
|
|
#include <LibGfx/Point.h>
|
|
|
|
#include <LibGfx/Size.h>
|
|
|
|
#include <LibGfx/TextAlignment.h>
|
2020-08-12 08:44:45 +00:00
|
|
|
#include <math.h>
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
namespace Gfx {
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<typename T>
|
|
|
|
T abst(T value)
|
|
|
|
{
|
|
|
|
return value < 0 ? -value : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class Rect {
|
2018-10-10 14:49:36 +00:00
|
|
|
public:
|
2020-08-14 20:10:39 +00:00
|
|
|
Rect() { }
|
2020-07-26 04:31:47 +00:00
|
|
|
|
|
|
|
Rect(T x, T y, T width, T height)
|
|
|
|
: m_location(x, y)
|
|
|
|
, m_size(width, height)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
Rect(U x, U y, U width, U 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)
|
|
|
|
{
|
|
|
|
}
|
2020-07-26 04:31:47 +00:00
|
|
|
|
|
|
|
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)
|
2019-01-09 01:06:04 +00:00
|
|
|
: m_location(location)
|
|
|
|
, m_size(size)
|
2018-10-10 14:49:36 +00:00
|
|
|
{
|
|
|
|
}
|
2019-01-20 22:42:36 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<typename U>
|
|
|
|
explicit Rect(const Rect<U>& other)
|
|
|
|
: m_location(other.location())
|
|
|
|
, m_size(other.size())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void move_by(T dx, T 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
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void move_by(const Point<T>& 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
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Point<T> center() const
|
2018-10-10 23:48:09 +00:00
|
|
|
{
|
|
|
|
return { x() + width() / 2, y() + height() / 2 };
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_location(const Point<T>& location)
|
2019-02-10 10:07:13 +00:00
|
|
|
{
|
|
|
|
m_location = location;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_size(const Size<T>& size)
|
2019-02-10 10:07:13 +00:00
|
|
|
{
|
|
|
|
m_size = size;
|
|
|
|
}
|
|
|
|
|
2021-01-23 00:16:06 +00:00
|
|
|
void set_size_around(const Size<T>&, const Point<T>& fixed_point);
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_size(T width, T height)
|
2019-03-09 20:09:29 +00:00
|
|
|
{
|
|
|
|
m_size.set_width(width);
|
|
|
|
m_size.set_height(height);
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void inflate(T w, T h)
|
2018-10-11 14:52:40 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2020-10-04 17:35:22 +00:00
|
|
|
void inflate(const Size<T>& size)
|
|
|
|
{
|
|
|
|
set_x(x() - size.width() / 2);
|
|
|
|
set_width(width() + size.width());
|
|
|
|
set_y(y() - size.height() / 2);
|
|
|
|
set_height(height() + size.height());
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void shrink(T w, T h)
|
2018-10-13 20:46:34 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2020-10-04 17:35:22 +00:00
|
|
|
void shrink(const Size<T>& size)
|
|
|
|
{
|
|
|
|
set_x(x() + size.width() / 2);
|
|
|
|
set_width(width() - size.width());
|
|
|
|
set_y(y() + size.height() / 2);
|
|
|
|
set_height(height() - size.height());
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> shrunken(T w, T h) const
|
2019-02-05 10:42:35 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> rect = *this;
|
2019-02-05 10:42:35 +00:00
|
|
|
rect.shrink(w, h);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2020-10-04 17:35:22 +00:00
|
|
|
Rect<T> shrunken(const Size<T>& size) const
|
|
|
|
{
|
|
|
|
Rect<T> rect = *this;
|
|
|
|
rect.shrink(size);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> inflated(T w, T h) const
|
2019-02-05 10:42:35 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> rect = *this;
|
2019-02-05 10:42:35 +00:00
|
|
|
rect.inflate(w, h);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2020-10-04 17:35:22 +00:00
|
|
|
Rect<T> inflated(const Size<T>& size) const
|
|
|
|
{
|
|
|
|
Rect<T> rect = *this;
|
|
|
|
rect.inflate(size);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> translated(T dx, T dy) const
|
2019-02-05 10:42:35 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> rect = *this;
|
2019-02-05 10:42:35 +00:00
|
|
|
rect.move_by(dx, dy);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> translated(const Point<T>& delta) const
|
2019-02-10 13:28:39 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> rect = *this;
|
2019-02-10 13:28:39 +00:00
|
|
|
rect.move_by(delta);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
bool contains_vertically(T y) const
|
2019-09-01 15:28:49 +00:00
|
|
|
{
|
|
|
|
return y >= top() && y <= bottom();
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
bool contains_horizontally(T x) const
|
2019-09-01 15:28:49 +00:00
|
|
|
{
|
|
|
|
return x >= left() && x <= right();
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
bool contains(T x, T y) const
|
2018-10-10 14:49:36 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
bool contains(const Point<T>& point) const
|
2018-10-10 18:06:58 +00:00
|
|
|
{
|
|
|
|
return contains(point.x(), point.y());
|
|
|
|
}
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
bool contains(const Rect<T>& other) const
|
2019-01-12 02:07:23 +00:00
|
|
|
{
|
|
|
|
return left() <= other.left()
|
|
|
|
&& right() >= other.right()
|
|
|
|
&& top() <= other.top()
|
|
|
|
&& bottom() >= other.bottom();
|
|
|
|
}
|
|
|
|
|
2020-08-14 20:10:39 +00:00
|
|
|
template<typename Container>
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-20 14:46:33 +00:00
|
|
|
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); }
|
|
|
|
void set_secondary_offset_for_orientation(Orientation orientation, int value) { m_location.set_secondary_offset_for_orientation(orientation, value); }
|
|
|
|
|
2019-07-20 20:16:40 +00:00
|
|
|
int primary_size_for_orientation(Orientation orientation) const { return m_size.primary_size_for_orientation(orientation); }
|
|
|
|
int secondary_size_for_orientation(Orientation orientation) const { return m_size.secondary_size_for_orientation(orientation); }
|
|
|
|
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); }
|
2019-07-20 14:46:33 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
T first_edge_for_orientation(Orientation orientation) const
|
2019-07-20 14:46:33 +00:00
|
|
|
{
|
|
|
|
if (orientation == Orientation::Vertical)
|
|
|
|
return top();
|
|
|
|
return left();
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
T last_edge_for_orientation(Orientation orientation) const
|
2019-07-20 14:46:33 +00:00
|
|
|
{
|
|
|
|
if (orientation == Orientation::Vertical)
|
|
|
|
return bottom();
|
|
|
|
return right();
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
T left() const { return x(); }
|
|
|
|
T right() const { return x() + width() - 1; }
|
|
|
|
T top() const { return y(); }
|
|
|
|
T bottom() const { return y() + height() - 1; }
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_left(T 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
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_top(T 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
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_right(T right)
|
2019-01-12 05:39:34 +00:00
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
set_width(right - x() + 1);
|
2019-01-12 05:39:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_bottom(T bottom)
|
2019-01-12 05:39:34 +00:00
|
|
|
{
|
2019-01-16 16:54:06 +00:00
|
|
|
set_height(bottom - y() + 1);
|
2019-01-12 05:39:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_right_without_resize(T new_right)
|
2019-09-16 18:56:23 +00:00
|
|
|
{
|
|
|
|
int delta = new_right - right();
|
|
|
|
move_by(delta, 0);
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_bottom_without_resize(T new_bottom)
|
2019-09-16 18:56:23 +00:00
|
|
|
{
|
|
|
|
int delta = new_bottom - bottom();
|
|
|
|
move_by(0, delta);
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
bool intersects_vertically(const Rect<T>& other) const
|
2019-12-13 22:35:44 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
return top() <= other.bottom() && other.top() <= bottom();
|
2019-12-13 22:35:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
bool intersects_horizontally(const Rect<T>& other) const
|
2019-12-13 22:35:44 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
return left() <= other.right() && other.left() <= right();
|
2019-12-13 22:35:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
bool intersects(const Rect<T>& other) const
|
2018-10-12 17:39:48 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2020-08-14 20:10:39 +00:00
|
|
|
template<typename Container>
|
|
|
|
bool intersects(const Container& others) const
|
|
|
|
{
|
|
|
|
for (const auto& other : others) {
|
|
|
|
if (intersects(other))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container, typename Function>
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
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(); }
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
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); }
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
const Point<T>& location() const { return m_location; }
|
|
|
|
const Size<T>& size() const { return m_size; }
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Vector<Rect<T>, 4> shatter(const Rect<T>& hammer) const;
|
2019-02-19 13:49:23 +00:00
|
|
|
|
2021-01-22 20:12:37 +00:00
|
|
|
template<class U>
|
|
|
|
bool operator==(const Rect<U>& other) const
|
2018-10-11 23:03:22 +00:00
|
|
|
{
|
2021-01-22 20:12:37 +00:00
|
|
|
return location() == other.location() && size() == other.size();
|
2019-01-09 01:06:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 20:12:37 +00:00
|
|
|
template<class U>
|
|
|
|
bool operator!=(const Rect<U>& other) const
|
2020-02-11 10:27:13 +00:00
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:15:38 +00:00
|
|
|
Rect<T> operator*(T factor) const { return { m_location * factor, m_size * factor }; }
|
|
|
|
|
|
|
|
Rect<T>& operator*=(T factor)
|
|
|
|
{
|
|
|
|
m_location *= factor;
|
|
|
|
m_size *= factor;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void intersect(const Rect<T>&);
|
2019-01-09 01:06:04 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
static Rect<T> from_two_points(const Point<T>& a, const Point<T>& b)
|
2019-11-17 15:36:43 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
return { min(a.x(), b.x()), min(a.y(), b.y()), abst(a.x() - b.x()), abst(a.y() - b.y()) };
|
2019-11-17 15:36:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
static Rect<T> intersection(const Rect<T>& a, const Rect<T>& b)
|
2019-01-09 01:06:04 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> r = a;
|
2019-01-09 01:06:04 +00:00
|
|
|
r.intersect(b);
|
2019-01-12 05:39:34 +00:00
|
|
|
return r;
|
2018-10-11 23:03:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> intersected(const Rect<T>& other) const
|
2019-04-16 11:58:02 +00:00
|
|
|
{
|
|
|
|
return intersection(*this, other);
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Rect<T> united(const Rect<T>&) const;
|
2019-01-12 23:04:23 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
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() }; }
|
2019-02-28 10:04:03 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void align_within(const Rect<T>&, TextAlignment);
|
2019-05-25 18:15:52 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void center_within(const Rect<T>& other)
|
2019-03-07 22:01:36 +00:00
|
|
|
{
|
|
|
|
center_horizontally_within(other);
|
|
|
|
center_vertically_within(other);
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void center_horizontally_within(const Rect<T>& other)
|
2019-03-03 12:34:50 +00:00
|
|
|
{
|
|
|
|
set_x(other.center().x() - width() / 2);
|
2019-03-07 22:01:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void center_vertically_within(const Rect<T>& other)
|
2019-03-07 22:01:36 +00:00
|
|
|
{
|
2019-03-03 12:34:50 +00:00
|
|
|
set_y(other.center().y() - height() / 2);
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<typename U>
|
|
|
|
Rect<U> to() const
|
|
|
|
{
|
|
|
|
return Rect<U>(*this);
|
|
|
|
}
|
|
|
|
|
2020-02-14 23:58:14 +00:00
|
|
|
String to_string() const;
|
2019-02-09 10:19:38 +00:00
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
private:
|
2020-07-26 04:31:47 +00:00
|
|
|
Point<T> m_location;
|
|
|
|
Size<T> m_size;
|
2018-10-10 14:49:36 +00:00
|
|
|
};
|
2019-01-12 00:00:24 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<typename T>
|
|
|
|
const LogStream& operator<<(const LogStream& stream, const Rect<T>& rect)
|
2019-01-12 00:00:24 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
return stream << rect.to_string();
|
2019-01-12 00:00:24 +00:00
|
|
|
}
|
2019-07-04 04:45:50 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
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()),
|
|
|
|
};
|
|
|
|
}
|
2020-02-06 10:56:38 +00:00
|
|
|
|
|
|
|
}
|
2020-02-15 11:04:35 +00:00
|
|
|
|
2020-10-09 13:44:52 +00:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Formatter<Gfx::Rect<T>> : Formatter<StringView> {
|
2020-12-30 11:14:15 +00:00
|
|
|
void format(FormatBuilder& builder, const Gfx::Rect<T>& value)
|
2020-10-09 13:44:52 +00:00
|
|
|
{
|
2020-12-30 11:14:15 +00:00
|
|
|
Formatter<StringView>::format(builder, value.to_string());
|
2020-10-09 13:44:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-15 11:04:35 +00:00
|
|
|
namespace IPC {
|
2020-07-26 04:31:47 +00:00
|
|
|
|
2020-06-10 08:57:59 +00:00
|
|
|
bool decode(Decoder&, Gfx::IntRect&);
|
|
|
|
bool encode(Encoder&, const Gfx::IntRect&);
|
2020-07-26 04:31:47 +00:00
|
|
|
|
2020-02-15 11:04:35 +00:00
|
|
|
}
|