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.

158 lines
5.5KB

  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. @tags{GUI}
  34. */
  35. struct ContinuousWithMomentum
  36. {
  37. ContinuousWithMomentum() = default;
  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. /** Sets the minimum velocity of the movement. Any velocity that's slower than
  46. this will stop the animation. The default is 0.05. */
  47. void setMinimumVelocity (double newMinimumVelocityToUse) noexcept
  48. {
  49. minimumVelocity = newMinimumVelocityToUse;
  50. }
  51. /** Called by the AnimatedPosition class. This tells us the position and
  52. velocity at which the user is about to release the object.
  53. The velocity is measured in units/second.
  54. */
  55. void releasedWithVelocity (double /*position*/, double releaseVelocity) noexcept
  56. {
  57. velocity = releaseVelocity;
  58. }
  59. /** Called by the AnimatedPosition class to get the new position, after
  60. the given time has elapsed.
  61. */
  62. double getNextPosition (double oldPos, double elapsedSeconds) noexcept
  63. {
  64. velocity *= damping;
  65. if (std::abs (velocity) < minimumVelocity)
  66. velocity = 0;
  67. return oldPos + velocity * elapsedSeconds;
  68. }
  69. /** Called by the AnimatedPosition class to check whether the object
  70. is now stationary.
  71. */
  72. bool isStopped (double /*position*/) const noexcept
  73. {
  74. return velocity == 0.0;
  75. }
  76. private:
  77. double velocity = 0, damping = 0.92, minimumVelocity = 0.05;
  78. };
  79. //==============================================================================
  80. /** A behaviour that gravitates an AnimatedPosition object towards the nearest
  81. integer position when released.
  82. This class is intended to be used as a template parameter to the
  83. AnimatedPosition class. It's handy when using an AnimatedPosition to show a
  84. series of pages, because it allows the pages can be scrolled smoothly, but when
  85. released, snaps back to show a whole page.
  86. @see AnimatedPosition
  87. @tags{GUI}
  88. */
  89. struct SnapToPageBoundaries
  90. {
  91. SnapToPageBoundaries() = default;
  92. /** Called by the AnimatedPosition class. This tells us the position and
  93. velocity at which the user is about to release the object.
  94. The velocity is measured in units/second.
  95. */
  96. void releasedWithVelocity (double position, double releaseVelocity) noexcept
  97. {
  98. targetSnapPosition = std::floor (position + 0.5);
  99. if (releaseVelocity > 1.0 && targetSnapPosition < position) ++targetSnapPosition;
  100. if (releaseVelocity < -1.0 && targetSnapPosition > position) --targetSnapPosition;
  101. }
  102. /** Called by the AnimatedPosition class to get the new position, after
  103. the given time has elapsed.
  104. */
  105. double getNextPosition (double oldPos, double elapsedSeconds) const noexcept
  106. {
  107. if (isStopped (oldPos))
  108. return targetSnapPosition;
  109. const double snapSpeed = 10.0;
  110. const double velocity = (targetSnapPosition - oldPos) * snapSpeed;
  111. const double newPos = oldPos + velocity * elapsedSeconds;
  112. return isStopped (newPos) ? targetSnapPosition : newPos;
  113. }
  114. /** Called by the AnimatedPosition class to check whether the object
  115. is now stationary.
  116. */
  117. bool isStopped (double position) const noexcept
  118. {
  119. return std::abs (targetSnapPosition - position) < 0.001;
  120. }
  121. private:
  122. double targetSnapPosition = 0.0;
  123. };
  124. }
  125. } // namespace juce