diff --git a/esphome/components/display/display_buffer.cpp b/esphome/components/display/display_buffer.cpp index 420801f863..7cd85dabd4 100644 --- a/esphome/components/display/display_buffer.cpp +++ b/esphome/components/display/display_buffer.cpp @@ -32,9 +32,11 @@ void Rect::extend(Rect rect) { this->h = rect.h; } else { if (this->x > rect.x) { + this->w = this->w + (this->x - rect.x); this->x = rect.x; } if (this->y > rect.y) { + this->h = this->h + (this->y - rect.y); this->y = rect.y; } if (this->x2() < rect.x2()) { @@ -49,29 +51,35 @@ void Rect::shrink(Rect rect) { if (!this->inside(rect)) { (*this) = Rect(); } else { - if (this->x < rect.x) { - this->x = rect.x; - } - if (this->y < rect.y) { - this->y = rect.y; - } if (this->x2() > rect.x2()) { this->w = rect.x2() - this->x; } + if (this->x < rect.x) { + this->w = this->w + (this->x - rect.x); + this->x = rect.x; + } if (this->y2() > rect.y2()) { this->h = rect.y2() - this->y; } + if (this->y < rect.y) { + this->h = this->h + (this->y - rect.y); + this->y = rect.y; + } } } -bool Rect::inside(int16_t x, int16_t y, bool absolute) { // NOLINT +bool Rect::equal(Rect rect) { + return (rect.x == this->x) && (rect.w == this->w) && (rect.y == this->y) && (rect.h == this->h); +} + +bool Rect::inside(int16_t test_x, int16_t test_y, bool absolute) { // NOLINT if (!this->is_set()) { return true; } if (absolute) { - return ((x >= 0) && (x <= this->w) && (y >= 0) && (y <= this->h)); + return ((test_x >= this->x) && (test_x <= this->x2()) && (test_y >= this->y) && (test_y <= this->y2())); } else { - return ((x >= this->x) && (x <= this->x2()) && (y >= this->y) && (y <= this->y2())); + return ((test_x >= 0) && (test_x <= this->w) && (test_y >= 0) && (test_y <= this->h)); } } @@ -80,15 +88,16 @@ bool Rect::inside(Rect rect, bool absolute) { return true; } if (absolute) { - return ((rect.x <= this->w) && (rect.w >= 0) && (rect.y <= this->h) && (rect.h >= 0)); - } else { return ((rect.x <= this->x2()) && (rect.x2() >= this->x) && (rect.y <= this->y2()) && (rect.y2() >= this->y)); + } else { + return ((rect.x <= this->w) && (rect.w >= 0) && (rect.y <= this->h) && (rect.h >= 0)); } } void Rect::info(const std::string &prefix) { if (this->is_set()) { - ESP_LOGI(TAG, "%s [%3d,%3d,%3d,%3d]", prefix.c_str(), this->x, this->y, this->w, this->h); + ESP_LOGI(TAG, "%s [%3d,%3d,%3d,%3d] (%3d,%3d)", prefix.c_str(), this->x, this->y, this->w, this->h, this->x2(), + this->y2()); } else ESP_LOGI(TAG, "%s ** IS NOT SET **", prefix.c_str()); } diff --git a/esphome/components/display/display_buffer.h b/esphome/components/display/display_buffer.h index 0402826594..4477685e1b 100644 --- a/esphome/components/display/display_buffer.h +++ b/esphome/components/display/display_buffer.h @@ -120,8 +120,9 @@ class Rect { void extend(Rect rect); void shrink(Rect rect); - bool inside(Rect rect, bool absolute = false); - bool inside(int16_t x, int16_t y, bool absolute = false); + bool inside(Rect rect, bool absolute = true); + bool inside(int16_t test_x, int16_t test_y, bool absolute = true); + bool equal(Rect rect); void info(const std::string &prefix = "rect info:"); };