The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

534 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_RECTANGLE_JUCEHEADER__
  19. #define __JUCE_RECTANGLE_JUCEHEADER__
  20. #include "../../../text/juce_StringArray.h"
  21. #include "juce_Point.h"
  22. class RectangleList;
  23. //==============================================================================
  24. /**
  25. Manages a rectangle and allows geometric operations to be performed on it.
  26. @see RectangleList, Path, Line, Point
  27. */
  28. template <typename ValueType>
  29. class Rectangle
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a rectangle of zero size.
  34. The default co-ordinates will be (0, 0, 0, 0).
  35. */
  36. Rectangle() throw()
  37. : x (0), y (0), w (0), h (0)
  38. {
  39. }
  40. /** Creates a copy of another rectangle. */
  41. Rectangle (const Rectangle& other) throw()
  42. : x (other.x), y (other.y),
  43. w (other.w), h (other.h)
  44. {
  45. }
  46. /** Creates a rectangle with a given position and size. */
  47. Rectangle (const ValueType initialX, const ValueType initialY,
  48. const ValueType width, const ValueType height) throw()
  49. : x (initialX), y (initialY),
  50. w (width), h (height)
  51. {
  52. }
  53. /** Creates a rectangle with a given size, and a position of (0, 0). */
  54. Rectangle (const ValueType width, const ValueType height) throw()
  55. : x (0), y (0), w (width), h (height)
  56. {
  57. }
  58. Rectangle& operator= (const Rectangle& other) throw()
  59. {
  60. x = other.x; y = other.y;
  61. w = other.w; h = other.h;
  62. return *this;
  63. }
  64. /** Destructor. */
  65. ~Rectangle() throw() {}
  66. //==============================================================================
  67. /** Returns true if the rectangle's width and height are both zero or less */
  68. bool isEmpty() const throw() { return w <= 0 || h <= 0; }
  69. /** Returns the x co-ordinate of the rectangle's left-hand-side. */
  70. inline ValueType getX() const throw() { return x; }
  71. /** Returns the y co-ordinate of the rectangle's top edge. */
  72. inline ValueType getY() const throw() { return y; }
  73. /** Returns the width of the rectangle. */
  74. inline ValueType getWidth() const throw() { return w; }
  75. /** Returns the height of the rectangle. */
  76. inline ValueType getHeight() const throw() { return h; }
  77. /** Returns the x co-ordinate of the rectangle's right-hand-side. */
  78. inline ValueType getRight() const throw() { return x + w; }
  79. /** Returns the y co-ordinate of the rectangle's bottom edge. */
  80. inline ValueType getBottom() const throw() { return y + h; }
  81. /** Returns the x co-ordinate of the rectangle's centre. */
  82. ValueType getCentreX() const throw() { return x + w / (ValueType) 2; }
  83. /** Returns the y co-ordinate of the rectangle's centre. */
  84. ValueType getCentreY() const throw() { return y + h / (ValueType) 2; }
  85. /** Returns the centre point of the rectangle. */
  86. const Point<ValueType> getCentre() const throw() { return Point<ValueType> (x + w / (ValueType) 2, y + h / (ValueType) 2); }
  87. /** Returns the aspect ratio of the rectangle's width / height.
  88. If widthOverHeight is true, it returns width / height; if widthOverHeight is false,
  89. it returns height / width. */
  90. ValueType getAspectRatio (const bool widthOverHeight = true) const throw() { return widthOverHeight ? w / h : h / w; }
  91. //==============================================================================
  92. /** Returns the rectangle's top-left position as a Point. */
  93. const Point<ValueType> getPosition() const throw() { return Point<ValueType> (x, y); }
  94. /** Changes the position of the rectangle's top-left corner (leaving its size unchanged). */
  95. void setPosition (const Point<ValueType>& newPos) throw() { x = newPos.getX(); y = newPos.getY(); }
  96. /** Changes the position of the rectangle's top-left corner (leaving its size unchanged). */
  97. void setPosition (const ValueType newX, const ValueType newY) throw() { x = newX; y = newY; }
  98. /** Returns a rectangle with the same size as this one, but a new position. */
  99. const Rectangle withPosition (const Point<ValueType>& newPos) const throw() { return Rectangle (newPos.getX(), newPos.getY(), w, h); }
  100. /** Changes the rectangle's size, leaving the position of its top-left corner unchanged. */
  101. void setSize (const ValueType newWidth, const ValueType newHeight) throw() { w = newWidth; h = newHeight; }
  102. /** Returns a rectangle with the same position as this one, but a new size. */
  103. const Rectangle withSize (const ValueType newWidth, const ValueType newHeight) const throw() { return Rectangle (x, y, newWidth, newHeight); }
  104. /** Changes all the rectangle's co-ordinates. */
  105. void setBounds (const ValueType newX, const ValueType newY,
  106. const ValueType newWidth, const ValueType newHeight) throw()
  107. {
  108. x = newX; y = newY; w = newWidth; h = newHeight;
  109. }
  110. /** Changes the rectangle's width */
  111. void setWidth (const ValueType newWidth) throw() { w = newWidth; }
  112. /** Changes the rectangle's height */
  113. void setHeight (const ValueType newHeight) throw() { h = newHeight; }
  114. /** Moves the x position, adjusting the width so that the right-hand edge remains in the same place.
  115. If the x is moved to be on the right of the current right-hand edge, the width will be set to zero.
  116. */
  117. void setLeft (const ValueType newLeft) throw()
  118. {
  119. w = jmax (ValueType(), x + w - newLeft);
  120. x = newLeft;
  121. }
  122. /** Moves the y position, adjusting the height so that the bottom edge remains in the same place.
  123. If the y is moved to be below the current bottom edge, the height will be set to zero.
  124. */
  125. void setTop (const ValueType newTop) throw()
  126. {
  127. h = jmax (ValueType(), y + h - newTop);
  128. y = newTop;
  129. }
  130. /** Adjusts the width so that the right-hand edge of the rectangle has this new value.
  131. If the new right is below the current X value, the X will be pushed down to match it.
  132. @see getRight
  133. */
  134. void setRight (const ValueType newRight) throw()
  135. {
  136. x = jmin (x, newRight);
  137. w = newRight - x;
  138. }
  139. /** Adjusts the height so that the bottom edge of the rectangle has this new value.
  140. If the new bottom is lower than the current Y value, the Y will be pushed down to match it.
  141. @see getBottom
  142. */
  143. void setBottom (const ValueType newBottom) throw()
  144. {
  145. y = jmin (y, newBottom);
  146. h = newBottom - y;
  147. }
  148. //==============================================================================
  149. /** Moves the rectangle's position by adding amount to its x and y co-ordinates. */
  150. void translate (const ValueType deltaX,
  151. const ValueType deltaY) throw()
  152. {
  153. x += deltaX;
  154. y += deltaY;
  155. }
  156. /** Returns a rectangle which is the same as this one moved by a given amount. */
  157. const Rectangle translated (const ValueType deltaX,
  158. const ValueType deltaY) const throw()
  159. {
  160. return Rectangle (x + deltaX, y + deltaY, w, h);
  161. }
  162. /** Expands the rectangle by a given amount.
  163. Effectively, its new size is (x - deltaX, y - deltaY, w + deltaX * 2, h + deltaY * 2).
  164. @see expanded, reduce, reduced
  165. */
  166. void expand (const ValueType deltaX,
  167. const ValueType deltaY) throw()
  168. {
  169. const ValueType nw = jmax (ValueType(), w + deltaX * 2);
  170. const ValueType nh = jmax (ValueType(), h + deltaY * 2);
  171. setBounds (x - deltaX, y - deltaY, nw, nh);
  172. }
  173. /** Returns a rectangle that is larger than this one by a given amount.
  174. Effectively, the rectangle returned is (x - deltaX, y - deltaY, w + deltaX * 2, h + deltaY * 2).
  175. @see expand, reduce, reduced
  176. */
  177. const Rectangle expanded (const ValueType deltaX,
  178. const ValueType deltaY) const throw()
  179. {
  180. const ValueType nw = jmax (ValueType(), w + deltaX * 2);
  181. const ValueType nh = jmax (ValueType(), h + deltaY * 2);
  182. return Rectangle (x - deltaX, y - deltaY, nw, nh);
  183. }
  184. /** Shrinks the rectangle by a given amount.
  185. Effectively, its new size is (x + deltaX, y + deltaY, w - deltaX * 2, h - deltaY * 2).
  186. @see reduced, expand, expanded
  187. */
  188. void reduce (const ValueType deltaX,
  189. const ValueType deltaY) throw()
  190. {
  191. expand (-deltaX, -deltaY);
  192. }
  193. /** Returns a rectangle that is smaller than this one by a given amount.
  194. Effectively, the rectangle returned is (x + deltaX, y + deltaY, w - deltaX * 2, h - deltaY * 2).
  195. @see reduce, expand, expanded
  196. */
  197. const Rectangle reduced (const ValueType deltaX,
  198. const ValueType deltaY) const throw()
  199. {
  200. return expanded (-deltaX, -deltaY);
  201. }
  202. //==============================================================================
  203. /** Returns true if the two rectangles are identical. */
  204. bool operator== (const Rectangle& other) const throw()
  205. {
  206. return x == other.x && y == other.y
  207. && w == other.w && h == other.h;
  208. }
  209. /** Returns true if the two rectangles are not identical. */
  210. bool operator!= (const Rectangle& other) const throw()
  211. {
  212. return x != other.x || y != other.y
  213. || w != other.w || h != other.h;
  214. }
  215. /** Returns true if this co-ordinate is inside the rectangle. */
  216. bool contains (const ValueType xCoord, const ValueType yCoord) const throw()
  217. {
  218. return xCoord >= x && yCoord >= y && xCoord < x + w && yCoord < y + h;
  219. }
  220. /** Returns true if this co-ordinate is inside the rectangle. */
  221. bool contains (const Point<ValueType> point) const throw()
  222. {
  223. return point.getX() >= x && point.getY() >= y && point.getX() < x + w && point.getY() < y + h;
  224. }
  225. /** Returns true if this other rectangle is completely inside this one. */
  226. bool contains (const Rectangle& other) const throw()
  227. {
  228. return x <= other.x && y <= other.y
  229. && x + w >= other.x + other.w && y + h >= other.y + other.h;
  230. }
  231. /** Returns the nearest point to the specified point that lies within this rectangle. */
  232. const Point<ValueType> getConstrainedPoint (const Point<ValueType>& point) const throw()
  233. {
  234. return Point<ValueType> (jlimit (x, x + w, point.getX()),
  235. jlimit (y, y + h, point.getY()));
  236. }
  237. /** Returns true if any part of another rectangle overlaps this one. */
  238. bool intersects (const Rectangle& other) const throw()
  239. {
  240. return x + w > other.x
  241. && y + h > other.y
  242. && x < other.x + other.w
  243. && y < other.y + other.h
  244. && w > ValueType() && h > ValueType();
  245. }
  246. /** Returns the region that is the overlap between this and another rectangle.
  247. If the two rectangles don't overlap, the rectangle returned will be empty.
  248. */
  249. const Rectangle getIntersection (const Rectangle& other) const throw()
  250. {
  251. const ValueType nx = jmax (x, other.x);
  252. const ValueType ny = jmax (y, other.y);
  253. const ValueType nw = jmin (x + w, other.x + other.w) - nx;
  254. const ValueType nh = jmin (y + h, other.y + other.h) - ny;
  255. if (nw >= ValueType() && nh >= ValueType())
  256. return Rectangle (nx, ny, nw, nh);
  257. return Rectangle();
  258. }
  259. /** Clips a rectangle so that it lies only within this one.
  260. This is a non-static version of intersectRectangles().
  261. Returns false if the two regions didn't overlap.
  262. */
  263. bool intersectRectangle (ValueType& otherX, ValueType& otherY, ValueType& otherW, ValueType& otherH) const throw()
  264. {
  265. const int maxX = jmax (otherX, x);
  266. otherW = jmin (otherX + otherW, x + w) - maxX;
  267. if (otherW > 0)
  268. {
  269. const int maxY = jmax (otherY, y);
  270. otherH = jmin (otherY + otherH, y + h) - maxY;
  271. if (otherH > 0)
  272. {
  273. otherX = maxX; otherY = maxY;
  274. return true;
  275. }
  276. }
  277. return false;
  278. }
  279. /** Returns the smallest rectangle that contains both this one and the one
  280. passed-in.
  281. */
  282. const Rectangle getUnion (const Rectangle& other) const throw()
  283. {
  284. const ValueType newX = jmin (x, other.x);
  285. const ValueType newY = jmin (y, other.y);
  286. return Rectangle (newX, newY,
  287. jmax (x + w, other.x + other.w) - newX,
  288. jmax (y + h, other.y + other.h) - newY);
  289. }
  290. /** If this rectangle merged with another one results in a simple rectangle, this
  291. will set this rectangle to the result, and return true.
  292. Returns false and does nothing to this rectangle if the two rectangles don't overlap,
  293. or if they form a complex region.
  294. */
  295. bool enlargeIfAdjacent (const Rectangle& other) throw()
  296. {
  297. if (x == other.x && getRight() == other.getRight()
  298. && (other.getBottom() >= y && other.y <= getBottom()))
  299. {
  300. const ValueType newY = jmin (y, other.y);
  301. h = jmax (getBottom(), other.getBottom()) - newY;
  302. y = newY;
  303. return true;
  304. }
  305. else if (y == other.y && getBottom() == other.getBottom()
  306. && (other.getRight() >= x && other.x <= getRight()))
  307. {
  308. const ValueType newX = jmin (x, other.x);
  309. w = jmax (getRight(), other.getRight()) - newX;
  310. x = newX;
  311. return true;
  312. }
  313. return false;
  314. }
  315. /** If after removing another rectangle from this one the result is a simple rectangle,
  316. this will set this object's bounds to be the result, and return true.
  317. Returns false and does nothing to this rectangle if the two rectangles don't overlap,
  318. or if removing the other one would form a complex region.
  319. */
  320. bool reduceIfPartlyContainedIn (const Rectangle& other) throw()
  321. {
  322. int inside = 0;
  323. const int otherR = other.getRight();
  324. if (x >= other.x && x < otherR) inside = 1;
  325. const int otherB = other.getBottom();
  326. if (y >= other.y && y < otherB) inside |= 2;
  327. const int r = x + w;
  328. if (r >= other.x && r < otherR) inside |= 4;
  329. const int b = y + h;
  330. if (b >= other.y && b < otherB) inside |= 8;
  331. switch (inside)
  332. {
  333. case 1 + 2 + 8: w = r - otherR; x = otherR; return true;
  334. case 1 + 2 + 4: h = b - otherB; y = otherB; return true;
  335. case 2 + 4 + 8: w = other.x - x; return true;
  336. case 1 + 4 + 8: h = other.y - y; return true;
  337. }
  338. return false;
  339. }
  340. /** Returns the smallest rectangle that can contain the shape created by applying
  341. a transform to this rectangle.
  342. This should only be used on floating point rectangles.
  343. */
  344. const Rectangle<ValueType> transformed (const AffineTransform& transform) const throw()
  345. {
  346. float x1 = x, y1 = y;
  347. float x2 = x + w, y2 = y;
  348. float x3 = x, y3 = y + h;
  349. float x4 = x2, y4 = y3;
  350. transform.transformPoint (x1, y1);
  351. transform.transformPoint (x2, y2);
  352. transform.transformPoint (x3, y3);
  353. transform.transformPoint (x4, y4);
  354. const float x = jmin (x1, x2, x3, x4);
  355. const float y = jmin (y1, y2, y3, y4);
  356. return Rectangle (x, y,
  357. jmax (x1, x2, x3, x4) - x,
  358. jmax (y1, y2, y3, y4) - y);
  359. }
  360. /** Returns the smallest integer-aligned rectangle that completely contains this one.
  361. This is only relevent for floating-point rectangles, of course.
  362. */
  363. const Rectangle<int> getSmallestIntegerContainer() const throw()
  364. {
  365. const int x1 = (int) floorf ((float) x);
  366. const int y1 = (int) floorf ((float) y);
  367. const int x2 = (int) floorf ((float) (x + w + 0.9999f));
  368. const int y2 = (int) floorf ((float) (y + h + 0.9999f));
  369. return Rectangle<int> (x1, y1, x2 - x1, y2 - y1);
  370. }
  371. //==============================================================================
  372. /** Static utility to intersect two sets of rectangular co-ordinates.
  373. Returns false if the two regions didn't overlap.
  374. @see intersectRectangle
  375. */
  376. static bool intersectRectangles (ValueType& x1, ValueType& y1, ValueType& w1, ValueType& h1,
  377. const ValueType x2, const ValueType y2, const ValueType w2, const ValueType h2) throw()
  378. {
  379. const ValueType x = jmax (x1, x2);
  380. w1 = jmin (x1 + w1, x2 + w2) - x;
  381. if (w1 > 0)
  382. {
  383. const ValueType y = jmax (y1, y2);
  384. h1 = jmin (y1 + h1, y2 + h2) - y;
  385. if (h1 > 0)
  386. {
  387. x1 = x; y1 = y;
  388. return true;
  389. }
  390. }
  391. return false;
  392. }
  393. //==============================================================================
  394. /** Creates a string describing this rectangle.
  395. The string will be of the form "x y width height", e.g. "100 100 400 200".
  396. Coupled with the fromString() method, this is very handy for things like
  397. storing rectangles (particularly component positions) in XML attributes.
  398. @see fromString
  399. */
  400. const String toString() const
  401. {
  402. String s;
  403. s.preallocateStorage (16);
  404. s << x << T(' ') << y << T(' ') << w << T(' ') << h;
  405. return s;
  406. }
  407. /** Parses a string containing a rectangle's details.
  408. The string should contain 4 integer tokens, in the form "x y width height". They
  409. can be comma or whitespace separated.
  410. This method is intended to go with the toString() method, to form an easy way
  411. of saving/loading rectangles as strings.
  412. @see toString
  413. */
  414. static const Rectangle fromString (const String& stringVersion)
  415. {
  416. StringArray toks;
  417. toks.addTokens (stringVersion.trim(), T(",; \t\r\n"), 0);
  418. return Rectangle (toks[0].trim().getIntValue(),
  419. toks[1].trim().getIntValue(),
  420. toks[2].trim().getIntValue(),
  421. toks[3].trim().getIntValue());
  422. }
  423. //==============================================================================
  424. juce_UseDebuggingNewOperator
  425. private:
  426. friend class RectangleList;
  427. ValueType x, y, w, h;
  428. };
  429. #endif // __JUCE_RECTANGLE_JUCEHEADER__