3#include <animation/SInterpolatorImpl.h>
4#include <helper/STime.h>
49 removeAnimationCallback();
52void SValueAnimator::addAnimationCallback()
70void SValueAnimator::removeAnimationCallback()
84void SValueAnimator::animateValue(
float fraction)
88 onEvaluateValue(fraction);
90 for (
size_t i = 0; i < numListeners; ++i)
101bool SValueAnimator::doAnimationFrame(uint64_t frameTime)
145 uint64_t currentTime = smax(frameTime,
mStartTime);
146 bool finished = animateBasedOnTime(currentTime);
155bool SValueAnimator::isInitialized()
160void SValueAnimator::skipToEndValue(
bool inReverse)
162 float endFraction = inReverse ? 0.f : 1.f;
168 animateValue(endFraction);
171void SValueAnimator::animateBasedOnPlayTime(
long currentPlayTime,
long lastPlayTime,
bool inReverse)
173 if (currentPlayTime < 0 || lastPlayTime < 0)
180 int iteration = (int)(currentPlayTime /
mDuration);
181 int lastIteration = (int)(lastPlayTime /
mDuration);
187 if (iteration != lastIteration)
189 size_t numListeners = mListeners.GetCount();
190 for (
size_t i = 0; i < numListeners; ++i)
192 mListeners[i]->onAnimationRepeat(
this);
199 skipToEndValue(inReverse);
204 float fraction = currentPlayTime / (float)
mDuration;
205 fraction = getCurrentIterationFraction(fraction, inReverse);
206 animateValue(fraction);
210bool SValueAnimator::animateBasedOnTime(uint64_t currentTime)
215 long scaledDuration = getScaledDuration();
216 float fraction = scaledDuration > 0 ? (float)(currentTime -
mStartTime) / scaledDuration : 1.f;
218 bool newIteration = (int)fraction > (
int)lastFraction;
220 if (scaledDuration == 0)
225 else if (newIteration && !lastIterationFinished)
228 SArray<IAnimatorListener *> tmpListeners = mListeners;
229 for (UINT i = 0; i < tmpListeners.GetCount(); i++)
231 tmpListeners[i]->onAnimationRepeat(
this);
234 else if (lastIterationFinished)
240 animateValue(currentIterationFraction);
258bool SValueAnimator::isPulsingInternal()
263void SValueAnimator::startAnimation()
275 notifyStartListeners();
278void SValueAnimator::endAnimation()
284 removeAnimationCallback();
291 notifyStartListeners();
302 SArray<IAnimatorListener *> tmpListeners = mListeners;
303 size_t numListeners = tmpListeners.GetCount();
304 for (
size_t i = 0; i < numListeners; ++i)
306 tmpListeners[i]->onAnimationEnd(
this);
313bool SValueAnimator::canReverse()
320 if (isPulsingInternal())
323 uint64_t currentPlayTime = currentTime -
mStartTime;
324 uint64_t timeLeft = getScaledDuration() - currentPlayTime;
363void SValueAnimator::start(ITimelineHandlersMgr *pContainer)
369void SValueAnimator::start(
bool playBackwards)
396 addAnimationCallback();
418void SValueAnimator::notifyStartListeners()
421 SArray<IAnimatorListener *> tmpListeners = mListeners;
422 size_t numListeners = tmpListeners.GetCount();
423 for (
size_t i = 0; i < numListeners; ++i)
425 tmpListeners[i]->onAnimationStart(
this);
433 int iFind = mListeners.Find(p);
436 mListeners.RemoveAt(iFind);
520bool SValueAnimator::shouldPlayBackward(
int iteration,
bool inReverse)
528 return (iteration % 2) == 0;
532 return (iteration % 2) != 0;
541float SValueAnimator::clampFraction(
float fraction)
554float SValueAnimator::getCurrentIterationFraction(
float fraction,
bool inReverse)
556 fraction = clampFraction(fraction);
557 int iteration = getCurrentIteration(fraction);
558 float currentFraction = fraction - iteration;
559 return shouldPlayBackward(iteration, inReverse) ? 1.f - currentFraction : currentFraction;
562int SValueAnimator::getCurrentIteration(
float fraction)
564 fraction = clampFraction(fraction);
568 double iteration = floor(fraction);
569 if (fraction == iteration && fraction > 0)
573 return (
int)iteration;
578 fraction = clampFraction(fraction);
580 if (isPulsingInternal())
582 long seekTime = (long)(getScaledDuration() * fraction);
595 float currentIterationFraction = getCurrentIterationFraction(fraction,
mReversing);
596 animateValue(currentIterationFraction);
622long SValueAnimator::getScaledDuration()
646 COLORREF crStart = GETCOLOR(strValue);
652 COLORREF crEnd = GETCOLOR(strValue);
669 ani->removeListener(
this);
682 if (ani->isRunning())
684 else if (ani->isStarted())
689 ani->addListener(
this);
698 ani->removeListener(
this);
705 AnimatorStateMap::CPair *it =
m_lstAnimator.Lookup(pAnimator);
708 SSLOGW() <<
"onAnimationStart error, animator is not found!";
711 it->m_value = running;
716 AnimatorStateMap::CPair *it =
m_lstAnimator.Lookup(pAnimator);
719 SSLOGW() <<
"onAnimationStart error, animator is not found!";
723 bool bAllStop =
true;
Provides a simple timing engine for running animations which calculate animated values and set them o...
void onAnimationStart(IValueAnimator *pAnimator)
Called when an animation starts.
AniState
Enumeration of animation states.
AnimatorStateMap m_lstAnimator
Map of animators and their states.
SAnimatorGroup()
Constructor.
void onAnimationEnd(IValueAnimator *pAnimator)
Called when an animation ends.
IAnimatorGroupListerer * m_listener
Listener for the animator group.
void SetListener(IAnimatorGroupListerer *listener) OVERRIDE
Sets the listener for the animator group.
BOOL AddAnimator(IValueAnimator *ani) OVERRIDE
Adds an animator to the group.
BOOL RemoveAnimator(IValueAnimator *ani) OVERRIDE
Removes an animator from the group.
~SAnimatorGroup()
Destructor.
virtual IValueAnimator * CreateValueAnimatorByName(LPCWSTR pszName) const
Create a value animator by name.
HRESULT OnAttrTo(const SStringW &strValue, BOOL bLoading)
Handles the "valueTo" attribute.
HRESULT OnAttrFrom(const SStringW &strValue, BOOL bLoading)
Handles the "valueFrom" attribute.
An interpolator where the rate of change is constant.
LPCWSTR GetObjectClass() SCONST OVERRIDE
static SApplication * getSingletonPtr(void)
A class representing an ASCII string.
static uint64_t GetCurrentTimeMs()
获取当前时间的毫秒数
int getRepeatCount() SCONST OVERRIDE
Defines how many times the animation should repeat.
SAutoRefPtr< IInterpolator > mInterpolator
The time interpolator to be used.
ITimelineHandlersMgr * mContainer
The container managing the timeline handlers.
void setRepeatCount(int value) OVERRIDE
Sets how many times the animation should be repeated.
void addUpdateListener(IAnimatorUpdateListener *listener) OVERRIDE
Adds a listener to the set of listeners that are sent update events through the life of an animation.
float mCurrentFraction
Tracks current elapsed/eased fraction, for querying in getAnimatedFraction().
void commitAnimationFrame(long frameTime) OVERRIDE
Applies an adjustment to the animation to compensate for jank between when the animation first ran an...
BOOL isStarted() SCONST OVERRIDE
Checks if the animation has been started.
bool mStarted
Additional playing state to indicate whether an animator has been start()'d, whether or not there is ...
void setInterpolator(IInterpolator *value) OVERRIDE
Sets the time interpolator used in calculating the elapsed fraction of this animation.
void setStartDelay(long startDelay) OVERRIDE
Sets the amount of time, in milliseconds, to delay starting the animation after start() is called.
void setRepeatMode(RepeatMode value) OVERRIDE
Defines what this animation should do when it reaches the end.
bool mStartListenersCalled
Tracks whether we've notified listeners of the onAnimationStart() event.
uint64_t mLastFrameTime
Tracks the time (in milliseconds) when the last frame arrived.
long getTotalDuration() SCONST OVERRIDE
Gets the total duration of the animation, including any repetitions.
bool mAnimationEndRequested
Flag that tracks whether animation has been requested to end.
float mSeekFraction
Set when setCurrentPlayTime() is called.
void setDuration(long duration) OVERRIDE
Sets the length of the animation.
void setCurrentPlayTime(long playTime) OVERRIDE
Sets the position of the animation to the specified point in time.
void reverse() OVERRIDE
Plays the SValueAnimator in reverse.
void setCurrentFraction(float fraction) OVERRIDE
Sets the position of the animation to the specified fraction.
bool mReversing
Flag to indicate whether this animator is playing in reverse mode.
IInterpolator * getInterpolator() SCONST OVERRIDE
Returns the timing interpolator that this SValueAnimator uses.
float getAnimatedFraction() SCONST OVERRIDE
Returns the current animation fraction.
uint64_t mStartTime
The first time that the animation's animateFrame() method is called.
void end() OVERRIDE
Ends the animation.
uint64_t mFirstFrameTime
Tracks the time (in milliseconds) when the first frame arrived.
BOOL isRunning() SCONST OVERRIDE
Checks if the animation is running.
bool mRunning
Additional playing state to indicate whether an animator has been start()'d.
~SValueAnimator()
Destructor.
long mStartDelay
The amount of time in milliseconds to delay starting the animation after start() is called.
RepeatMode getRepeatMode() SCONST OVERRIDE
Defines what this animation should do when it reaches the end.
SValueAnimator()
Creates a new SValueAnimator object.
IValueAnimator * clone() SCONST OVERRIDE
Creates a clone of the current animation.
void addListener(IAnimatorListener *p) OVERRIDE
Adds an animator listener.
void copy(const IValueAnimator *pSrc) OVERRIDE
Copies the properties of another animation to this animation.
SArray< IAnimatorUpdateListener * > mUpdateListeners
The set of listeners to be sent update events through the life of an animation.
float mOverallFraction
Tracks the overall fraction of the animation, ranging from 0 to mRepeatCount + 1.
void removeListener(IAnimatorListener *p) OVERRIDE
Removes an animator listener.
long getStartDelay() SCONST OVERRIDE
Gets the amount of time, in milliseconds, to delay starting the animation after start() is called.
float sDurationScale
Scaling factor for the duration.
long getCurrentPlayTime() OVERRIDE
Gets the current position of the animation in time.
int mRepeatCount
The number of times the animation will repeat.
RepeatMode mRepeatMode
The type of repetition that will occur when repeatMode is nonzero.
long mDuration
How long the animation should last in milliseconds.
void removeUpdateListener(IAnimatorUpdateListener *listener) OVERRIDE
Removes a listener from the set listening to frame updates for this animation.
void removeAllUpdateListeners() OVERRIDE
Removes all listeners from the set listening to frame updates for this animation.
void OnNextFrame() OVERRIDE
Handles the next frame of the animation.
bool mInitialized
Flag that denotes whether the animation is set up and ready to go.
long getDuration() SCONST OVERRIDE
Gets the length of the animation.
bool mStartTimeCommitted
Flag indicating whether the start time has been firmly committed.
TypeEvaluator< COLORREF > mValueEvaluator