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.

170 lines
7.8KB

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