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.

173 lines
7.9KB

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