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.

150 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /** Contains classes for different types of physics behaviours - these classes
  17. are used as template parameters for the AnimatedPosition class.
  18. */
  19. namespace AnimatedPositionBehaviours
  20. {
  21. /** A non-snapping behaviour that allows the content to be freely flicked in
  22. either direction, with momentum based on the velocity at which it was
  23. released, and variable friction to make it come to a halt.
  24. This class is intended to be used as a template parameter to the
  25. AnimatedPosition class.
  26. @see AnimatedPosition
  27. @tags{GUI}
  28. */
  29. struct ContinuousWithMomentum
  30. {
  31. ContinuousWithMomentum() = default;
  32. /** Sets the friction that damps the movement of the value.
  33. A typical value is 0.08; higher values indicate more friction.
  34. */
  35. void setFriction (double newFriction) noexcept
  36. {
  37. damping = 1.0 - newFriction;
  38. }
  39. /** Sets the minimum velocity of the movement. Any velocity that's slower than
  40. this will stop the animation. The default is 0.05. */
  41. void setMinimumVelocity (double newMinimumVelocityToUse) noexcept
  42. {
  43. minimumVelocity = newMinimumVelocityToUse;
  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) < minimumVelocity)
  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.0;
  69. }
  70. private:
  71. double velocity = 0, damping = 0.92, minimumVelocity = 0.05;
  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. @tags{GUI}
  82. */
  83. struct SnapToPageBoundaries
  84. {
  85. SnapToPageBoundaries() = default;
  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 = 0.0;
  117. };
  118. }
  119. } // namespace juce