CCActiveAnimation.cpp [plain text]
#include "config.h"
#include "cc/CCActiveAnimation.h"
#include "cc/CCAnimationCurve.h"
#include <cmath>
namespace WebCore {
PassOwnPtr<CCActiveAnimation> CCActiveAnimation::create(PassOwnPtr<CCAnimationCurve> curve, int animationId, int groupId, TargetProperty targetProperty)
{
return adoptPtr(new CCActiveAnimation(curve, animationId, groupId, targetProperty));
}
CCActiveAnimation::CCActiveAnimation(PassOwnPtr<CCAnimationCurve> curve, int animationId, int groupId, TargetProperty targetProperty)
: m_curve(curve)
, m_id(animationId)
, m_group(groupId)
, m_targetProperty(targetProperty)
, m_runState(WaitingForTargetAvailability)
, m_iterations(1)
, m_startTime(0)
, m_timeOffset(0)
, m_needsSynchronizedStartTime(false)
, m_suspended(false)
, m_pauseTime(0)
, m_totalPausedTime(0)
{
}
CCActiveAnimation::~CCActiveAnimation()
{
}
void CCActiveAnimation::setRunState(RunState runState, double monotonicTime)
{
if (m_suspended)
return;
if (runState == Running && m_runState == Paused)
m_totalPausedTime += monotonicTime - m_pauseTime;
else if (runState == Paused)
m_pauseTime = monotonicTime;
m_runState = runState;
}
void CCActiveAnimation::suspend(double monotonicTime)
{
setRunState(Paused, monotonicTime);
m_suspended = true;
}
void CCActiveAnimation::resume(double monotonicTime)
{
m_suspended = false;
setRunState(Running, monotonicTime);
}
bool CCActiveAnimation::isFinishedAt(double monotonicTime) const
{
if (m_runState == Finished || m_runState == Aborted)
return true;
if (m_needsSynchronizedStartTime)
return false;
return m_runState == Running
&& m_iterations >= 0
&& m_iterations * m_curve->duration() <= monotonicTime - startTime() - m_totalPausedTime;
}
double CCActiveAnimation::trimTimeToCurrentIteration(double monotonicTime) const
{
double trimmed = monotonicTime + m_timeOffset;
if (m_runState == Paused)
trimmed = m_pauseTime;
trimmed -= m_startTime + m_totalPausedTime;
if (trimmed <= 0)
return 0;
if (!m_iterations)
return 0;
if (trimmed < m_curve->duration())
return trimmed;
if (m_iterations >= 0 && trimmed >= m_curve->duration() * m_iterations)
return m_curve->duration();
return fmod(trimmed, m_curve->duration());
}
PassOwnPtr<CCActiveAnimation> CCActiveAnimation::cloneForImplThread() const
{
OwnPtr<CCActiveAnimation> toReturn(adoptPtr(new CCActiveAnimation(m_curve->clone(), m_id, m_group, m_targetProperty)));
toReturn->m_runState = m_runState;
toReturn->m_iterations = m_iterations;
toReturn->m_startTime = m_startTime;
toReturn->m_pauseTime = m_pauseTime;
toReturn->m_totalPausedTime = m_totalPausedTime;
toReturn->m_timeOffset = m_timeOffset;
return toReturn.release();
}
void CCActiveAnimation::pushPropertiesTo(CCActiveAnimation* other) const
{
if (m_runState == CCActiveAnimation::Paused || other->m_runState == CCActiveAnimation::Paused) {
other->m_runState = m_runState;
other->m_pauseTime = m_pauseTime;
other->m_totalPausedTime = m_totalPausedTime;
}
}
}