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_AnimatedPositionBehaviours.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. const double newPos = oldPos + velocity * elapsedSeconds;
  107. return isStopped (newPos) ? targetSnapPosition : newPos;
  108. }
  109. /** Called by the AnimatedPosition class to check whether the object
  110. is now stationary.
  111. */
  112. bool isStopped (double position) const noexcept
  113. {
  114. return std::abs (targetSnapPosition - position) < 0.001;
  115. }
  116. private:
  117. double targetSnapPosition;
  118. };
  119. };
  120. #endif // JUCE_ANIMATEDPOSITIONBEHAVIOURS_H_INCLUDED