JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
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
26//==============================================================================
31{
32
45 {
46 ContinuousWithMomentum() = default;
47
51 void setFriction (double newFriction) noexcept
52 {
53 damping = 1.0 - newFriction;
54 }
55
59 {
60 minimumVelocity = newMinimumVelocityToUse;
61 }
62
67 void releasedWithVelocity (double /*position*/, double releaseVelocity) noexcept
68 {
69 velocity = releaseVelocity;
70 }
71
75 double getNextPosition (double oldPos, double elapsedSeconds) noexcept
76 {
77 velocity *= damping;
78
79 if (std::abs (velocity) < minimumVelocity)
80 velocity = 0;
81
82 return oldPos + velocity * elapsedSeconds;
83 }
84
88 bool isStopped (double /*position*/) const noexcept
89 {
90 return approximatelyEqual (velocity, 0.0);
91 }
92
93 private:
94 double velocity = 0, damping = 0.92, minimumVelocity = 0.05;
95 };
96
97 //==============================================================================
111 {
112 SnapToPageBoundaries() = default;
113
118 void releasedWithVelocity (double position, double releaseVelocity) noexcept
119 {
120 targetSnapPosition = std::floor (position + 0.5);
121
122 if (releaseVelocity > 1.0 && targetSnapPosition < position) ++targetSnapPosition;
123 if (releaseVelocity < -1.0 && targetSnapPosition > position) --targetSnapPosition;
124 }
125
129 double getNextPosition (double oldPos, double elapsedSeconds) const noexcept
130 {
131 if (isStopped (oldPos))
132 return targetSnapPosition;
133
134 const double snapSpeed = 10.0;
135 const double velocity = (targetSnapPosition - oldPos) * snapSpeed;
136 const double newPos = oldPos + velocity * elapsedSeconds;
137
138 return isStopped (newPos) ? targetSnapPosition : newPos;
139 }
140
144 bool isStopped (double position) const noexcept
145 {
146 return std::abs (targetSnapPosition - position) < 0.001;
147 }
148
149 private:
150 double targetSnapPosition = 0.0;
151 };
152
153} // namespace juce::AnimatedPositionBehaviours
T floor(T... args)
Contains classes for different types of physics behaviours - these classes are used as template param...
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
A non-snapping behaviour that allows the content to be freely flicked in either direction,...
bool isStopped(double) const noexcept
Called by the AnimatedPosition class to check whether the object is now stationary.
double getNextPosition(double oldPos, double elapsedSeconds) noexcept
Called by the AnimatedPosition class to get the new position, after the given time has elapsed.
void setFriction(double newFriction) noexcept
Sets the friction that damps the movement of the value.
void releasedWithVelocity(double, double releaseVelocity) noexcept
Called by the AnimatedPosition class.
void setMinimumVelocity(double newMinimumVelocityToUse) noexcept
Sets the minimum velocity of the movement.
A behaviour that gravitates an AnimatedPosition object towards the nearest integer position when rele...
bool isStopped(double position) const noexcept
Called by the AnimatedPosition class to check whether the object is now stationary.
double getNextPosition(double oldPos, double elapsedSeconds) const noexcept
Called by the AnimatedPosition class to get the new position, after the given time has elapsed.
void releasedWithVelocity(double position, double releaseVelocity) noexcept
Called by the AnimatedPosition class.