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.

257 lines
9.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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_RECTANGLELIST_JUCEHEADER__
  19. #define __JUCE_RECTANGLELIST_JUCEHEADER__
  20. #include "juce_Rectangle.h"
  21. #include "juce_Path.h"
  22. //==============================================================================
  23. /**
  24. Maintains a set of rectangles as a complex region.
  25. This class allows a set of rectangles to be treated as a solid shape, and can
  26. add and remove rectangular sections of it, and simplify overlapping or
  27. adjacent rectangles.
  28. @see Rectangle
  29. */
  30. class JUCE_API RectangleList
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates an empty RectangleList */
  35. RectangleList() noexcept;
  36. /** Creates a copy of another list */
  37. RectangleList (const RectangleList& other);
  38. /** Creates a list containing just one rectangle. */
  39. RectangleList (const Rectangle<int>& rect);
  40. /** Copies this list from another one. */
  41. RectangleList& operator= (const RectangleList& other);
  42. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  43. RectangleList (RectangleList&& other) noexcept;
  44. RectangleList& operator= (RectangleList&& other) noexcept;
  45. #endif
  46. /** Destructor. */
  47. ~RectangleList();
  48. //==============================================================================
  49. /** Returns true if the region is empty. */
  50. bool isEmpty() const noexcept;
  51. /** Returns the number of rectangles in the list. */
  52. int getNumRectangles() const noexcept { return rects.size(); }
  53. /** Returns one of the rectangles at a particular index.
  54. @returns the rectangle at the index, or an empty rectangle if the
  55. index is out-of-range.
  56. */
  57. Rectangle<int> getRectangle (int index) const noexcept;
  58. //==============================================================================
  59. /** Removes all rectangles to leave an empty region. */
  60. void clear();
  61. /** Merges a new rectangle into the list.
  62. The rectangle being added will first be clipped to remove any parts of it
  63. that overlap existing rectangles in the list.
  64. */
  65. void add (int x, int y, int width, int height);
  66. /** Merges a new rectangle into the list.
  67. The rectangle being added will first be clipped to remove any parts of it
  68. that overlap existing rectangles in the list, and adjacent rectangles will be
  69. merged into it.
  70. */
  71. void add (const Rectangle<int>& rect);
  72. /** Dumbly adds a rectangle to the list without checking for overlaps.
  73. This simply adds the rectangle to the end, it doesn't merge it or remove
  74. any overlapping bits.
  75. */
  76. void addWithoutMerging (const Rectangle<int>& rect);
  77. /** Merges another rectangle list into this one.
  78. Any overlaps between the two lists will be clipped, so that the result is
  79. the union of both lists.
  80. */
  81. void add (const RectangleList& other);
  82. /** Removes a rectangular region from the list.
  83. Any rectangles in the list which overlap this will be clipped and subdivided
  84. if necessary.
  85. */
  86. void subtract (const Rectangle<int>& rect);
  87. /** Removes all areas in another RectangleList from this one.
  88. Any rectangles in the list which overlap this will be clipped and subdivided
  89. if necessary.
  90. @returns true if the resulting list is non-empty.
  91. */
  92. bool subtract (const RectangleList& otherList);
  93. /** Removes any areas of the region that lie outside a given rectangle.
  94. Any rectangles in the list which overlap this will be clipped and subdivided
  95. if necessary.
  96. Returns true if the resulting region is not empty, false if it is empty.
  97. @see getIntersectionWith
  98. */
  99. bool clipTo (const Rectangle<int>& rect);
  100. /** Removes any areas of the region that lie outside a given rectangle list.
  101. Any rectangles in this object which overlap the specified list will be clipped
  102. and subdivided if necessary.
  103. Returns true if the resulting region is not empty, false if it is empty.
  104. @see getIntersectionWith
  105. */
  106. bool clipTo (const RectangleList& other);
  107. /** Creates a region which is the result of clipping this one to a given rectangle.
  108. Unlike the other clipTo method, this one doesn't affect this object - it puts the
  109. resulting region into the list whose reference is passed-in.
  110. Returns true if the resulting region is not empty, false if it is empty.
  111. @see clipTo
  112. */
  113. bool getIntersectionWith (const Rectangle<int>& rect, RectangleList& destRegion) const;
  114. /** Swaps the contents of this and another list.
  115. This swaps their internal pointers, so is hugely faster than using copy-by-value
  116. to swap them.
  117. */
  118. void swapWith (RectangleList& otherList) noexcept;
  119. //==============================================================================
  120. /** Checks whether the region contains a given point.
  121. @returns true if the point lies within one of the rectangles in the list
  122. */
  123. bool containsPoint (int x, int y) const noexcept;
  124. /** Checks whether the region contains the whole of a given rectangle.
  125. @returns true all parts of the rectangle passed in lie within the region
  126. defined by this object
  127. @see intersectsRectangle, containsPoint
  128. */
  129. bool containsRectangle (const Rectangle<int>& rectangleToCheck) const;
  130. /** Checks whether the region contains any part of a given rectangle.
  131. @returns true if any part of the rectangle passed in lies within the region
  132. defined by this object
  133. @see containsRectangle
  134. */
  135. bool intersectsRectangle (const Rectangle<int>& rectangleToCheck) const noexcept;
  136. /** Checks whether this region intersects any part of another one.
  137. @see intersectsRectangle
  138. */
  139. bool intersects (const RectangleList& other) const noexcept;
  140. //==============================================================================
  141. /** Returns the smallest rectangle that can enclose the whole of this region. */
  142. Rectangle<int> getBounds() const noexcept;
  143. /** Optimises the list into a minimum number of constituent rectangles.
  144. This will try to combine any adjacent rectangles into larger ones where
  145. possible, to simplify lists that might have been fragmented by repeated
  146. add/subtract calls.
  147. */
  148. void consolidate();
  149. /** Adds an x and y value to all the co-ordinates. */
  150. void offsetAll (int dx, int dy) noexcept;
  151. //==============================================================================
  152. /** Creates a Path object to represent this region. */
  153. Path toPath() const;
  154. //==============================================================================
  155. /** An iterator for accessing all the rectangles in a RectangleList. */
  156. class JUCE_API Iterator
  157. {
  158. public:
  159. //==============================================================================
  160. Iterator (const RectangleList& list) noexcept;
  161. ~Iterator();
  162. //==============================================================================
  163. /** Advances to the next rectangle, and returns true if it's not finished.
  164. Call this before using getRectangle() to find the rectangle that was returned.
  165. */
  166. bool next() noexcept;
  167. /** Returns the current rectangle. */
  168. const Rectangle<int>* getRectangle() const noexcept { return current; }
  169. private:
  170. const Rectangle<int>* current;
  171. const RectangleList& owner;
  172. int index;
  173. JUCE_DECLARE_NON_COPYABLE (Iterator);
  174. };
  175. private:
  176. //==============================================================================
  177. friend class Iterator;
  178. Array <Rectangle<int> > rects;
  179. JUCE_LEAK_DETECTOR (RectangleList);
  180. };
  181. #endif // __JUCE_RECTANGLELIST_JUCEHEADER__