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.

129 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. RectanglePlacement::RectanglePlacement (const RectanglePlacement& other) noexcept
  19. : flags (other.flags)
  20. {
  21. }
  22. RectanglePlacement& RectanglePlacement::operator= (const RectanglePlacement& other) noexcept
  23. {
  24. flags = other.flags;
  25. return *this;
  26. }
  27. bool RectanglePlacement::operator== (const RectanglePlacement& other) const noexcept
  28. {
  29. return flags == other.flags;
  30. }
  31. bool RectanglePlacement::operator!= (const RectanglePlacement& other) const noexcept
  32. {
  33. return flags != other.flags;
  34. }
  35. void RectanglePlacement::applyTo (double& x, double& y, double& w, double& h,
  36. const double dx, const double dy, const double dw, const double dh) const noexcept
  37. {
  38. if (w == 0 || h == 0)
  39. return;
  40. if ((flags & stretchToFit) != 0)
  41. {
  42. x = dx;
  43. y = dy;
  44. w = dw;
  45. h = dh;
  46. }
  47. else
  48. {
  49. double scale = (flags & fillDestination) != 0 ? jmax (dw / w, dh / h)
  50. : jmin (dw / w, dh / h);
  51. if ((flags & onlyReduceInSize) != 0)
  52. scale = jmin (scale, 1.0);
  53. if ((flags & onlyIncreaseInSize) != 0)
  54. scale = jmax (scale, 1.0);
  55. w *= scale;
  56. h *= scale;
  57. if ((flags & xLeft) != 0)
  58. x = dx;
  59. else if ((flags & xRight) != 0)
  60. x = dx + dw - w;
  61. else
  62. x = dx + (dw - w) * 0.5;
  63. if ((flags & yTop) != 0)
  64. y = dy;
  65. else if ((flags & yBottom) != 0)
  66. y = dy + dh - h;
  67. else
  68. y = dy + (dh - h) * 0.5;
  69. }
  70. }
  71. const AffineTransform RectanglePlacement::getTransformToFit (const Rectangle<float>& source, const Rectangle<float>& destination) const noexcept
  72. {
  73. if (source.isEmpty())
  74. return AffineTransform::identity;
  75. float newX = destination.getX();
  76. float newY = destination.getY();
  77. float scaleX = destination.getWidth() / source.getWidth();
  78. float scaleY = destination.getHeight() / source.getHeight();
  79. if ((flags & stretchToFit) == 0)
  80. {
  81. scaleX = (flags & fillDestination) != 0 ? jmax (scaleX, scaleY)
  82. : jmin (scaleX, scaleY);
  83. if ((flags & onlyReduceInSize) != 0)
  84. scaleX = jmin (scaleX, 1.0f);
  85. if ((flags & onlyIncreaseInSize) != 0)
  86. scaleX = jmax (scaleX, 1.0f);
  87. scaleY = scaleX;
  88. if ((flags & xRight) != 0)
  89. newX += destination.getWidth() - source.getWidth() * scaleX; // right
  90. else if ((flags & xLeft) == 0)
  91. newX += (destination.getWidth() - source.getWidth() * scaleX) / 2.0f; // centre
  92. if ((flags & yBottom) != 0)
  93. newY += destination.getHeight() - source.getHeight() * scaleX; // bottom
  94. else if ((flags & yTop) == 0)
  95. newY += (destination.getHeight() - source.getHeight() * scaleX) / 2.0f; // centre
  96. }
  97. return AffineTransform::translation (-source.getX(), -source.getY())
  98. .scaled (scaleX, scaleY)
  99. .translated (newX, newY);
  100. }