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 3.8KB

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