Files
android_development/ndk/sources/android/helper/interpolator.cpp
Hak Matsuda 5d1c91fb75 cleaned code, added pinch gesture detector, better opengl context handling, fixed issues
Change-Id: I26a28374ae74391204586b4584d03cf0c58772c2
2013-10-28 11:41:58 +08:00

170 lines
4.6 KiB
C++

/*
* Copyright 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "interpolator.h"
#include <math.h>
#include "interpolator.h"
//-------------------------------------------------
//Ctor
//-------------------------------------------------
interpolator::interpolator()
{
m_listParams.clear();
}
//-------------------------------------------------
//Dtor
//-------------------------------------------------
interpolator::~interpolator()
{
m_listParams.clear();
}
void interpolator::clear()
{
m_listParams.clear();
}
interpolator& interpolator::set(float start,float dest, INTERPOLATOR_TYPE type, double duration)
{
//init the parameters for the interpolation process
_dStartTime = perfMonitor::getCurrentTime();
_dDestTime = _dStartTime + duration;
_type = type;
_fStartValue = start;
_fDestValue = dest;
return *this;
}
interpolator& interpolator::add(const float dest,
INTERPOLATOR_TYPE type, double duration)
{
interpolatorParam param;
param.fDestValue = dest;
param.type = type;
param.dDuration = duration;
m_listParams.push_back( param );
return *this;
}
bool interpolator::update( const double currentTime, float& p )
{
bool bContinue;
if( currentTime >= _dDestTime )
{
p = _fDestValue;
if( m_listParams.size () )
{
interpolatorParam& item = m_listParams.front();
set(_fDestValue, item.fDestValue, item.type, item.dDuration );
m_listParams.pop_front();
bContinue = true;
}
else
{
bContinue = false;
}
}
else
{
float t = (float)(currentTime - _dStartTime);
float d = (float)(_dDestTime - _dStartTime);
float b = _fStartValue;
float c = _fDestValue - _fStartValue;
p = getFormula(_type, t, b, d, c);
bContinue = true;
}
return bContinue;
}
float interpolator::getFormula(INTERPOLATOR_TYPE type, float t, float b, float d, float c)
{
float t1;
switch( type )
{
case INTERPOLATOR_TYPE_LINEAR:
// simple linear interpolation - no easing
return (c * t / d + b);
case INTERPOLATOR_TYPE_EASEINQUAD:
// quadratic (t^2) easing in - accelerating from zero velocity
t1 = t / d;
return (c * t1 * t1 + b);
case INTERPOLATOR_TYPE_EASEOUTQUAD:
// quadratic (t^2) easing out - decelerating to zero velocity
t1 = t / d;
return (-c * t1 * (t1-2) + b);
case INTERPOLATOR_TYPE_EASEINOUTQUAD:
// quadratic easing in/out - acceleration until halfway, then deceleration
t1 = t / d / 2;
if (t1 < 1)
return ( c/2 * t1 * t1 + b);
else
{
t1 = t1 -1;
return (-c/2 * (t1 * (t1-2) - 1) + b);
}
case INTERPOLATOR_TYPE_EASEINCUBIC:
// cubic easing in - accelerating from zero velocity
t1 = t / d;
return (c * t1 * t1 * t1 + b);
case INTERPOLATOR_TYPE_EASEOUTCUBIC:
// cubic easing in - accelerating from zero velocity
t1 = t / d - 1;
return (c * (t1 * t1 * t1 + 1) + b);
case INTERPOLATOR_TYPE_EASEINOUTCUBIC:
// cubic easing in - accelerating from zero velocity
t1 = t / d / 2;
if ( t1 < 1)
return (c/2 * t1 * t1 * t1 + b);
else
{
t1 -= 2;
return (c/2 * (t1 * t1 * t1 + 2 ) + b);
}
case INTERPOLATOR_TYPE_EASEINQUART:
// quartic easing in - accelerating from zero velocity
t1 = t / d;
return (c * t1 * t1 * t1 * t1 + b);
case INTERPOLATOR_TYPE_EASEINEXPO:
// exponential (2^t) easing in - accelerating from zero velocity
if (t==0)
return b;
else
return (c*powf(2,(10*(t/d-1)))+b);
case INTERPOLATOR_TYPE_EASEOUTEXPO:
// exponential (2^t) easing out - decelerating to zero velocity
if (t==d)
return (b+c);
else
return (c * (-powf(2,-10*t/d)+1)+b);
default:
return 0;
}
}