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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /** Contains classes for different types of physics behaviours - these classes
  23. are used as template parameters for the AnimatedPosition class.
  24. */
  25. namespace AnimatedPositionBehaviours
  26. {
  27. /** A non-snapping behaviour that allows the content to be freely flicked in
  28. either direction, with momentum based on the velocity at which it was
  29. released, and variable friction to make it come to a halt.
  30. This class is intended to be used as a template parameter to the
  31. AnimatedPosition class.
  32. @see AnimatedPosition
  33. */
  34. struct ContinuousWithMomentum
  35. {
  36. ContinuousWithMomentum() noexcept
  37. {
  38. }
  39. /** Sets the friction that damps the movement of the value.
  40. A typical value is 0.08; higher values indicate more friction.
  41. */
  42. void setFriction (double newFriction) noexcept
  43. {
  44. damping = 1.0 - newFriction;
  45. }
  46. /** Sets the minimum velocity of the movement. Any velocity that's slower than
  47. this will stop the animation. The default is 0.05. */
  48. void setMinimumVelocity (double newMinimumVelocityToUse) noexcept
  49. {
  50. minimumVelocity = newMinimumVelocityToUse;
  51. }
  52. /** Called by the AnimatedPosition class. This tells us the position and
  53. velocity at which the user is about to release the object.
  54. The velocity is measured in units/second.
  55. */
  56. void releasedWithVelocity (double /*position*/, double releaseVelocity) noexcept
  57. {
  58. velocity = releaseVelocity;
  59. }
  60. /** Called by the AnimatedPosition class to get the new position, after
  61. the given time has elapsed.
  62. */
  63. double getNextPosition (double oldPos, double elapsedSeconds) noexcept
  64. {
  65. velocity *= damping;
  66. if (std::abs (velocity) < minimumVelocity)
  67. velocity = 0;
  68. return oldPos + velocity * elapsedSeconds;
  69. }
  70. /** Called by the AnimatedPosition class to check whether the object
  71. is now stationary.
  72. */
  73. bool isStopped (double /*position*/) const noexcept
  74. {
  75. return velocity == 0.0;
  76. }
  77. private:
  78. double velocity = 0, damping = 0.92, minimumVelocity = 0.05;
  79. };
  80. //==============================================================================
  81. /** A behaviour that gravitates an AnimatedPosition object towards the nearest
  82. integer position when released.
  83. This class is intended to be used as a template parameter to the
  84. AnimatedPosition class. It's handy when using an AnimatedPosition to show a
  85. series of pages, because it allows the pages can be scrolled smoothly, but when
  86. released, snaps back to show a whole page.
  87. @see AnimatedPosition
  88. */
  89. struct SnapToPageBoundaries
  90. {
  91. SnapToPageBoundaries() noexcept : targetSnapPosition()
  92. {
  93. }
  94. /** Called by the AnimatedPosition class. This tells us the position and
  95. velocity at which the user is about to release the object.
  96. The velocity is measured in units/second.
  97. */
  98. void releasedWithVelocity (double position, double releaseVelocity) noexcept
  99. {
  100. targetSnapPosition = std::floor (position + 0.5);
  101. if (releaseVelocity > 1.0 && targetSnapPosition < position) ++targetSnapPosition;
  102. if (releaseVelocity < -1.0 && targetSnapPosition > position) --targetSnapPosition;
  103. }
  104. /** Called by the AnimatedPosition class to get the new position, after
  105. the given time has elapsed.
  106. */
  107. double getNextPosition (double oldPos, double elapsedSeconds) const noexcept
  108. {
  109. if (isStopped (oldPos))
  110. return targetSnapPosition;
  111. const double snapSpeed = 10.0;
  112. const double velocity = (targetSnapPosition - oldPos) * snapSpeed;
  113. const double newPos = oldPos + velocity * elapsedSeconds;
  114. return isStopped (newPos) ? targetSnapPosition : newPos;
  115. }
  116. /** Called by the AnimatedPosition class to check whether the object
  117. is now stationary.
  118. */
  119. bool isStopped (double position) const noexcept
  120. {
  121. return std::abs (targetSnapPosition - position) < 0.001;
  122. }
  123. private:
  124. double targetSnapPosition;
  125. };
  126. }
  127. } // namespace juce