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.

272 lines
9.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_RECTANGLELIST_H_INCLUDED
  18. #define JUCE_RECTANGLELIST_H_INCLUDED
  19. #include "juce_Rectangle.h"
  20. #include "juce_Path.h"
  21. //==============================================================================
  22. /**
  23. Maintains a set of rectangles as a complex region.
  24. This class allows a set of rectangles to be treated as a solid shape, and can
  25. add and remove rectangular sections of it, and simplify overlapping or
  26. adjacent rectangles.
  27. @see Rectangle
  28. */
  29. class JUCE_API RectangleList
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an empty RectangleList */
  34. RectangleList() noexcept;
  35. /** Creates a copy of another list */
  36. RectangleList (const RectangleList& other);
  37. /** Creates a list containing just one rectangle. */
  38. RectangleList (const Rectangle<int>& rect);
  39. /** Copies this list from another one. */
  40. RectangleList& operator= (const RectangleList& other);
  41. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  42. RectangleList (RectangleList&& other) noexcept;
  43. RectangleList& operator= (RectangleList&& other) noexcept;
  44. #endif
  45. /** Destructor. */
  46. ~RectangleList();
  47. //==============================================================================
  48. /** Returns true if the region is empty. */
  49. bool isEmpty() const noexcept;
  50. /** Returns the number of rectangles in the list. */
  51. int getNumRectangles() const noexcept { return rects.size(); }
  52. /** Returns one of the rectangles at a particular index.
  53. @returns the rectangle at the index, or an empty rectangle if the
  54. index is out-of-range.
  55. */
  56. Rectangle<int> getRectangle (int index) const noexcept;
  57. //==============================================================================
  58. /** Removes all rectangles to leave an empty region. */
  59. void clear();
  60. /** Merges a new rectangle into the list.
  61. The rectangle being added will first be clipped to remove any parts of it
  62. that overlap existing rectangles in the list.
  63. */
  64. void add (int x, int y, int width, int height);
  65. /** Merges a new rectangle into the list.
  66. The rectangle being added will first be clipped to remove any parts of it
  67. that overlap existing rectangles in the list, and adjacent rectangles will be
  68. merged into it.
  69. */
  70. void add (const Rectangle<int>& rect);
  71. /** Dumbly adds a rectangle to the list without checking for overlaps.
  72. This simply adds the rectangle to the end, it doesn't merge it or remove
  73. any overlapping bits.
  74. */
  75. void addWithoutMerging (const Rectangle<int>& rect);
  76. /** Merges another rectangle list into this one.
  77. Any overlaps between the two lists will be clipped, so that the result is
  78. the union of both lists.
  79. */
  80. void add (const RectangleList& other);
  81. /** Removes a rectangular region from the list.
  82. Any rectangles in the list which overlap this will be clipped and subdivided
  83. if necessary.
  84. */
  85. void subtract (const Rectangle<int>& rect);
  86. /** Removes all areas in another RectangleList from this one.
  87. Any rectangles in the list which overlap this will be clipped and subdivided
  88. if necessary.
  89. @returns true if the resulting list is non-empty.
  90. */
  91. bool subtract (const RectangleList& otherList);
  92. /** Removes any areas of the region that lie outside a given rectangle.
  93. Any rectangles in the list which overlap this will be clipped and subdivided
  94. if necessary.
  95. Returns true if the resulting region is not empty, false if it is empty.
  96. @see getIntersectionWith
  97. */
  98. bool clipTo (const Rectangle<int>& rect);
  99. /** Removes any areas of the region that lie outside a given rectangle list.
  100. Any rectangles in this object which overlap the specified list will be clipped
  101. and subdivided if necessary.
  102. Returns true if the resulting region is not empty, false if it is empty.
  103. @see getIntersectionWith
  104. */
  105. bool clipTo (const RectangleList& other);
  106. /** Creates a region which is the result of clipping this one to a given rectangle.
  107. Unlike the other clipTo method, this one doesn't affect this object - it puts the
  108. resulting region into the list whose reference is passed-in.
  109. Returns true if the resulting region is not empty, false if it is empty.
  110. @see clipTo
  111. */
  112. bool getIntersectionWith (const Rectangle<int>& rect, RectangleList& destRegion) const;
  113. /** Swaps the contents of this and another list.
  114. This swaps their internal pointers, so is hugely faster than using copy-by-value
  115. to swap them.
  116. */
  117. void swapWith (RectangleList& otherList) noexcept;
  118. //==============================================================================
  119. /** Checks whether the region contains a given point.
  120. @returns true if the point lies within one of the rectangles in the list
  121. */
  122. bool containsPoint (int x, int y) const noexcept;
  123. /** Checks whether the region contains the whole of a given rectangle.
  124. @returns true all parts of the rectangle passed in lie within the region
  125. defined by this object
  126. @see intersectsRectangle, containsPoint
  127. */
  128. bool containsRectangle (const Rectangle<int>& rectangleToCheck) const;
  129. /** Checks whether the region contains any part of a given rectangle.
  130. @returns true if any part of the rectangle passed in lies within the region
  131. defined by this object
  132. @see containsRectangle
  133. */
  134. bool intersectsRectangle (const Rectangle<int>& rectangleToCheck) const noexcept;
  135. /** Checks whether this region intersects any part of another one.
  136. @see intersectsRectangle
  137. */
  138. bool intersects (const RectangleList& other) const noexcept;
  139. //==============================================================================
  140. /** Returns the smallest rectangle that can enclose the whole of this region. */
  141. Rectangle<int> getBounds() const noexcept;
  142. /** Optimises the list into a minimum number of constituent rectangles.
  143. This will try to combine any adjacent rectangles into larger ones where
  144. possible, to simplify lists that might have been fragmented by repeated
  145. add/subtract calls.
  146. */
  147. void consolidate();
  148. /** Adds an x and y value to all the coordinates. */
  149. void offsetAll (int dx, int dy) noexcept;
  150. /** Scales all the coordinates. */
  151. template <typename ScaleType>
  152. void scaleAll (ScaleType scaleFactor) noexcept
  153. {
  154. for (Rectangle<int>* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  155. *r *= scaleFactor;
  156. }
  157. //==============================================================================
  158. /** Creates a Path object to represent this region. */
  159. Path toPath() const;
  160. //==============================================================================
  161. /** Standard method for iterating the rectangles in the list. */
  162. const Rectangle<int>* begin() const noexcept { return rects.begin(); }
  163. /** Standard method for iterating the rectangles in the list. */
  164. const Rectangle<int>* end() const noexcept { return rects.end(); }
  165. //==============================================================================
  166. /** An iterator for accessing all the rectangles in a RectangleList.
  167. Note that this class is deprectated in favour of just using the standard
  168. RectangleList::begin() and RectangleList::end() methods, which are more efficient.
  169. */
  170. class JUCE_API Iterator
  171. {
  172. public:
  173. //==============================================================================
  174. Iterator (const RectangleList& list) noexcept;
  175. ~Iterator();
  176. //==============================================================================
  177. /** Advances to the next rectangle, and returns true if it's not finished.
  178. Call this before using getRectangle() to find the rectangle that was returned.
  179. */
  180. bool next() noexcept;
  181. /** Returns the current rectangle. */
  182. const Rectangle<int>* getRectangle() const noexcept { return current; }
  183. private:
  184. const Rectangle<int>* current;
  185. const RectangleList& owner;
  186. int index;
  187. JUCE_DECLARE_NON_COPYABLE (Iterator)
  188. };
  189. private:
  190. //==============================================================================
  191. friend class Iterator;
  192. Array <Rectangle<int> > rects;
  193. JUCE_LEAK_DETECTOR (RectangleList)
  194. };
  195. #endif // JUCE_RECTANGLELIST_H_INCLUDED