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.

124 lines
3.8KB

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