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.

188 lines
8.1KB

  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. Represents a type of justification to be used when positioning graphical items.
  23. e.g. it indicates whether something should be placed top-left, top-right,
  24. centred, etc.
  25. It is used in various places wherever this kind of information is needed.
  26. @tags{Graphics}
  27. */
  28. class Justification
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a Justification object using a combination of flags from the Flags enum. */
  33. Justification (int justificationFlags) noexcept : flags (justificationFlags) {}
  34. /** Creates a copy of another Justification object. */
  35. Justification (const Justification&) = default;
  36. /** Copies another Justification object. */
  37. Justification& operator= (const Justification&) = default;
  38. bool operator== (const Justification& other) const noexcept { return flags == other.flags; }
  39. bool operator!= (const Justification& other) const noexcept { return flags != other.flags; }
  40. //==============================================================================
  41. /** Returns the raw flags that are set for this Justification object. */
  42. inline int getFlags() const noexcept { return flags; }
  43. /** Tests a set of flags for this object.
  44. @returns true if any of the flags passed in are set on this object.
  45. */
  46. inline bool testFlags (int flagsToTest) const noexcept { return (flags & flagsToTest) != 0; }
  47. /** Returns just the flags from this object that deal with vertical layout. */
  48. int getOnlyVerticalFlags() const noexcept { return flags & (top | bottom | verticallyCentred); }
  49. /** Returns just the flags from this object that deal with horizontal layout. */
  50. int getOnlyHorizontalFlags() const noexcept { return flags & (left | right | horizontallyCentred | horizontallyJustified); }
  51. //==============================================================================
  52. /** Adjusts the position of a rectangle to fit it into a space.
  53. The (x, y) position of the rectangle will be updated to position it inside the
  54. given space according to the justification flags.
  55. */
  56. template <typename ValueType>
  57. void applyToRectangle (ValueType& x, ValueType& y, ValueType w, ValueType h,
  58. ValueType spaceX, ValueType spaceY, ValueType spaceW, ValueType spaceH) const noexcept
  59. {
  60. x = spaceX;
  61. if ((flags & horizontallyCentred) != 0) x += (spaceW - w) / (ValueType) 2;
  62. else if ((flags & right) != 0) x += spaceW - w;
  63. y = spaceY;
  64. if ((flags & verticallyCentred) != 0) y += (spaceH - h) / (ValueType) 2;
  65. else if ((flags & bottom) != 0) y += spaceH - h;
  66. }
  67. /** Returns the new position of a rectangle that has been justified to fit within a given space.
  68. */
  69. template <typename ValueType>
  70. const Rectangle<ValueType> appliedToRectangle (const Rectangle<ValueType>& areaToAdjust,
  71. const Rectangle<ValueType>& targetSpace) const noexcept
  72. {
  73. ValueType x = areaToAdjust.getX(), y = areaToAdjust.getY();
  74. applyToRectangle (x, y, areaToAdjust.getWidth(), areaToAdjust.getHeight(),
  75. targetSpace.getX(), targetSpace.getY(), targetSpace.getWidth(), targetSpace.getHeight());
  76. return areaToAdjust.withPosition (x, y);
  77. }
  78. //==============================================================================
  79. /** Flag values that can be combined and used in the constructor. */
  80. enum Flags
  81. {
  82. //==============================================================================
  83. /** Indicates that the item should be aligned against the left edge of the available space. */
  84. left = 1,
  85. /** Indicates that the item should be aligned against the right edge of the available space. */
  86. right = 2,
  87. /** Indicates that the item should be placed in the centre between the left and right
  88. sides of the available space. */
  89. horizontallyCentred = 4,
  90. //==============================================================================
  91. /** Indicates that the item should be aligned against the top edge of the available space. */
  92. top = 8,
  93. /** Indicates that the item should be aligned against the bottom edge of the available space. */
  94. bottom = 16,
  95. /** Indicates that the item should be placed in the centre between the top and bottom
  96. sides of the available space. */
  97. verticallyCentred = 32,
  98. //==============================================================================
  99. /** Indicates that lines of text should be spread out to fill the maximum width
  100. available, so that both margins are aligned vertically.
  101. */
  102. horizontallyJustified = 64,
  103. //==============================================================================
  104. /** Indicates that the item should be centred vertically and horizontally.
  105. This is equivalent to (horizontallyCentred | verticallyCentred)
  106. */
  107. centred = 36,
  108. /** Indicates that the item should be centred vertically but placed on the left hand side.
  109. This is equivalent to (left | verticallyCentred)
  110. */
  111. centredLeft = 33,
  112. /** Indicates that the item should be centred vertically but placed on the right hand side.
  113. This is equivalent to (right | verticallyCentred)
  114. */
  115. centredRight = 34,
  116. /** Indicates that the item should be centred horizontally and placed at the top.
  117. This is equivalent to (horizontallyCentred | top)
  118. */
  119. centredTop = 12,
  120. /** Indicates that the item should be centred horizontally and placed at the bottom.
  121. This is equivalent to (horizontallyCentred | bottom)
  122. */
  123. centredBottom = 20,
  124. /** Indicates that the item should be placed in the top-left corner.
  125. This is equivalent to (left | top)
  126. */
  127. topLeft = 9,
  128. /** Indicates that the item should be placed in the top-right corner.
  129. This is equivalent to (right | top)
  130. */
  131. topRight = 10,
  132. /** Indicates that the item should be placed in the bottom-left corner.
  133. This is equivalent to (left | bottom)
  134. */
  135. bottomLeft = 17,
  136. /** Indicates that the item should be placed in the bottom-left corner.
  137. This is equivalent to (right | bottom)
  138. */
  139. bottomRight = 18
  140. };
  141. private:
  142. //==============================================================================
  143. int flags;
  144. };
  145. } // namespace juce