Rect.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (C) 2006 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_UI_RECT
  17. #define ANDROID_UI_RECT
  18. #include <utils/Flattenable.h>
  19. #include <utils/Log.h>
  20. #include <utils/TypeHelpers.h>
  21. #include <ui/Point.h>
  22. #include <android/rect.h>
  23. namespace android {
  24. class Rect : public ARect, public LightFlattenablePod<Rect>
  25. {
  26. public:
  27. typedef ARect::value_type value_type;
  28. static const Rect INVALID_RECT;
  29. // we don't provide copy-ctor and operator= on purpose
  30. // because we want the compiler generated versions
  31. inline Rect() {
  32. left = right = top = bottom = 0;
  33. }
  34. inline Rect(int32_t w, int32_t h) {
  35. left = top = 0;
  36. right = w;
  37. bottom = h;
  38. }
  39. inline Rect(uint32_t w, uint32_t h) {
  40. if (w > INT32_MAX) {
  41. ALOG(LOG_WARN, "Rect",
  42. "Width %u too large for Rect class, clamping", w);
  43. w = INT32_MAX;
  44. }
  45. if (h > INT32_MAX) {
  46. ALOG(LOG_WARN, "Rect",
  47. "Height %u too large for Rect class, clamping", h);
  48. h = INT32_MAX;
  49. }
  50. left = top = 0;
  51. right = w;
  52. bottom = h;
  53. }
  54. inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
  55. left = l;
  56. top = t;
  57. right = r;
  58. bottom = b;
  59. }
  60. inline Rect(const Point& lt, const Point& rb) {
  61. left = lt.x;
  62. top = lt.y;
  63. right = rb.x;
  64. bottom = rb.y;
  65. }
  66. void makeInvalid();
  67. inline void clear() {
  68. left = top = right = bottom = 0;
  69. }
  70. // a valid rectangle has a non negative width and height
  71. inline bool isValid() const {
  72. return (getWidth() >= 0) && (getHeight() >= 0);
  73. }
  74. // an empty rect has a zero width or height, or is invalid
  75. inline bool isEmpty() const {
  76. return (getWidth() <= 0) || (getHeight() <= 0);
  77. }
  78. // rectangle's width
  79. inline int32_t getWidth() const {
  80. return right - left;
  81. }
  82. // rectangle's height
  83. inline int32_t getHeight() const {
  84. return bottom - top;
  85. }
  86. inline Rect getBounds() const {
  87. return Rect(right - left, bottom - top);
  88. }
  89. void setLeftTop(const Point& lt) {
  90. left = lt.x;
  91. top = lt.y;
  92. }
  93. void setRightBottom(const Point& rb) {
  94. right = rb.x;
  95. bottom = rb.y;
  96. }
  97. // the following 4 functions return the 4 corners of the rect as Point
  98. Point leftTop() const {
  99. return Point(left, top);
  100. }
  101. Point rightBottom() const {
  102. return Point(right, bottom);
  103. }
  104. Point rightTop() const {
  105. return Point(right, top);
  106. }
  107. Point leftBottom() const {
  108. return Point(left, bottom);
  109. }
  110. // comparisons
  111. inline bool operator == (const Rect& rhs) const {
  112. return (left == rhs.left) && (top == rhs.top) &&
  113. (right == rhs.right) && (bottom == rhs.bottom);
  114. }
  115. inline bool operator != (const Rect& rhs) const {
  116. return !operator == (rhs);
  117. }
  118. // operator < defines an order which allows to use rectangles in sorted
  119. // vectors.
  120. bool operator < (const Rect& rhs) const;
  121. const Rect operator + (const Point& rhs) const;
  122. const Rect operator - (const Point& rhs) const;
  123. Rect& operator += (const Point& rhs) {
  124. return offsetBy(rhs.x, rhs.y);
  125. }
  126. Rect& operator -= (const Point& rhs) {
  127. return offsetBy(-rhs.x, -rhs.y);
  128. }
  129. Rect& offsetToOrigin() {
  130. right -= left;
  131. bottom -= top;
  132. left = top = 0;
  133. return *this;
  134. }
  135. Rect& offsetTo(const Point& p) {
  136. return offsetTo(p.x, p.y);
  137. }
  138. Rect& offsetBy(const Point& dp) {
  139. return offsetBy(dp.x, dp.y);
  140. }
  141. Rect& offsetTo(int32_t x, int32_t y);
  142. Rect& offsetBy(int32_t x, int32_t y);
  143. bool intersect(const Rect& with, Rect* result) const;
  144. // Create a new Rect by transforming this one using a graphics HAL
  145. // transform. This rectangle is defined in a coordinate space starting at
  146. // the origin and extending to (width, height). If the transform includes
  147. // a ROT90 then the output rectangle is defined in a space extending to
  148. // (height, width). Otherwise the output rectangle is in the same space as
  149. // the input.
  150. Rect transform(uint32_t xform, int32_t width, int32_t height) const;
  151. // this calculates (Region(*this) - exclude).bounds() efficiently
  152. Rect reduce(const Rect& exclude) const;
  153. // for backward compatibility
  154. inline int32_t width() const { return getWidth(); }
  155. inline int32_t height() const { return getHeight(); }
  156. inline void set(const Rect& rhs) { operator = (rhs); }
  157. };
  158. ANDROID_BASIC_TYPES_TRAITS(Rect)
  159. }; // namespace android
  160. #endif // ANDROID_UI_RECT