2018-09-11 22:27:47 +00:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-31 22:31:03 +00:00
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2018-03-19 02:04:13 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-05-16 06:32:36 +00:00
|
|
|
#include <algorithm>
|
2018-04-10 19:45:33 +00:00
|
|
|
#include <tuple>
|
|
|
|
|
2018-07-02 04:22:21 +00:00
|
|
|
#include <fabric/graphics/Float.h>
|
2018-03-19 02:04:25 +00:00
|
|
|
|
2018-03-19 02:04:13 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Point
|
|
|
|
*/
|
|
|
|
struct Point {
|
|
|
|
Float x {0};
|
|
|
|
Float y {0};
|
|
|
|
|
2018-09-04 05:53:13 +00:00
|
|
|
Point &operator+=(const Point &point) {
|
|
|
|
x += point.x;
|
|
|
|
y += point.y;
|
2018-03-19 02:04:13 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-09-04 05:53:13 +00:00
|
|
|
Point &operator*=(const Point &point) {
|
|
|
|
x *= point.x;
|
|
|
|
y *= point.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend Point operator+(Point lhs, const Point &rhs) {
|
2018-03-19 02:04:13 +00:00
|
|
|
return lhs += rhs;
|
|
|
|
}
|
2018-03-19 23:51:29 +00:00
|
|
|
|
2018-09-04 05:53:13 +00:00
|
|
|
bool operator==(const Point &rhs) const {
|
2018-03-19 23:51:29 +00:00
|
|
|
return
|
|
|
|
std::tie(this->x, this->y) ==
|
|
|
|
std::tie(rhs.x, rhs.y);
|
|
|
|
}
|
|
|
|
|
2018-09-04 05:53:13 +00:00
|
|
|
bool operator!=(const Point &rhs) const {
|
2018-03-19 23:51:29 +00:00
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2018-03-19 02:04:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Size
|
|
|
|
*/
|
|
|
|
struct Size {
|
|
|
|
Float width {0};
|
|
|
|
Float height {0};
|
2018-03-19 23:51:29 +00:00
|
|
|
|
2018-09-04 05:53:13 +00:00
|
|
|
Size &operator+=(const Point &point) {
|
|
|
|
width += point.x;
|
|
|
|
height += point.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size &operator*=(const Point &point) {
|
|
|
|
width *= point.x;
|
|
|
|
height *= point.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Size &rhs) const {
|
2018-03-19 23:51:29 +00:00
|
|
|
return
|
|
|
|
std::tie(this->width, this->height) ==
|
|
|
|
std::tie(rhs.width, rhs.height);
|
|
|
|
}
|
|
|
|
|
2018-09-04 05:53:13 +00:00
|
|
|
bool operator!=(const Size &rhs) const {
|
2018-03-19 23:51:29 +00:00
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2018-03-19 02:04:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rect: Point and Size
|
|
|
|
*/
|
|
|
|
struct Rect {
|
|
|
|
Point origin {0, 0};
|
|
|
|
Size size {0, 0};
|
2018-03-19 23:51:29 +00:00
|
|
|
|
2018-09-04 05:53:13 +00:00
|
|
|
bool operator==(const Rect &rhs) const {
|
2018-03-19 23:51:29 +00:00
|
|
|
return
|
|
|
|
std::tie(this->origin, this->size) ==
|
|
|
|
std::tie(rhs.origin, rhs.size);
|
|
|
|
}
|
|
|
|
|
2018-09-04 05:53:13 +00:00
|
|
|
bool operator!=(const Rect &rhs) const {
|
2018-03-19 23:51:29 +00:00
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2018-05-16 06:32:36 +00:00
|
|
|
|
|
|
|
Float getMaxX() const { return size.width > 0 ? origin.x + size.width : origin.x; }
|
|
|
|
Float getMaxY() const { return size.height > 0 ? origin.y + size.height : origin.y; }
|
|
|
|
Float getMinX() const { return size.width >= 0 ? origin.x : origin.x + size.width; }
|
|
|
|
Float getMinY() const { return size.height >= 0 ? origin.y : origin.y + size.height; }
|
|
|
|
|
|
|
|
void unionInPlace(const Rect &rect) {
|
2018-09-10 23:33:48 +00:00
|
|
|
auto x1 = std::min(getMinX(), rect.getMinX());
|
|
|
|
auto y1 = std::min(getMinY(), rect.getMinY());
|
|
|
|
auto x2 = std::max(getMaxX(), rect.getMaxX());
|
|
|
|
auto y2 = std::max(getMaxY(), rect.getMaxY());
|
2018-05-16 06:32:36 +00:00
|
|
|
origin = {x1, y1};
|
|
|
|
size = {x2 - x1, y2 - y1};
|
|
|
|
}
|
2018-03-19 02:04:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2018-09-04 05:53:35 +00:00
|
|
|
* Generic data structure describes some values associated with *edges*
|
|
|
|
* of a rectangle.
|
2018-03-19 02:04:13 +00:00
|
|
|
*/
|
2018-09-04 05:53:35 +00:00
|
|
|
template <typename T>
|
|
|
|
struct RectangleEdges {
|
|
|
|
T left {};
|
|
|
|
T top {};
|
|
|
|
T right {};
|
|
|
|
T bottom {};
|
|
|
|
|
|
|
|
bool operator==(const RectangleEdges<T> &rhs) const {
|
2018-03-19 23:51:29 +00:00
|
|
|
return
|
|
|
|
std::tie(this->left, this->top, this->right, this->bottom) ==
|
|
|
|
std::tie(rhs.left, rhs.top, rhs.right, rhs.bottom);
|
|
|
|
}
|
|
|
|
|
2018-09-04 05:53:35 +00:00
|
|
|
bool operator!=(const RectangleEdges<T> &rhs) const {
|
2018-03-19 23:51:29 +00:00
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2018-06-22 18:53:48 +00:00
|
|
|
|
|
|
|
bool isUniform() const {
|
|
|
|
return left == top &&
|
|
|
|
left == right &&
|
|
|
|
left == bottom;
|
|
|
|
}
|
2018-03-19 02:04:13 +00:00
|
|
|
};
|
|
|
|
|
2018-06-15 18:25:13 +00:00
|
|
|
/*
|
2018-09-04 05:53:35 +00:00
|
|
|
* Generic data structure describes some values associated with *corners*
|
|
|
|
* of a rectangle.
|
2018-06-15 18:25:13 +00:00
|
|
|
*/
|
2018-09-04 05:53:35 +00:00
|
|
|
template <typename T>
|
|
|
|
struct RectangleCorners {
|
|
|
|
T topLeft {};
|
|
|
|
T topRight {};
|
|
|
|
T bottomLeft {};
|
|
|
|
T bottomRight {};
|
|
|
|
|
|
|
|
bool operator==(const RectangleCorners<T> &rhs) const {
|
2018-06-15 18:25:13 +00:00
|
|
|
return
|
2018-09-04 05:53:35 +00:00
|
|
|
std::tie(this->topLeft, this->topRight, this->bottomLeft, this->bottomRight) ==
|
|
|
|
std::tie(rhs.topLeft, rhs.topRight, rhs.bottomLeft, rhs.bottomRight);
|
2018-06-15 18:25:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 05:53:35 +00:00
|
|
|
bool operator!=(const RectangleCorners<T> &rhs) const {
|
2018-06-15 18:25:13 +00:00
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2018-06-22 18:53:48 +00:00
|
|
|
|
|
|
|
bool isUniform() const {
|
|
|
|
return topLeft == topRight &&
|
|
|
|
topLeft == bottomLeft &&
|
|
|
|
topLeft == bottomRight;
|
|
|
|
}
|
2018-06-15 18:25:13 +00:00
|
|
|
};
|
|
|
|
|
2018-09-04 05:53:35 +00:00
|
|
|
/*
|
|
|
|
* EdgeInsets
|
|
|
|
*/
|
|
|
|
using EdgeInsets = RectangleEdges<Float>;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CornerInsets
|
|
|
|
*/
|
|
|
|
using CornerInsets = RectangleCorners<Float>;
|
|
|
|
|
2018-03-19 02:04:13 +00:00
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|