7 #ifndef _FCITX_UTILS_RECT_H_ 8 #define _FCITX_UTILS_RECT_H_ 18 #include "fcitxutils_export.h" 21 class FCITXUTILS_EXPORT
Rect {
23 explicit Rect(
int _x1 = 0,
int _y1 = 0,
int _x2 = 0,
int _y2 = 0)
24 : x1_(_x1), y1_(_y1), x2_(_x2), y2_(_y2) {}
27 Rect &operator=(
const Rect &rect) =
default;
29 inline Rect &setPosition(
int x,
int y) noexcept {
35 inline Rect &setSize(
int width,
int height) noexcept {
41 inline Rect &setLeft(
int pos) noexcept {
45 inline Rect &setTop(
int pos) noexcept {
49 inline Rect &setRight(
int pos) noexcept {
53 inline Rect &setBottom(
int pos) noexcept {
59 inline Rect intersected(
const Rect &rect) noexcept {
60 return std::as_const(*this).intersected(rect);
63 inline Rect intersected(
const Rect &rect)
const noexcept {
65 tmp.x1_ = std::max(x1_, rect.x1_);
66 tmp.x2_ = std::min(x2_, rect.x2_);
67 tmp.y1_ = std::max(y1_, rect.y1_);
68 tmp.y2_ = std::min(y2_, rect.y2_);
69 if (tmp.x1_ < tmp.x2_ && tmp.y1_ < tmp.y2_) {
76 Rect translated(
int offsetX,
int offsetY)
const {
78 .setPosition(x1_ + offsetX, y1_ + offsetY)
79 .setSize(width(), height());
82 bool contains(
int x,
int y)
const noexcept {
83 return (x1_ <= x && x <= x2_) && (y1_ <= y && y <= y2_);
86 bool contains(
const Rect &other)
const noexcept {
87 return contains(other.x1_, other.y1_) && contains(other.x2_, other.y2_);
90 int distance(
int x,
int y)
const noexcept {
100 }
else if (y > y2_) {
106 inline int left()
const noexcept {
return x1_; }
107 inline int top()
const noexcept {
return y1_; }
108 inline int right()
const noexcept {
return x2_; }
109 inline int bottom()
const noexcept {
return y2_; }
111 inline int width()
const noexcept {
return x2_ - x1_; }
112 inline int height()
const noexcept {
return y2_ - y1_; }
114 inline bool operator==(
const Rect &other)
const {
115 return x1_ == other.x1_ && x2_ == other.x2_ && y1_ == other.y1_ &&
119 inline bool operator!=(
const Rect &other)
const {
120 return !operator==(other);
123 inline bool isEmpty()
const {
return width() <= 0 || height() <= 0; }
126 int x1_, y1_, x2_, y2_;
129 inline std::ostream &operator<<(std::ostream &os,
const Rect &r) {
130 os <<
"Rect(" << r.left() <<
"," << r.top() <<
"+" << r.width() <<
"x" 131 << r.height() <<
")";
137 #endif // _FCITX_UTILS_RECT_H_