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.

162 lines
5.6KB

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