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.

168 lines
6.3KB

  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. Specifies a set of gaps to be left around the sides of a rectangle.
  23. This is basically the size of the spaces at the top, bottom, left and right of
  24. a rectangle. It's used by various component classes to specify borders.
  25. @see Rectangle
  26. @tags{Graphics}
  27. */
  28. template <typename ValueType>
  29. class BorderSize
  30. {
  31. auto tie() const { return std::tie (top, left, bottom, right); }
  32. public:
  33. //==============================================================================
  34. /** Creates a null border.
  35. All sizes are left as 0.
  36. */
  37. BorderSize() = default;
  38. /** Creates a border with the given gaps. */
  39. BorderSize (ValueType topGap, ValueType leftGap, ValueType bottomGap, ValueType rightGap) noexcept
  40. : top (topGap), left (leftGap), bottom (bottomGap), right (rightGap)
  41. {
  42. }
  43. /** Creates a border with the given gap on all sides. */
  44. explicit BorderSize (ValueType allGaps) noexcept
  45. : top (allGaps), left (allGaps), bottom (allGaps), right (allGaps)
  46. {
  47. }
  48. //==============================================================================
  49. /** Returns the gap that should be left at the top of the region. */
  50. ValueType getTop() const noexcept { return top; }
  51. /** Returns the gap that should be left at the left of the region. */
  52. ValueType getLeft() const noexcept { return left; }
  53. /** Returns the gap that should be left at the bottom of the region. */
  54. ValueType getBottom() const noexcept { return bottom; }
  55. /** Returns the gap that should be left at the right of the region. */
  56. ValueType getRight() const noexcept { return right; }
  57. /** Returns the sum of the top and bottom gaps. */
  58. ValueType getTopAndBottom() const noexcept { return top + bottom; }
  59. /** Returns the sum of the left and right gaps. */
  60. ValueType getLeftAndRight() const noexcept { return left + right; }
  61. /** Returns true if this border has no thickness along any edge. */
  62. bool isEmpty() const noexcept { return left + right + top + bottom == ValueType(); }
  63. //==============================================================================
  64. /** Changes the top gap. */
  65. void setTop (ValueType newTopGap) noexcept { top = newTopGap; }
  66. /** Changes the left gap. */
  67. void setLeft (ValueType newLeftGap) noexcept { left = newLeftGap; }
  68. /** Changes the bottom gap. */
  69. void setBottom (ValueType newBottomGap) noexcept { bottom = newBottomGap; }
  70. /** Changes the right gap. */
  71. void setRight (ValueType newRightGap) noexcept { right = newRightGap; }
  72. //==============================================================================
  73. /** Returns a rectangle with these borders removed from it. */
  74. Rectangle<ValueType> subtractedFrom (const Rectangle<ValueType>& original) const noexcept
  75. {
  76. return { original.getX() + left,
  77. original.getY() + top,
  78. original.getWidth() - (left + right),
  79. original.getHeight() - (top + bottom) };
  80. }
  81. /** Removes this border from a given rectangle. */
  82. void subtractFrom (Rectangle<ValueType>& rectangle) const noexcept
  83. {
  84. rectangle = subtractedFrom (rectangle);
  85. }
  86. /** Returns a rectangle with these borders added around it. */
  87. Rectangle<ValueType> addedTo (const Rectangle<ValueType>& original) const noexcept
  88. {
  89. return { original.getX() - left,
  90. original.getY() - top,
  91. original.getWidth() + (left + right),
  92. original.getHeight() + (top + bottom) };
  93. }
  94. /** Adds this border around a given rectangle. */
  95. void addTo (Rectangle<ValueType>& rectangle) const noexcept
  96. {
  97. rectangle = addedTo (rectangle);
  98. }
  99. /** Removes this border from another border. */
  100. BorderSize<ValueType> subtractedFrom (const BorderSize<ValueType>& other) const noexcept
  101. {
  102. return { other.top - top,
  103. other.left - left,
  104. other.bottom - bottom,
  105. other.right - right };
  106. }
  107. /** Adds this border to another border. */
  108. BorderSize<ValueType> addedTo (const BorderSize<ValueType>& other) const noexcept
  109. {
  110. return { other.top + top,
  111. other.left + left,
  112. other.bottom + bottom,
  113. other.right + right };
  114. }
  115. /** Multiplies each member of the border by a scalar. */
  116. template <typename ScalarType>
  117. BorderSize<ValueType> multipliedBy (ScalarType scalar) const noexcept
  118. {
  119. return { static_cast<ValueType> (scalar * top),
  120. static_cast<ValueType> (scalar * left),
  121. static_cast<ValueType> (scalar * bottom),
  122. static_cast<ValueType> (scalar * right) };
  123. }
  124. //==============================================================================
  125. bool operator== (const BorderSize& other) const noexcept { return tie() == other.tie(); }
  126. bool operator!= (const BorderSize& other) const noexcept { return tie() != other.tie(); }
  127. private:
  128. //==============================================================================
  129. ValueType top{}, left{}, bottom{}, right{};
  130. };
  131. } // namespace juce