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.

130 lines
4.0KB

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