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.

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