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.

206 lines
7.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. /**
  20. Models a 1-dimensional position that can be dragged around by the user, and which
  21. will then continue moving with a customisable physics behaviour when released.
  22. This is useful for things like scrollable views or objects that can be dragged and
  23. thrown around with the mouse/touch, and by writing your own behaviour class, you can
  24. customise the trajectory that it follows when released.
  25. The class uses its own Timer to continuously change its value when a drag ends, and
  26. Listener objects can be registered to receive callbacks whenever the value changes.
  27. The value is stored as a double, and can be used to represent whatever units you need.
  28. The template parameter Behaviour must be a class that implements various methods to
  29. return the physics of the value's movement - you can use the classes provided for this
  30. in the AnimatedPositionBehaviours namespace, or write your own custom behaviour.
  31. @see AnimatedPositionBehaviours::ContinuousWithMomentum,
  32. AnimatedPositionBehaviours::SnapToPageBoundaries
  33. */
  34. template <typename Behaviour>
  35. class AnimatedPosition : private Timer
  36. {
  37. public:
  38. AnimatedPosition()
  39. : position(), grabbedPos(), releaseVelocity(),
  40. range (-std::numeric_limits<double>::max(),
  41. std::numeric_limits<double>::max())
  42. {
  43. }
  44. /** Sets a range within which the value will be constrained. */
  45. void setLimits (Range<double> newRange) noexcept
  46. {
  47. range = newRange;
  48. }
  49. //==============================================================================
  50. /** Called to indicate that the object is now being controlled by a
  51. mouse-drag or similar operation.
  52. After calling this method, you should make calls to the drag() method
  53. each time the mouse drags the position around, and always be sure to
  54. finish with a call to endDrag() when the mouse is released, which allows
  55. the position to continue moving freely according to the specified behaviour.
  56. */
  57. void beginDrag()
  58. {
  59. grabbedPos = position;
  60. releaseVelocity = 0;
  61. stopTimer();
  62. }
  63. /** Called during a mouse-drag operation, to indicate that the mouse has moved.
  64. The delta is the difference between the position when beginDrag() was called
  65. and the new position that's required.
  66. */
  67. void drag (double deltaFromStartOfDrag)
  68. {
  69. moveTo (grabbedPos + deltaFromStartOfDrag);
  70. }
  71. /** Called after beginDrag() and drag() to indicate that the drag operation has
  72. now finished.
  73. */
  74. void endDrag()
  75. {
  76. startTimerHz (60);
  77. }
  78. /** Called outside of a drag operation to cause a nudge in the specified direction.
  79. This is intended for use by e.g. mouse-wheel events.
  80. */
  81. void nudge (double deltaFromCurrentPosition)
  82. {
  83. startTimerHz (10);
  84. moveTo (position + deltaFromCurrentPosition);
  85. }
  86. //==============================================================================
  87. /** Returns the current position. */
  88. double getPosition() const noexcept
  89. {
  90. return position;
  91. }
  92. /** Explicitly sets the position and stops any further movement.
  93. This will cause a synchronous call to any listeners if the position actually
  94. changes.
  95. */
  96. void setPosition (double newPosition)
  97. {
  98. stopTimer();
  99. setPositionAndSendChange (newPosition);
  100. }
  101. //==============================================================================
  102. /** Implement this class if you need to receive callbacks when the value of
  103. an AnimatedPosition changes.
  104. @see AnimatedPosition::addListener, AnimatedPosition::removeListener
  105. */
  106. class Listener
  107. {
  108. public:
  109. virtual ~Listener() {}
  110. /** Called synchronously when an AnimatedPosition changes. */
  111. virtual void positionChanged (AnimatedPosition&, double newPosition) = 0;
  112. };
  113. /** Adds a listener to be called when the value changes. */
  114. void addListener (Listener* listener) { listeners.add (listener); }
  115. /** Removes a previously-registered listener. */
  116. void removeListener (Listener* listener) { listeners.remove (listener); }
  117. //==============================================================================
  118. /** The behaviour object.
  119. This is public to let you tweak any parameters that it provides.
  120. */
  121. Behaviour behaviour;
  122. private:
  123. //==============================================================================
  124. double position, grabbedPos, releaseVelocity;
  125. Range<double> range;
  126. Time lastUpdate, lastDrag;
  127. ListenerList<Listener> listeners;
  128. static double getSpeed (const Time last, double lastPos,
  129. const Time now, double newPos)
  130. {
  131. const double elapsedSecs = jmax (0.005, (now - last).inSeconds());
  132. const double v = (newPos - lastPos) / elapsedSecs;
  133. return std::abs (v) > 0.2 ? v : 0.0;
  134. }
  135. void moveTo (double newPos)
  136. {
  137. const Time now (Time::getCurrentTime());
  138. releaseVelocity = getSpeed (lastDrag, position, now, newPos);
  139. behaviour.releasedWithVelocity (newPos, releaseVelocity);
  140. lastDrag = now;
  141. setPositionAndSendChange (newPos);
  142. }
  143. void setPositionAndSendChange (double newPosition)
  144. {
  145. newPosition = range.clipValue (newPosition);
  146. if (position != newPosition)
  147. {
  148. position = newPosition;
  149. listeners.call (&Listener::positionChanged, *this, newPosition);
  150. }
  151. }
  152. void timerCallback() override
  153. {
  154. const Time now = Time::getCurrentTime();
  155. const double elapsed = jlimit (0.001, 0.020, (now - lastUpdate).inSeconds());
  156. lastUpdate = now;
  157. const double newPos = behaviour.getNextPosition (position, elapsed);
  158. if (behaviour.isStopped (newPos))
  159. stopTimer();
  160. else
  161. startTimerHz (60);
  162. setPositionAndSendChange (newPos);
  163. }
  164. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AnimatedPosition)
  165. };