Rectangle.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef IDRECTANGLE_H_
  21. #define IDRECTANGLE_H_
  22. //
  23. // simple rectangle
  24. //
  25. extern void RotateVector(idVec3 &v, idVec3 origin, float a, float c, float s);
  26. class idRectangle {
  27. public:
  28. float x; // horiz position
  29. float y; // vert position
  30. float w; // width
  31. float h; // height;
  32. idRectangle() { x = y = w= h = 0.0; }
  33. idRectangle(float ix, float iy, float iw, float ih) { x = ix; y = iy; w = iw; h = ih; }
  34. float Bottom() const { return y + h; }
  35. float Right() const { return x + w; }
  36. void Offset (float x, float y) {
  37. this->x += x;
  38. this->y += y;
  39. }
  40. bool Contains(float xt, float yt) {
  41. if (w == 0.0 && h == 0.0) {
  42. return false;
  43. }
  44. if (xt >= x && xt <= Right() && yt >= y && yt <= Bottom()) {
  45. return true;
  46. }
  47. return false;
  48. }
  49. void Empty() { x = y = w = h = 0.0; };
  50. void ClipAgainst(idRectangle r, bool sizeOnly) {
  51. if (!sizeOnly) {
  52. if (x < r.x) {
  53. w -= r.x - x;
  54. x = r.x;
  55. }
  56. if (y < r.y) {
  57. h -= r.y - y;
  58. y = r.y;
  59. }
  60. }
  61. if (x + w > r.x + r.w) {
  62. w = (r.x + r.w) - x;
  63. }
  64. if (y + h > r.y + r.h) {
  65. h = (r.y + r.h) - y;
  66. }
  67. }
  68. void Rotate(float a, idRectangle &out) {
  69. idVec3 p1, p2, p3, p4, p5;
  70. float c, s;
  71. idVec3 center;
  72. center.Set((x + w) / 2.0, (y + h) / 2.0, 0);
  73. p1.Set(x, y, 0);
  74. p2.Set(Right(), y, 0);
  75. p4.Set(x, Bottom(), 0);
  76. if (a) {
  77. s = sin( DEG2RAD( a ) );
  78. c = cos( DEG2RAD( a ) );
  79. }
  80. else {
  81. s = c = 0;
  82. }
  83. RotateVector(p1, center, a, c, s);
  84. RotateVector(p2, center, a, c, s);
  85. RotateVector(p4, center, a, c, s);
  86. out.x = p1.x;
  87. out.y = p1.y;
  88. out.w = (p2 - p1).Length();
  89. out.h = (p4 - p1).Length();
  90. }
  91. idRectangle & operator+=( const idRectangle &a );
  92. idRectangle & operator-=( const idRectangle &a );
  93. idRectangle & operator/=( const idRectangle &a );
  94. idRectangle & operator/=( const float a );
  95. idRectangle & operator*=( const float a );
  96. idRectangle & operator=( const idVec4 v );
  97. int operator==(const idRectangle &a) const;
  98. float & operator[]( const int index );
  99. char * String() const;
  100. const idVec4& ToVec4() const;
  101. };
  102. ID_INLINE const idVec4 &idRectangle::ToVec4() const {
  103. return *reinterpret_cast<const idVec4 *>(&x);
  104. }
  105. ID_INLINE idRectangle &idRectangle::operator+=( const idRectangle &a ) {
  106. x += a.x;
  107. y += a.y;
  108. w += a.w;
  109. h += a.h;
  110. return *this;
  111. }
  112. ID_INLINE idRectangle &idRectangle::operator/=( const idRectangle &a ) {
  113. x /= a.x;
  114. y /= a.y;
  115. w /= a.w;
  116. h /= a.h;
  117. return *this;
  118. }
  119. ID_INLINE idRectangle &idRectangle::operator/=( const float a ) {
  120. float inva = 1.0f / a;
  121. x *= inva;
  122. y *= inva;
  123. w *= inva;
  124. h *= inva;
  125. return *this;
  126. }
  127. ID_INLINE idRectangle &idRectangle::operator-=( const idRectangle &a ) {
  128. x -= a.x;
  129. y -= a.y;
  130. w -= a.w;
  131. h -= a.h;
  132. return *this;
  133. }
  134. ID_INLINE idRectangle &idRectangle::operator*=( const float a ) {
  135. x *= a;
  136. y *= a;
  137. w *= a;
  138. h *= a;
  139. return *this;
  140. }
  141. ID_INLINE idRectangle &idRectangle::operator=( const idVec4 v ) {
  142. x = v.x;
  143. y = v.y;
  144. w = v.z;
  145. h = v.w;
  146. return *this;
  147. }
  148. ID_INLINE int idRectangle::operator==( const idRectangle &a ) const {
  149. return (x == a.x && y == a.y && w == a.w && a.h);
  150. }
  151. ID_INLINE float& idRectangle::operator[]( int index ) {
  152. return ( &x )[ index ];
  153. }
  154. class idRegion {
  155. public:
  156. idRegion() { };
  157. void Empty() {
  158. rects.Clear();
  159. }
  160. bool Contains(float xt, float yt) {
  161. int c = rects.Num();
  162. for (int i = 0; i < c; i++) {
  163. if (rects[i].Contains(xt, yt)) {
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. void AddRect(float x, float y, float w, float h) {
  170. rects.Append(idRectangle(x, y, w, h));
  171. }
  172. int GetRectCount() {
  173. return rects.Num();
  174. }
  175. idRectangle *GetRect(int index) {
  176. if (index >= 0 && index < rects.Num()) {
  177. return &rects[index];
  178. }
  179. return NULL;
  180. }
  181. protected:
  182. idList<idRectangle, TAG_OLD_UI> rects;
  183. };
  184. #endif