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.

201 lines
6.9KB

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