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.

175 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Defines the method used to position some kind of rectangular object within
  23. a rectangular viewport.
  24. Although similar to Justification, this is more specific, and has some extra
  25. options.
  26. @tags{Graphics}
  27. */
  28. class JUCE_API RectanglePlacement
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a RectanglePlacement object using a combination of flags from the Flags enum. */
  33. inline RectanglePlacement (int placementFlags) noexcept : flags (placementFlags) {}
  34. /** Creates a default RectanglePlacement object, which is equivalent to using the 'centred' flag. */
  35. inline RectanglePlacement() = default;
  36. /** Creates a copy of another RectanglePlacement object. */
  37. RectanglePlacement (const RectanglePlacement&) = default;
  38. /** Copies another RectanglePlacement object. */
  39. RectanglePlacement& operator= (const RectanglePlacement&) = default;
  40. bool operator== (const RectanglePlacement&) const noexcept;
  41. bool operator!= (const RectanglePlacement&) const noexcept;
  42. //==============================================================================
  43. /** Flag values that can be combined and used in the constructor. */
  44. enum Flags
  45. {
  46. //==============================================================================
  47. /** Indicates that the source rectangle's left edge should be aligned with the left edge of the target rectangle. */
  48. xLeft = 1,
  49. /** Indicates that the source rectangle's right edge should be aligned with the right edge of the target rectangle. */
  50. xRight = 2,
  51. /** Indicates that the source should be placed in the centre between the left and right
  52. sides of the available space. */
  53. xMid = 4,
  54. //==============================================================================
  55. /** Indicates that the source's top edge should be aligned with the top edge of the
  56. destination rectangle. */
  57. yTop = 8,
  58. /** Indicates that the source's bottom edge should be aligned with the bottom edge of the
  59. destination rectangle. */
  60. yBottom = 16,
  61. /** Indicates that the source should be placed in the centre between the top and bottom
  62. sides of the available space. */
  63. yMid = 32,
  64. //==============================================================================
  65. /** If this flag is set, then the source rectangle will be resized to completely fill
  66. the destination rectangle, and all other flags are ignored.
  67. */
  68. stretchToFit = 64,
  69. //==============================================================================
  70. /** If this flag is set, then the source rectangle will be resized so that it is the
  71. minimum size to completely fill the destination rectangle, without changing its
  72. aspect ratio. This means that some of the source rectangle may fall outside
  73. the destination.
  74. If this flag is not set, the source will be given the maximum size at which none
  75. of it falls outside the destination rectangle.
  76. */
  77. fillDestination = 128,
  78. /** Indicates that the source rectangle can be reduced in size if required, but should
  79. never be made larger than its original size.
  80. */
  81. onlyReduceInSize = 256,
  82. /** Indicates that the source rectangle can be enlarged if required, but should
  83. never be made smaller than its original size.
  84. */
  85. onlyIncreaseInSize = 512,
  86. /** Indicates that the source rectangle's size should be left unchanged.
  87. */
  88. doNotResize = (onlyIncreaseInSize | onlyReduceInSize),
  89. //==============================================================================
  90. /** A shorthand value that is equivalent to (xMid | yMid). */
  91. centred = 4 + 32
  92. };
  93. //==============================================================================
  94. /** Returns the raw flags that are set for this object. */
  95. inline int getFlags() const noexcept { return flags; }
  96. /** Tests a set of flags for this object.
  97. @returns true if any of the flags passed in are set on this object.
  98. */
  99. inline bool testFlags (int flagsToTest) const noexcept { return (flags & flagsToTest) != 0; }
  100. //==============================================================================
  101. /** Adjusts the position and size of a rectangle to fit it into a space.
  102. The source rectangle coordinates will be adjusted so that they fit into
  103. the destination rectangle based on this object's flags.
  104. */
  105. void applyTo (double& sourceX,
  106. double& sourceY,
  107. double& sourceW,
  108. double& sourceH,
  109. double destinationX,
  110. double destinationY,
  111. double destinationW,
  112. double destinationH) const noexcept;
  113. /** Returns the rectangle that should be used to fit the given source rectangle
  114. into the destination rectangle using the current flags.
  115. */
  116. template <typename ValueType>
  117. Rectangle<ValueType> appliedTo (const Rectangle<ValueType>& source,
  118. const Rectangle<ValueType>& destination) const noexcept
  119. {
  120. double x = source.getX(), y = source.getY(), w = source.getWidth(), h = source.getHeight();
  121. applyTo (x, y, w, h, static_cast<double> (destination.getX()), static_cast<double> (destination.getY()),
  122. static_cast<double> (destination.getWidth()), static_cast<double> (destination.getHeight()));
  123. return Rectangle<ValueType> (static_cast<ValueType> (x), static_cast<ValueType> (y),
  124. static_cast<ValueType> (w), static_cast<ValueType> (h));
  125. }
  126. /** Returns the transform that should be applied to these source coordinates to fit them
  127. into the destination rectangle using the current flags.
  128. */
  129. AffineTransform getTransformToFit (const Rectangle<float>& source,
  130. const Rectangle<float>& destination) const noexcept;
  131. private:
  132. //==============================================================================
  133. int flags { centred };
  134. };
  135. } // namespace juce