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_AnimatedPosition.h 7.1KB

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