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_AnimatedPosition.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//==============================================================================
52template <typename Behaviour>
53class AnimatedPosition : private Timer
54{
55public:
59 {
60 }
61
64 {
65 range = newRange;
66 }
67
68 //==============================================================================
77 void beginDrag()
78 {
79 grabbedPos = position;
80 releaseVelocity = 0;
81 stopTimer();
82 }
83
89 {
90 moveTo (grabbedPos + deltaFromStartOfDrag);
91 }
92
96 void endDrag()
97 {
98 startTimerHz (60);
99 }
100
105 {
106 startTimerHz (10);
107 moveTo (position + deltaFromCurrentPosition);
108 }
109
110 //==============================================================================
112 double getPosition() const noexcept
113 {
114 return position;
115 }
116
122 {
123 stopTimer();
124 setPositionAndSendChange (newPosition);
125 }
126
127 //==============================================================================
133 {
134 public:
135 virtual ~Listener() = default;
136
138 virtual void positionChanged (AnimatedPosition&, double newPosition) = 0;
139 };
140
142 void addListener (Listener* listener) { listeners.add (listener); }
143
145 void removeListener (Listener* listener) { listeners.remove (listener); }
146
147 //==============================================================================
151 Behaviour behaviour;
152
153private:
154 //==============================================================================
155 double position = 0.0, grabbedPos = 0.0, releaseVelocity = 0.0;
156 Range<double> range;
157 Time lastUpdate, lastDrag;
158 ListenerList<Listener> listeners;
159
160 static double getSpeed (const Time last, double lastPos,
161 const Time now, double newPos)
162 {
163 auto elapsedSecs = jmax (0.005, (now - last).inSeconds());
164 auto v = (newPos - lastPos) / elapsedSecs;
165 return std::abs (v) > 0.2 ? v : 0.0;
166 }
167
168 void moveTo (double newPos)
169 {
170 auto now = Time::getCurrentTime();
171 releaseVelocity = getSpeed (lastDrag, position, now, newPos);
172 behaviour.releasedWithVelocity (newPos, releaseVelocity);
173 lastDrag = now;
174
175 setPositionAndSendChange (newPos);
176 }
177
178 void setPositionAndSendChange (double newPosition)
179 {
181
182 if (! approximatelyEqual (position, newPosition))
183 {
184 position = newPosition;
185 listeners.call ([this, newPosition] (Listener& l) { l.positionChanged (*this, newPosition); });
186 }
187 }
188
189 void timerCallback() override
190 {
191 auto now = Time::getCurrentTime();
192 auto elapsed = jlimit (0.001, 0.020, (now - lastUpdate).inSeconds());
193 lastUpdate = now;
194 auto newPos = behaviour.getNextPosition (position, elapsed);
195
196 if (behaviour.isStopped (newPos))
197 stopTimer();
198 else
199 startTimerHz (60);
200
201 setPositionAndSendChange (newPos);
202 }
203
205};
206
207} // namespace juce
Implement this class if you need to receive callbacks when the value of an AnimatedPosition changes.
virtual void positionChanged(AnimatedPosition &, double newPosition)=0
Called synchronously when an AnimatedPosition changes.
Models a 1-dimensional position that can be dragged around by the user, and which will then continue ...
Behaviour behaviour
The behaviour object.
void addListener(Listener *listener)
Adds a listener to be called when the value changes.
void endDrag()
Called after beginDrag() and drag() to indicate that the drag operation has now finished.
void setPosition(double newPosition)
Explicitly sets the position and stops any further movement.
double getPosition() const noexcept
Returns the current position.
void removeListener(Listener *listener)
Removes a previously-registered listener.
void nudge(double deltaFromCurrentPosition)
Called outside of a drag operation to cause a nudge in the specified direction.
void beginDrag()
Called to indicate that the object is now being controlled by a mouse-drag or similar operation.
void setLimits(Range< double > newRange) noexcept
Sets a range within which the value will be constrained.
void drag(double deltaFromStartOfDrag)
Called during a mouse-drag operation, to indicate that the mouse has moved.
Holds a set of objects and can invoke a member function callback on each object in the set with a sin...
void call(Callback &&callback)
Calls an invokable object for each listener in the list.
A general-purpose range object, that simply represents any linear range with a start and end point.
Definition juce_Range.h:40
ValueType clipValue(const ValueType value) const noexcept
Returns the nearest value to the one supplied, which lies within the range.
Definition juce_Range.h:220
Holds an absolute date and time.
Definition juce_Time.h:37
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Returns a Time object that is set to the current system time.
Makes repeated callbacks to a virtual method at a specified time interval.
Definition juce_Timer.h:52
void stopTimer() noexcept
Stops the timer.
void startTimerHz(int timerFrequencyHz) noexcept
Starts the timer with an interval specified in Hertz.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
JUCE Namespace.
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.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
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