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

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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::detail
  19. {
  20. struct ScalingHelpers
  21. {
  22. template <typename PointOrRect>
  23. static PointOrRect unscaledScreenPosToScaled (float scale, PointOrRect pos) noexcept
  24. {
  25. return ! approximatelyEqual (scale, 1.0f) ? pos / scale : pos;
  26. }
  27. template <typename PointOrRect>
  28. static PointOrRect scaledScreenPosToUnscaled (float scale, PointOrRect pos) noexcept
  29. {
  30. return ! approximatelyEqual (scale, 1.0f) ? pos * scale : pos;
  31. }
  32. // For these, we need to avoid getSmallestIntegerContainer being used, which causes
  33. // judder when moving windows
  34. static Rectangle<int> unscaledScreenPosToScaled (float scale, Rectangle<int> pos) noexcept
  35. {
  36. return ! approximatelyEqual (scale, 1.0f) ? Rectangle<int> (roundToInt ((float) pos.getX() / scale),
  37. roundToInt ((float) pos.getY() / scale),
  38. roundToInt ((float) pos.getWidth() / scale),
  39. roundToInt ((float) pos.getHeight() / scale)) : pos;
  40. }
  41. static Rectangle<int> scaledScreenPosToUnscaled (float scale, Rectangle<int> pos) noexcept
  42. {
  43. return ! approximatelyEqual (scale, 1.0f) ? Rectangle<int> (roundToInt ((float) pos.getX() * scale),
  44. roundToInt ((float) pos.getY() * scale),
  45. roundToInt ((float) pos.getWidth() * scale),
  46. roundToInt ((float) pos.getHeight() * scale)) : pos;
  47. }
  48. static Rectangle<float> unscaledScreenPosToScaled (float scale, Rectangle<float> pos) noexcept
  49. {
  50. return ! approximatelyEqual (scale, 1.0f) ? Rectangle<float> (pos.getX() / scale,
  51. pos.getY() / scale,
  52. pos.getWidth() / scale,
  53. pos.getHeight() / scale) : pos;
  54. }
  55. static Rectangle<float> scaledScreenPosToUnscaled (float scale, Rectangle<float> pos) noexcept
  56. {
  57. return ! approximatelyEqual (scale, 1.0f) ? Rectangle<float> (pos.getX() * scale,
  58. pos.getY() * scale,
  59. pos.getWidth() * scale,
  60. pos.getHeight() * scale) : pos;
  61. }
  62. template <typename PointOrRect>
  63. static PointOrRect unscaledScreenPosToScaled (PointOrRect pos) noexcept
  64. {
  65. return unscaledScreenPosToScaled (Desktop::getInstance().getGlobalScaleFactor(), pos);
  66. }
  67. template <typename PointOrRect>
  68. static PointOrRect scaledScreenPosToUnscaled (PointOrRect pos) noexcept
  69. {
  70. return scaledScreenPosToUnscaled (Desktop::getInstance().getGlobalScaleFactor(), pos);
  71. }
  72. template <typename PointOrRect>
  73. static PointOrRect unscaledScreenPosToScaled (const Component& comp, PointOrRect pos) noexcept
  74. {
  75. return unscaledScreenPosToScaled (comp.getDesktopScaleFactor(), pos);
  76. }
  77. template <typename PointOrRect>
  78. static PointOrRect scaledScreenPosToUnscaled (const Component& comp, PointOrRect pos) noexcept
  79. {
  80. return scaledScreenPosToUnscaled (comp.getDesktopScaleFactor(), pos);
  81. }
  82. static Point<int> addPosition (Point<int> p, const Component& c) noexcept { return p + c.getPosition(); }
  83. static Rectangle<int> addPosition (Rectangle<int> p, const Component& c) noexcept { return p + c.getPosition(); }
  84. static Point<float> addPosition (Point<float> p, const Component& c) noexcept { return p + c.getPosition().toFloat(); }
  85. static Rectangle<float> addPosition (Rectangle<float> p, const Component& c) noexcept { return p + c.getPosition().toFloat(); }
  86. static Point<int> subtractPosition (Point<int> p, const Component& c) noexcept { return p - c.getPosition(); }
  87. static Rectangle<int> subtractPosition (Rectangle<int> p, const Component& c) noexcept { return p - c.getPosition(); }
  88. static Point<float> subtractPosition (Point<float> p, const Component& c) noexcept { return p - c.getPosition().toFloat(); }
  89. static Rectangle<float> subtractPosition (Rectangle<float> p, const Component& c) noexcept { return p - c.getPosition().toFloat(); }
  90. static Point<float> screenPosToLocalPos (Component& comp, Point<float> pos)
  91. {
  92. if (auto* peer = comp.getPeer())
  93. {
  94. pos = peer->globalToLocal (pos);
  95. auto& peerComp = peer->getComponent();
  96. return comp.getLocalPoint (&peerComp, unscaledScreenPosToScaled (peerComp, pos));
  97. }
  98. return comp.getLocalPoint (nullptr, unscaledScreenPosToScaled (comp, pos));
  99. }
  100. };
  101. } // namespace juce::detail