openshot-audio  0.1.6
juce_AnimatedPositionBehaviours.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission is granted to use this software under the terms of either:
8  a) the GPL v2 (or any later version)
9  b) the Affero GPL v3
10 
11  Details of these licenses can be found at: www.gnu.org/licenses
12 
13  JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17  ------------------------------------------------------------------------------
18 
19  To release a closed-source product which uses JUCE, commercial licenses are
20  available: visit www.juce.com for more information.
21 
22  ==============================================================================
23 */
24 
25 #ifndef JUCE_ANIMATEDPOSITIONBEHAVIOURS_H_INCLUDED
26 #define JUCE_ANIMATEDPOSITIONBEHAVIOURS_H_INCLUDED
27 
28 
29 //==============================================================================
34 {
45  {
47  : velocity (0), damping (0.92)
48  {
49  }
50 
54  void setFriction (double newFriction) noexcept
55  {
56  damping = 1.0 - newFriction;
57  }
58 
63  void releasedWithVelocity (double /*position*/, double releaseVelocity) noexcept
64  {
65  velocity = releaseVelocity;
66  }
67 
71  double getNextPosition (double oldPos, double elapsedSeconds) noexcept
72  {
73  velocity *= damping;
74 
75  if (std::abs (velocity) < 0.05)
76  velocity = 0;
77 
78  return oldPos + velocity * elapsedSeconds;
79  }
80 
84  bool isStopped (double /*position*/) const noexcept
85  {
86  return velocity == 0;
87  }
88 
89  private:
90  double velocity, damping;
91  };
92 
93  //==============================================================================
105  {
106  SnapToPageBoundaries() noexcept : targetSnapPosition()
107  {
108  }
109 
114  void releasedWithVelocity (double position, double releaseVelocity) noexcept
115  {
116  targetSnapPosition = std::floor (position + 0.5);
117 
118  if (releaseVelocity > 1.0 && targetSnapPosition < position) ++targetSnapPosition;
119  if (releaseVelocity < -1.0 && targetSnapPosition > position) --targetSnapPosition;
120  }
121 
125  double getNextPosition (double oldPos, double elapsedSeconds) const noexcept
126  {
127  if (isStopped (oldPos))
128  return targetSnapPosition;
129 
130  const double snapSpeed = 10.0;
131  const double velocity = (targetSnapPosition - oldPos) * snapSpeed;
132  const double newPos = oldPos + velocity * elapsedSeconds;
133 
134  return isStopped (newPos) ? targetSnapPosition : newPos;
135  }
136 
140  bool isStopped (double position) const noexcept
141  {
142  return std::abs (targetSnapPosition - position) < 0.001;
143  }
144 
145  private:
146  double targetSnapPosition;
147  };
148 };
149 
150 
151 #endif // JUCE_ANIMATEDPOSITIONBEHAVIOURS_H_INCLUDED
#define noexcept
Definition: juce_CompilerSupport.h:141
bool isStopped(double position) const noexcept
Definition: juce_AnimatedPositionBehaviours.h:140
ContinuousWithMomentum() noexcept
Definition: juce_AnimatedPositionBehaviours.h:46
Definition: juce_AnimatedPositionBehaviours.h:44
#define const
double getNextPosition(double oldPos, double elapsedSeconds) const noexcept
Definition: juce_AnimatedPositionBehaviours.h:125
Definition: juce_AnimatedPositionBehaviours.h:33
SnapToPageBoundaries() noexcept
Definition: juce_AnimatedPositionBehaviours.h:106
void setFriction(double newFriction) noexcept
Definition: juce_AnimatedPositionBehaviours.h:54
Definition: juce_AnimatedPositionBehaviours.h:104
bool isStopped(double) const noexcept
Definition: juce_AnimatedPositionBehaviours.h:84
void releasedWithVelocity(double position, double releaseVelocity) noexcept
Definition: juce_AnimatedPositionBehaviours.h:114
void releasedWithVelocity(double, double releaseVelocity) noexcept
Definition: juce_AnimatedPositionBehaviours.h:63
double getNextPosition(double oldPos, double elapsedSeconds) noexcept
Definition: juce_AnimatedPositionBehaviours.h:71