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.

juce_RectanglePlacement.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. RectanglePlacement::RectanglePlacement (const RectanglePlacement& other) noexcept
  18. : flags (other.flags)
  19. {
  20. }
  21. RectanglePlacement& RectanglePlacement::operator= (const RectanglePlacement& other) noexcept
  22. {
  23. flags = other.flags;
  24. return *this;
  25. }
  26. bool RectanglePlacement::operator== (const RectanglePlacement& other) const noexcept
  27. {
  28. return flags == other.flags;
  29. }
  30. bool RectanglePlacement::operator!= (const RectanglePlacement& other) const noexcept
  31. {
  32. return flags != other.flags;
  33. }
  34. void RectanglePlacement::applyTo (double& x, double& y, double& w, double& h,
  35. const double dx, const double dy, const double dw, const double dh) const noexcept
  36. {
  37. if (w == 0 || h == 0)
  38. return;
  39. if ((flags & stretchToFit) != 0)
  40. {
  41. x = dx;
  42. y = dy;
  43. w = dw;
  44. h = dh;
  45. }
  46. else
  47. {
  48. double scale = (flags & fillDestination) != 0 ? jmax (dw / w, dh / h)
  49. : jmin (dw / w, dh / h);
  50. if ((flags & onlyReduceInSize) != 0)
  51. scale = jmin (scale, 1.0);
  52. if ((flags & onlyIncreaseInSize) != 0)
  53. scale = jmax (scale, 1.0);
  54. w *= scale;
  55. h *= scale;
  56. if ((flags & xLeft) != 0)
  57. x = dx;
  58. else if ((flags & xRight) != 0)
  59. x = dx + dw - w;
  60. else
  61. x = dx + (dw - w) * 0.5;
  62. if ((flags & yTop) != 0)
  63. y = dy;
  64. else if ((flags & yBottom) != 0)
  65. y = dy + dh - h;
  66. else
  67. y = dy + (dh - h) * 0.5;
  68. }
  69. }
  70. AffineTransform RectanglePlacement::getTransformToFit (const Rectangle<float>& source, const Rectangle<float>& destination) const noexcept
  71. {
  72. if (source.isEmpty())
  73. return AffineTransform::identity;
  74. float newX = destination.getX();
  75. float newY = destination.getY();
  76. float scaleX = destination.getWidth() / source.getWidth();
  77. float scaleY = destination.getHeight() / source.getHeight();
  78. if ((flags & stretchToFit) == 0)
  79. {
  80. scaleX = (flags & fillDestination) != 0 ? jmax (scaleX, scaleY)
  81. : jmin (scaleX, scaleY);
  82. if ((flags & onlyReduceInSize) != 0)
  83. scaleX = jmin (scaleX, 1.0f);
  84. if ((flags & onlyIncreaseInSize) != 0)
  85. scaleX = jmax (scaleX, 1.0f);
  86. scaleY = scaleX;
  87. if ((flags & xRight) != 0)
  88. newX += destination.getWidth() - source.getWidth() * scaleX; // right
  89. else if ((flags & xLeft) == 0)
  90. newX += (destination.getWidth() - source.getWidth() * scaleX) / 2.0f; // centre
  91. if ((flags & yBottom) != 0)
  92. newY += destination.getHeight() - source.getHeight() * scaleX; // bottom
  93. else if ((flags & yTop) == 0)
  94. newY += (destination.getHeight() - source.getHeight() * scaleX) / 2.0f; // centre
  95. }
  96. return AffineTransform::translation (-source.getX(), -source.getY())
  97. .scaled (scaleX, scaleY)
  98. .translated (newX, newY);
  99. }