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.

168 lines
7.5KB

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