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.

148 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. //==============================================================================
  19. /** Contains classes for different types of physics behaviours - these classes
  20. are used as template parameters for the AnimatedPosition class.
  21. */
  22. namespace AnimatedPositionBehaviours
  23. {
  24. /** A non-snapping behaviour that allows the content to be freely flicked in
  25. either direction, with momentum based on the velocity at which it was
  26. released, and variable friction to make it come to a halt.
  27. This class is intended to be used as a template parameter to the
  28. AnimatedPosition class.
  29. @see AnimatedPosition
  30. */
  31. struct ContinuousWithMomentum
  32. {
  33. ContinuousWithMomentum() noexcept
  34. : velocity (0), damping (0.92)
  35. {
  36. }
  37. /** Sets the friction that damps the movement of the value.
  38. A typical value is 0.08; higher values indicate more friction.
  39. */
  40. void setFriction (double newFriction) noexcept
  41. {
  42. damping = 1.0 - newFriction;
  43. }
  44. /** Called by the AnimatedPosition class. This tells us the position and
  45. velocity at which the user is about to release the object.
  46. The velocity is measured in units/second.
  47. */
  48. void releasedWithVelocity (double /*position*/, double releaseVelocity) noexcept
  49. {
  50. velocity = releaseVelocity;
  51. }
  52. /** Called by the AnimatedPosition class to get the new position, after
  53. the given time has elapsed.
  54. */
  55. double getNextPosition (double oldPos, double elapsedSeconds) noexcept
  56. {
  57. velocity *= damping;
  58. if (std::abs (velocity) < 0.05)
  59. velocity = 0;
  60. return oldPos + velocity * elapsedSeconds;
  61. }
  62. /** Called by the AnimatedPosition class to check whether the object
  63. is now stationary.
  64. */
  65. bool isStopped (double /*position*/) const noexcept
  66. {
  67. return velocity == 0;
  68. }
  69. private:
  70. double velocity, damping;
  71. };
  72. //==============================================================================
  73. /** A behaviour that gravitates an AnimatedPosition object towards the nearest
  74. integer position when released.
  75. This class is intended to be used as a template parameter to the
  76. AnimatedPosition class. It's handy when using an AnimatedPosition to show a
  77. series of pages, because it allows the pages can be scrolled smoothly, but when
  78. released, snaps back to show a whole page.
  79. @see AnimatedPosition
  80. */
  81. struct SnapToPageBoundaries
  82. {
  83. SnapToPageBoundaries() noexcept : targetSnapPosition()
  84. {
  85. }
  86. /** Called by the AnimatedPosition class. This tells us the position and
  87. velocity at which the user is about to release the object.
  88. The velocity is measured in units/second.
  89. */
  90. void releasedWithVelocity (double position, double releaseVelocity) noexcept
  91. {
  92. targetSnapPosition = std::floor (position + 0.5);
  93. if (releaseVelocity > 1.0 && targetSnapPosition < position) ++targetSnapPosition;
  94. if (releaseVelocity < -1.0 && targetSnapPosition > position) --targetSnapPosition;
  95. }
  96. /** Called by the AnimatedPosition class to get the new position, after
  97. the given time has elapsed.
  98. */
  99. double getNextPosition (double oldPos, double elapsedSeconds) const noexcept
  100. {
  101. if (isStopped (oldPos))
  102. return targetSnapPosition;
  103. const double snapSpeed = 10.0;
  104. const double velocity = (targetSnapPosition - oldPos) * snapSpeed;
  105. const double newPos = oldPos + velocity * elapsedSeconds;
  106. return isStopped (newPos) ? targetSnapPosition : newPos;
  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. }