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

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #pragma once
  20. //==============================================================================
  21. /** Contains classes for different types of physics behaviours - these classes
  22. are used as template parameters for the AnimatedPosition class.
  23. */
  24. namespace AnimatedPositionBehaviours
  25. {
  26. /** A non-snapping behaviour that allows the content to be freely flicked in
  27. either direction, with momentum based on the velocity at which it was
  28. released, and variable friction to make it come to a halt.
  29. This class is intended to be used as a template parameter to the
  30. AnimatedPosition class.
  31. @see AnimatedPosition
  32. */
  33. struct ContinuousWithMomentum
  34. {
  35. ContinuousWithMomentum() noexcept
  36. {
  37. }
  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. /** 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) < 0.05)
  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;
  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. */
  82. struct SnapToPageBoundaries
  83. {
  84. SnapToPageBoundaries() noexcept : targetSnapPosition()
  85. {
  86. }
  87. /** Called by the AnimatedPosition class. This tells us the position and
  88. velocity at which the user is about to release the object.
  89. The velocity is measured in units/second.
  90. */
  91. void releasedWithVelocity (double position, double releaseVelocity) noexcept
  92. {
  93. targetSnapPosition = std::floor (position + 0.5);
  94. if (releaseVelocity > 1.0 && targetSnapPosition < position) ++targetSnapPosition;
  95. if (releaseVelocity < -1.0 && targetSnapPosition > position) --targetSnapPosition;
  96. }
  97. /** Called by the AnimatedPosition class to get the new position, after
  98. the given time has elapsed.
  99. */
  100. double getNextPosition (double oldPos, double elapsedSeconds) const noexcept
  101. {
  102. if (isStopped (oldPos))
  103. return targetSnapPosition;
  104. const double snapSpeed = 10.0;
  105. const double velocity = (targetSnapPosition - oldPos) * snapSpeed;
  106. const double newPos = oldPos + velocity * elapsedSeconds;
  107. return isStopped (newPos) ? targetSnapPosition : newPos;
  108. }
  109. /** Called by the AnimatedPosition class to check whether the object
  110. is now stationary.
  111. */
  112. bool isStopped (double position) const noexcept
  113. {
  114. return std::abs (targetSnapPosition - position) < 0.001;
  115. }
  116. private:
  117. double targetSnapPosition;
  118. };
  119. }