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.

154 lines
5.5KB

  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. //==============================================================================
  19. /** Contains classes for different types of physics behaviours - these classes
  20. are used as template parameters for the AnimatedPosition class.
  21. */
  22. namespace juce::AnimatedPositionBehaviours
  23. {
  24. /** A non-snapping behaviour that allows the content to be freely flicked in
  25. either direction, with momentum based on the velocity at which it was
  26. released, and variable friction to make it come to a halt.
  27. This class is intended to be used as a template parameter to the
  28. AnimatedPosition class.
  29. @see AnimatedPosition
  30. @tags{GUI}
  31. */
  32. struct ContinuousWithMomentum
  33. {
  34. ContinuousWithMomentum() = default;
  35. /** Sets the friction that damps the movement of the value.
  36. A typical value is 0.08; higher values indicate more friction.
  37. */
  38. void setFriction (double newFriction) noexcept
  39. {
  40. damping = 1.0 - newFriction;
  41. }
  42. /** Sets the minimum velocity of the movement. Any velocity that's slower than
  43. this will stop the animation. The default is 0.05. */
  44. void setMinimumVelocity (double newMinimumVelocityToUse) noexcept
  45. {
  46. minimumVelocity = newMinimumVelocityToUse;
  47. }
  48. /** Called by the AnimatedPosition class. This tells us the position and
  49. velocity at which the user is about to release the object.
  50. The velocity is measured in units/second.
  51. */
  52. void releasedWithVelocity (double /*position*/, double releaseVelocity) noexcept
  53. {
  54. velocity = releaseVelocity;
  55. }
  56. /** Called by the AnimatedPosition class to get the new position, after
  57. the given time has elapsed.
  58. */
  59. double getNextPosition (double oldPos, double elapsedSeconds) noexcept
  60. {
  61. velocity *= damping;
  62. if (std::abs (velocity) < minimumVelocity)
  63. velocity = 0;
  64. return oldPos + velocity * elapsedSeconds;
  65. }
  66. /** Called by the AnimatedPosition class to check whether the object
  67. is now stationary.
  68. */
  69. bool isStopped (double /*position*/) const noexcept
  70. {
  71. return approximatelyEqual (velocity, 0.0);
  72. }
  73. private:
  74. double velocity = 0, damping = 0.92, minimumVelocity = 0.05;
  75. };
  76. //==============================================================================
  77. /** A behaviour that gravitates an AnimatedPosition object towards the nearest
  78. integer position when released.
  79. This class is intended to be used as a template parameter to the
  80. AnimatedPosition class. It's handy when using an AnimatedPosition to show a
  81. series of pages, because it allows the pages can be scrolled smoothly, but when
  82. released, snaps back to show a whole page.
  83. @see AnimatedPosition
  84. @tags{GUI}
  85. */
  86. struct SnapToPageBoundaries
  87. {
  88. SnapToPageBoundaries() = default;
  89. /** Called by the AnimatedPosition class. This tells us the position and
  90. velocity at which the user is about to release the object.
  91. The velocity is measured in units/second.
  92. */
  93. void releasedWithVelocity (double position, double releaseVelocity) noexcept
  94. {
  95. targetSnapPosition = std::floor (position + 0.5);
  96. if (releaseVelocity > 1.0 && targetSnapPosition < position) ++targetSnapPosition;
  97. if (releaseVelocity < -1.0 && targetSnapPosition > position) --targetSnapPosition;
  98. }
  99. /** Called by the AnimatedPosition class to get the new position, after
  100. the given time has elapsed.
  101. */
  102. double getNextPosition (double oldPos, double elapsedSeconds) const noexcept
  103. {
  104. if (isStopped (oldPos))
  105. return targetSnapPosition;
  106. const double snapSpeed = 10.0;
  107. const double velocity = (targetSnapPosition - oldPos) * snapSpeed;
  108. const double newPos = oldPos + velocity * elapsedSeconds;
  109. return isStopped (newPos) ? targetSnapPosition : newPos;
  110. }
  111. /** Called by the AnimatedPosition class to check whether the object
  112. is now stationary.
  113. */
  114. bool isStopped (double position) const noexcept
  115. {
  116. return std::abs (targetSnapPosition - position) < 0.001;
  117. }
  118. private:
  119. double targetSnapPosition = 0.0;
  120. };
  121. } // namespace juce::AnimatedPositionBehaviours