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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. bool RectanglePlacement::operator== (const RectanglePlacement& other) const noexcept
  16. {
  17. return flags == other.flags;
  18. }
  19. bool RectanglePlacement::operator!= (const RectanglePlacement& other) const noexcept
  20. {
  21. return flags != other.flags;
  22. }
  23. void RectanglePlacement::applyTo (double& x, double& y, double& w, double& h,
  24. const double dx, const double dy, const double dw, const double dh) const noexcept
  25. {
  26. if (w == 0.0 || h == 0.0)
  27. return;
  28. if ((flags & stretchToFit) != 0)
  29. {
  30. x = dx;
  31. y = dy;
  32. w = dw;
  33. h = dh;
  34. }
  35. else
  36. {
  37. double scale = (flags & fillDestination) != 0 ? jmax (dw / w, dh / h)
  38. : jmin (dw / w, dh / h);
  39. if ((flags & onlyReduceInSize) != 0)
  40. scale = jmin (scale, 1.0);
  41. if ((flags & onlyIncreaseInSize) != 0)
  42. scale = jmax (scale, 1.0);
  43. w *= scale;
  44. h *= scale;
  45. if ((flags & xLeft) != 0)
  46. x = dx;
  47. else if ((flags & xRight) != 0)
  48. x = dx + dw - w;
  49. else
  50. x = dx + (dw - w) * 0.5;
  51. if ((flags & yTop) != 0)
  52. y = dy;
  53. else if ((flags & yBottom) != 0)
  54. y = dy + dh - h;
  55. else
  56. y = dy + (dh - h) * 0.5;
  57. }
  58. }
  59. AffineTransform RectanglePlacement::getTransformToFit (const Rectangle<float>& source, const Rectangle<float>& destination) const noexcept
  60. {
  61. if (source.isEmpty())
  62. return AffineTransform();
  63. float newX = destination.getX();
  64. float newY = destination.getY();
  65. float scaleX = destination.getWidth() / source.getWidth();
  66. float scaleY = destination.getHeight() / source.getHeight();
  67. if ((flags & stretchToFit) == 0)
  68. {
  69. scaleX = (flags & fillDestination) != 0 ? jmax (scaleX, scaleY)
  70. : jmin (scaleX, scaleY);
  71. if ((flags & onlyReduceInSize) != 0)
  72. scaleX = jmin (scaleX, 1.0f);
  73. if ((flags & onlyIncreaseInSize) != 0)
  74. scaleX = jmax (scaleX, 1.0f);
  75. scaleY = scaleX;
  76. if ((flags & xRight) != 0)
  77. newX += destination.getWidth() - source.getWidth() * scaleX; // right
  78. else if ((flags & xLeft) == 0)
  79. newX += (destination.getWidth() - source.getWidth() * scaleX) / 2.0f; // centre
  80. if ((flags & yBottom) != 0)
  81. newY += destination.getHeight() - source.getHeight() * scaleX; // bottom
  82. else if ((flags & yTop) == 0)
  83. newY += (destination.getHeight() - source.getHeight() * scaleX) / 2.0f; // centre
  84. }
  85. return AffineTransform::translation (-source.getX(), -source.getY())
  86. .scaled (scaleX, scaleY)
  87. .translated (newX, newY);
  88. }
  89. } // namespace juce