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.

135 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. RectanglePlacement::RectanglePlacement (const RectanglePlacement& other) noexcept
  22. : flags (other.flags)
  23. {
  24. }
  25. RectanglePlacement& RectanglePlacement::operator= (const RectanglePlacement& other) noexcept
  26. {
  27. flags = other.flags;
  28. return *this;
  29. }
  30. bool RectanglePlacement::operator== (const RectanglePlacement& other) const noexcept
  31. {
  32. return flags == other.flags;
  33. }
  34. bool RectanglePlacement::operator!= (const RectanglePlacement& other) const noexcept
  35. {
  36. return flags != other.flags;
  37. }
  38. void RectanglePlacement::applyTo (double& x, double& y, double& w, double& h,
  39. const double dx, const double dy, const double dw, const double dh) const noexcept
  40. {
  41. if (w == 0.0 || h == 0.0)
  42. return;
  43. if ((flags & stretchToFit) != 0)
  44. {
  45. x = dx;
  46. y = dy;
  47. w = dw;
  48. h = dh;
  49. }
  50. else
  51. {
  52. double scale = (flags & fillDestination) != 0 ? jmax (dw / w, dh / h)
  53. : jmin (dw / w, dh / h);
  54. if ((flags & onlyReduceInSize) != 0)
  55. scale = jmin (scale, 1.0);
  56. if ((flags & onlyIncreaseInSize) != 0)
  57. scale = jmax (scale, 1.0);
  58. w *= scale;
  59. h *= scale;
  60. if ((flags & xLeft) != 0)
  61. x = dx;
  62. else if ((flags & xRight) != 0)
  63. x = dx + dw - w;
  64. else
  65. x = dx + (dw - w) * 0.5;
  66. if ((flags & yTop) != 0)
  67. y = dy;
  68. else if ((flags & yBottom) != 0)
  69. y = dy + dh - h;
  70. else
  71. y = dy + (dh - h) * 0.5;
  72. }
  73. }
  74. AffineTransform RectanglePlacement::getTransformToFit (const Rectangle<float>& source, const Rectangle<float>& destination) const noexcept
  75. {
  76. if (source.isEmpty())
  77. return AffineTransform();
  78. float newX = destination.getX();
  79. float newY = destination.getY();
  80. float scaleX = destination.getWidth() / source.getWidth();
  81. float scaleY = destination.getHeight() / source.getHeight();
  82. if ((flags & stretchToFit) == 0)
  83. {
  84. scaleX = (flags & fillDestination) != 0 ? jmax (scaleX, scaleY)
  85. : jmin (scaleX, scaleY);
  86. if ((flags & onlyReduceInSize) != 0)
  87. scaleX = jmin (scaleX, 1.0f);
  88. if ((flags & onlyIncreaseInSize) != 0)
  89. scaleX = jmax (scaleX, 1.0f);
  90. scaleY = scaleX;
  91. if ((flags & xRight) != 0)
  92. newX += destination.getWidth() - source.getWidth() * scaleX; // right
  93. else if ((flags & xLeft) == 0)
  94. newX += (destination.getWidth() - source.getWidth() * scaleX) / 2.0f; // centre
  95. if ((flags & yBottom) != 0)
  96. newY += destination.getHeight() - source.getHeight() * scaleX; // bottom
  97. else if ((flags & yTop) == 0)
  98. newY += (destination.getHeight() - source.getHeight() * scaleX) / 2.0f; // centre
  99. }
  100. return AffineTransform::translation (-source.getX(), -source.getY())
  101. .scaled (scaleX, scaleY)
  102. .translated (newX, newY);
  103. }
  104. } // namespace juce