Models a 1-dimensional position that can be dragged around by the user, and which will then continue moving with a customisable physics behaviour when released. More...
#include <juce_AnimatedPosition.h>
Classes | |
class | Listener |
Implement this class if you need to receive callbacks when the value of an AnimatedPosition changes. More... | |
Public Member Functions | |
AnimatedPosition () | |
void | addListener (Listener *listener) |
Adds a listener to be called when the value changes. More... | |
void | beginDrag () |
Called to indicate that the object is now being controlled by a mouse-drag or similar operation. More... | |
void | drag (double deltaFromStartOfDrag) |
Called during a mouse-drag operation, to indicate that the mouse has moved. More... | |
void | endDrag () |
Called after beginDrag() and drag() to indicate that the drag operation has now finished. More... | |
double | getPosition () const noexcept |
Returns the current position. More... | |
void | nudge (double deltaFromCurrentPosition) |
Called outside of a drag operation to cause a nudge in the specified direction. More... | |
void | removeListener (Listener *listener) |
Removes a previously-registered listener. More... | |
void | setLimits (Range< double > newRange) noexcept |
Sets a range within which the value will be constrained. More... | |
void | setPosition (double newPosition) |
Explicitly sets the position and stops any further movement. More... | |
Public Attributes | |
Behaviour | behaviour |
The behaviour object. More... | |
Private Member Functions | |
int | getTimerInterval () const noexcept |
Returns the timer's interval. More... | |
bool | isTimerRunning () const noexcept |
Returns true if the timer is currently running. More... | |
void | moveTo (double newPos) |
void | setPositionAndSendChange (double newPosition) |
void | startTimer (int intervalInMilliseconds) noexcept |
Starts the timer and sets the length of interval required. More... | |
void | startTimerHz (int timerFrequencyHz) noexcept |
Starts the timer with an interval specified in Hertz. More... | |
void | stopTimer () noexcept |
Stops the timer. More... | |
void | timerCallback () override |
The user-defined callback routine that actually gets called periodically. More... | |
Static Private Member Functions | |
static void | callAfterDelay (int milliseconds, std::function< void()> functionToCall) |
Invokes a lambda after a given number of milliseconds. More... | |
static void | callPendingTimersSynchronously () |
For internal use only: invokes any timers that need callbacks. More... | |
static double | getSpeed (const Time last, double lastPos, const Time now, double newPos) |
Private Attributes | |
double | grabbedPos |
Time | lastDrag |
Time | lastUpdate |
ListenerList< Listener > | listeners |
double | position |
Range< double > | range |
double | releaseVelocity |
Models a 1-dimensional position that can be dragged around by the user, and which will then continue moving with a customisable physics behaviour when released.
This is useful for things like scrollable views or objects that can be dragged and thrown around with the mouse/touch, and by writing your own behaviour class, you can customise the trajectory that it follows when released.
The class uses its own Timer to continuously change its value when a drag ends, and Listener objects can be registered to receive callbacks whenever the value changes.
The value is stored as a double, and can be used to represent whatever units you need.
The template parameter Behaviour must be a class that implements various methods to return the physics of the value's movement - you can use the classes provided for this in the AnimatedPositionBehaviours namespace, or write your own custom behaviour.
|
inline |
|
inline |
Adds a listener to be called when the value changes.
References AnimatedPosition< Behaviour >::listeners.
|
inline |
Called to indicate that the object is now being controlled by a mouse-drag or similar operation.
After calling this method, you should make calls to the drag() method each time the mouse drags the position around, and always be sure to finish with a call to endDrag() when the mouse is released, which allows the position to continue moving freely according to the specified behaviour.
References AnimatedPosition< Behaviour >::grabbedPos, AnimatedPosition< Behaviour >::position, AnimatedPosition< Behaviour >::releaseVelocity, and Timer::stopTimer().
|
staticinherited |
Invokes a lambda after a given number of milliseconds.
|
staticinherited |
For internal use only: invokes any timers that need callbacks.
Don't call this unless you really know what you're doing!
|
inline |
Called during a mouse-drag operation, to indicate that the mouse has moved.
The delta is the difference between the position when beginDrag() was called and the new position that's required.
References AnimatedPosition< Behaviour >::grabbedPos, and AnimatedPosition< Behaviour >::moveTo().
|
inline |
Called after beginDrag() and drag() to indicate that the drag operation has now finished.
References Timer::startTimerHz().
|
inlinenoexcept |
Returns the current position.
References AnimatedPosition< Behaviour >::position.
|
inlinestaticprivate |
References juce::jmax().
Referenced by AnimatedPosition< Behaviour >::moveTo().
|
inlinenoexceptinherited |
Returns the timer's interval.
References JUCE_CALLTYPE.
Referenced by CarbonViewWrapperComponent::setOurSizeToEmbeddedViewSize().
|
inlinenoexceptinherited |
Returns true if the timer is currently running.
|
inlineprivate |
|
inline |
Called outside of a drag operation to cause a nudge in the specified direction.
This is intended for use by e.g. mouse-wheel events.
References AnimatedPosition< Behaviour >::moveTo(), AnimatedPosition< Behaviour >::position, and Timer::startTimerHz().
|
inline |
Removes a previously-registered listener.
References AnimatedPosition< Behaviour >::listeners.
|
inlinenoexcept |
Sets a range within which the value will be constrained.
References AnimatedPosition< Behaviour >::range.
|
inline |
Explicitly sets the position and stops any further movement.
This will cause a synchronous call to any listeners if the position actually changes.
References AnimatedPosition< Behaviour >::setPositionAndSendChange(), and Timer::stopTimer().
|
inlineprivate |
References ListenerList< ListenerClass, ArrayType >::call(), Range< ValueType >::clipValue(), and AnimatedPosition< Behaviour >::Listener::positionChanged().
Referenced by AnimatedPosition< Behaviour >::moveTo(), AnimatedPosition< Behaviour >::setPosition(), and AnimatedPosition< Behaviour >::timerCallback().
|
noexceptinherited |
Starts the timer and sets the length of interval required.
If the timer is already started, this will reset it, so the time between calling this method and the next timer callback will not be less than the interval length passed in.
intervalInMilliseconds | the interval to use (any value less than 1 will be rounded up to 1) |
Referenced by FakeMouseMoveGenerator::FakeMouseMoveGenerator(), StandalonePluginHolder::init(), and CarbonViewWrapperComponent::setOurSizeToEmbeddedViewSize().
|
noexceptinherited |
Starts the timer with an interval specified in Hertz.
This is effectively the same as calling startTimer (1000 / timerFrequencyHz).
Referenced by AnimatedPosition< Behaviour >::endDrag(), AnimatedPosition< Behaviour >::nudge(), and AnimatedPosition< Behaviour >::timerCallback().
|
noexceptinherited |
Stops the timer.
No more timer callbacks will be triggered after this method returns.
Note that if you call this from a background thread while the message-thread is already in the middle of your callback, then this method will cancel any future timer callbacks, but it will return without waiting for the current one to finish. The current callback will continue, possibly still running some of your timer code after this method has returned.
Referenced by AnimatedPosition< Behaviour >::beginDrag(), CarbonViewWrapperComponent::setOurSizeToEmbeddedViewSize(), AnimatedPosition< Behaviour >::setPosition(), AnimatedPosition< Behaviour >::timerCallback(), and StandalonePluginHolder::~StandalonePluginHolder().
|
inlineoverrideprivatevirtual |
The user-defined callback routine that actually gets called periodically.
It's perfectly ok to call startTimer() or stopTimer() from within this callback to change the subsequent intervals.
Implements Timer.
References Time::getCurrentTime(), jlimit(), JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR, AnimatedPosition< Behaviour >::setPositionAndSendChange(), Timer::startTimerHz(), and Timer::stopTimer().
Behaviour AnimatedPosition< Behaviour >::behaviour |
The behaviour object.
This is public to let you tweak any parameters that it provides.
|
private |
Referenced by AnimatedPosition< Behaviour >::beginDrag(), and AnimatedPosition< Behaviour >::drag().
|
private |
|
private |
|
private |
|
private |
|
private |
Referenced by AnimatedPosition< Behaviour >::setLimits().
|
private |
Referenced by AnimatedPosition< Behaviour >::beginDrag().