Audio plugin host https://kx.studio/carla
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.

170 lines
7.7KB

  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_RECTANGLEPLACEMENT_H_INCLUDED
  18. #define JUCE_RECTANGLEPLACEMENT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Defines the method used to postion some kind of rectangular object within
  22. a rectangular viewport.
  23. Although similar to Justification, this is more specific, and has some extra
  24. options.
  25. */
  26. class JUCE_API RectanglePlacement
  27. {
  28. public:
  29. //==============================================================================
  30. /** Creates a RectanglePlacement object using a combination of flags from the Flags enum. */
  31. inline RectanglePlacement (int placementFlags) noexcept : flags (placementFlags) {}
  32. /** Creates a copy of another RectanglePlacement object. */
  33. RectanglePlacement (const RectanglePlacement& other) noexcept;
  34. /** Copies another RectanglePlacement object. */
  35. RectanglePlacement& operator= (const RectanglePlacement& other) noexcept;
  36. bool operator== (const RectanglePlacement& other) const noexcept;
  37. bool operator!= (const RectanglePlacement& other) const noexcept;
  38. //==============================================================================
  39. /** Flag values that can be combined and used in the constructor. */
  40. enum Flags
  41. {
  42. //==============================================================================
  43. /** Indicates that the source rectangle's left edge should be aligned with the left edge of the target rectangle. */
  44. xLeft = 1,
  45. /** Indicates that the source rectangle's right edge should be aligned with the right edge of the target rectangle. */
  46. xRight = 2,
  47. /** Indicates that the source should be placed in the centre between the left and right
  48. sides of the available space. */
  49. xMid = 4,
  50. //==============================================================================
  51. /** Indicates that the source's top edge should be aligned with the top edge of the
  52. destination rectangle. */
  53. yTop = 8,
  54. /** Indicates that the source's bottom edge should be aligned with the bottom edge of the
  55. destination rectangle. */
  56. yBottom = 16,
  57. /** Indicates that the source should be placed in the centre between the top and bottom
  58. sides of the available space. */
  59. yMid = 32,
  60. //==============================================================================
  61. /** If this flag is set, then the source rectangle will be resized to completely fill
  62. the destination rectangle, and all other flags are ignored.
  63. */
  64. stretchToFit = 64,
  65. //==============================================================================
  66. /** If this flag is set, then the source rectangle will be resized so that it is the
  67. minimum size to completely fill the destination rectangle, without changing its
  68. aspect ratio. This means that some of the source rectangle may fall outside
  69. the destination.
  70. If this flag is not set, the source will be given the maximum size at which none
  71. of it falls outside the destination rectangle.
  72. */
  73. fillDestination = 128,
  74. /** Indicates that the source rectangle can be reduced in size if required, but should
  75. never be made larger than its original size.
  76. */
  77. onlyReduceInSize = 256,
  78. /** Indicates that the source rectangle can be enlarged if required, but should
  79. never be made smaller than its original size.
  80. */
  81. onlyIncreaseInSize = 512,
  82. /** Indicates that the source rectangle's size should be left unchanged.
  83. */
  84. doNotResize = (onlyIncreaseInSize | onlyReduceInSize),
  85. //==============================================================================
  86. /** A shorthand value that is equivalent to (xMid | yMid). */
  87. centred = 4 + 32
  88. };
  89. //==============================================================================
  90. /** Returns the raw flags that are set for this object. */
  91. inline int getFlags() const noexcept { return flags; }
  92. /** Tests a set of flags for this object.
  93. @returns true if any of the flags passed in are set on this object.
  94. */
  95. inline bool testFlags (int flagsToTest) const noexcept { return (flags & flagsToTest) != 0; }
  96. //==============================================================================
  97. /** Adjusts the position and size of a rectangle to fit it into a space.
  98. The source rectangle coordinates will be adjusted so that they fit into
  99. the destination rectangle based on this object's flags.
  100. */
  101. void applyTo (double& sourceX,
  102. double& sourceY,
  103. double& sourceW,
  104. double& sourceH,
  105. double destinationX,
  106. double destinationY,
  107. double destinationW,
  108. double destinationH) const noexcept;
  109. /** Returns the rectangle that should be used to fit the given source rectangle
  110. into the destination rectangle using the current flags.
  111. */
  112. template <typename ValueType>
  113. Rectangle<ValueType> appliedTo (const Rectangle<ValueType>& source,
  114. const Rectangle<ValueType>& destination) const noexcept
  115. {
  116. double x = source.getX(), y = source.getY(), w = source.getWidth(), h = source.getHeight();
  117. applyTo (x, y, w, h, static_cast <double> (destination.getX()), static_cast <double> (destination.getY()),
  118. static_cast <double> (destination.getWidth()), static_cast <double> (destination.getHeight()));
  119. return Rectangle<ValueType> (static_cast <ValueType> (x), static_cast <ValueType> (y),
  120. static_cast <ValueType> (w), static_cast <ValueType> (h));
  121. }
  122. /** Returns the transform that should be applied to these source coordinates to fit them
  123. into the destination rectangle using the current flags.
  124. */
  125. AffineTransform getTransformToFit (const Rectangle<float>& source,
  126. const Rectangle<float>& destination) const noexcept;
  127. private:
  128. //==============================================================================
  129. int flags;
  130. };
  131. #endif // JUCE_RECTANGLEPLACEMENT_H_INCLUDED