LMMS
Loading...
Searching...
No Matches
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) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29//==============================================================================
34{
47 {
49
53 void setFriction (double newFriction) noexcept
54 {
55 damping = 1.0 - newFriction;
56 }
57
60 void setMinimumVelocity (double newMinimumVelocityToUse) noexcept
61 {
62 minimumVelocity = newMinimumVelocityToUse;
63 }
64
69 void releasedWithVelocity (double /*position*/, double releaseVelocity) noexcept
70 {
71 velocity = releaseVelocity;
72 }
73
77 double getNextPosition (double oldPos, double elapsedSeconds) noexcept
78 {
80
81 if (std::abs (velocity) < minimumVelocity)
82 velocity = 0;
83
84 return oldPos + velocity * elapsedSeconds;
85 }
86
90 bool isStopped (double /*position*/) const noexcept
91 {
92 return velocity == 0.0;
93 }
94
95 private:
96 double velocity = 0, damping = 0.92, minimumVelocity = 0.05;
97 };
98
99 //==============================================================================
113 {
115
120 void releasedWithVelocity (double position, double releaseVelocity) noexcept
121 {
122 targetSnapPosition = std::floor (position + 0.5);
123
124 if (releaseVelocity > 1.0 && targetSnapPosition < position) ++targetSnapPosition;
125 if (releaseVelocity < -1.0 && targetSnapPosition > position) --targetSnapPosition;
126 }
127
131 double getNextPosition (double oldPos, double elapsedSeconds) const noexcept
132 {
133 if (isStopped (oldPos))
134 return targetSnapPosition;
135
136 const double snapSpeed = 10.0;
137 const double velocity = (targetSnapPosition - oldPos) * snapSpeed;
138 const double newPos = oldPos + velocity * elapsedSeconds;
139
140 return isStopped (newPos) ? targetSnapPosition : newPos;
141 }
142
146 bool isStopped (double position) const noexcept
147 {
148 return std::abs (targetSnapPosition - position) < 0.001;
149 }
150
151 private:
152 double targetSnapPosition = 0.0;
153 };
154}
155
156} // namespace juce
Definition juce_AnimatedPositionBehaviours.h:34
Definition carla_juce.cpp:31
double velocity
Definition juce_AnimatedPositionBehaviours.h:96
bool isStopped(double) const noexcept
Definition juce_AnimatedPositionBehaviours.h:90
double getNextPosition(double oldPos, double elapsedSeconds) noexcept
Definition juce_AnimatedPositionBehaviours.h:77
void setFriction(double newFriction) noexcept
Definition juce_AnimatedPositionBehaviours.h:53
void releasedWithVelocity(double, double releaseVelocity) noexcept
Definition juce_AnimatedPositionBehaviours.h:69
double minimumVelocity
Definition juce_AnimatedPositionBehaviours.h:96
double damping
Definition juce_AnimatedPositionBehaviours.h:96
void setMinimumVelocity(double newMinimumVelocityToUse) noexcept
Definition juce_AnimatedPositionBehaviours.h:60
bool isStopped(double position) const noexcept
Definition juce_AnimatedPositionBehaviours.h:146
double targetSnapPosition
Definition juce_AnimatedPositionBehaviours.h:152
double getNextPosition(double oldPos, double elapsedSeconds) const noexcept
Definition juce_AnimatedPositionBehaviours.h:131
void releasedWithVelocity(double position, double releaseVelocity) noexcept
Definition juce_AnimatedPositionBehaviours.h:120