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.

150 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_ANIMATEDPOSITIONBEHAVIOURS_H_INCLUDED
  18. #define JUCE_ANIMATEDPOSITIONBEHAVIOURS_H_INCLUDED
  19. //==============================================================================
  20. /** Contains classes for different types of physics behaviours - these classes
  21. are used as template parameters for the AnimatedPosition class.
  22. */
  23. namespace AnimatedPositionBehaviours
  24. {
  25. /** A non-snapping behaviour that allows the content to be freely flicked in
  26. either direction, with momentum based on the velocity at which it was
  27. released, and variable friction to make it come to a halt.
  28. This class is intended to be used as a template parameter to the
  29. AnimatedPosition class.
  30. @see AnimatedPosition
  31. */
  32. struct ContinuousWithMomentum
  33. {
  34. ContinuousWithMomentum() noexcept
  35. : velocity (0), damping (0.92)
  36. {
  37. }
  38. /** Sets the friction that damps the movement of the value.
  39. A typical value is 0.08; higher values indicate more friction.
  40. */
  41. void setFriction (double newFriction) noexcept
  42. {
  43. damping = 1.0 - newFriction;
  44. }
  45. /** Called by the AnimatedPosition class. This tells us the position and
  46. velocity at which the user is about to release the object.
  47. The velocity is measured in units/second.
  48. */
  49. void releasedWithVelocity (double /*position*/, double releaseVelocity) noexcept
  50. {
  51. velocity = releaseVelocity;
  52. }
  53. /** Called by the AnimatedPosition class to get the new position, after
  54. the given time has elapsed.
  55. */
  56. double getNextPosition (double oldPos, double elapsedSeconds) noexcept
  57. {
  58. velocity *= damping;
  59. if (std::abs (velocity) < 0.05)
  60. velocity = 0;
  61. return oldPos + velocity * elapsedSeconds;
  62. }
  63. /** Called by the AnimatedPosition class to check whether the object
  64. is now stationary.
  65. */
  66. bool isStopped (double /*position*/) const noexcept
  67. {
  68. return velocity == 0;
  69. }
  70. private:
  71. double velocity, damping;
  72. };
  73. //==============================================================================
  74. /** A behaviour that gravitates an AnimatedPosition object towards the nearest
  75. integer position when released.
  76. This class is intended to be used as a template parameter to the
  77. AnimatedPosition class. It's handy when using an AnimatedPosition to show a
  78. series of pages, because it allows the pages can be scrolled smoothly, but when
  79. released, snaps back to show a whole page.
  80. @see AnimatedPosition
  81. */
  82. struct SnapToPageBoundaries
  83. {
  84. SnapToPageBoundaries() noexcept : targetSnapPosition()
  85. {
  86. }
  87. /** Called by the AnimatedPosition class. This tells us the position and
  88. velocity at which the user is about to release the object.
  89. The velocity is measured in units/second.
  90. */
  91. void releasedWithVelocity (double position, double releaseVelocity) noexcept
  92. {
  93. targetSnapPosition = std::floor (position + 0.5);
  94. if (releaseVelocity > 1.0 && targetSnapPosition < position) ++targetSnapPosition;
  95. if (releaseVelocity < -1.0 && targetSnapPosition > position) --targetSnapPosition;
  96. }
  97. /** Called by the AnimatedPosition class to get the new position, after
  98. the given time has elapsed.
  99. */
  100. double getNextPosition (double oldPos, double elapsedSeconds) const noexcept
  101. {
  102. if (isStopped (oldPos))
  103. return targetSnapPosition;
  104. const double snapSpeed = 10.0;
  105. const double velocity = (targetSnapPosition - oldPos) * snapSpeed;
  106. return oldPos + velocity * elapsedSeconds;
  107. }
  108. /** Called by the AnimatedPosition class to check whether the object
  109. is now stationary.
  110. */
  111. bool isStopped (double position) const noexcept
  112. {
  113. return std::abs (targetSnapPosition - position) < 0.001;
  114. }
  115. private:
  116. double targetSnapPosition;
  117. };
  118. };
  119. #endif // JUCE_ANIMATEDPOSITIONBEHAVIOURS_H_INCLUDED