This source file includes following definitions.
- position
- velocity
- timeAtVelocity
- Create
- apply
#include "content/child/touch_fling_gesture_curve.h"
#include <cmath>
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "third_party/WebKit/public/platform/WebFloatPoint.h"
#include "third_party/WebKit/public/platform/WebFloatSize.h"
#include "third_party/WebKit/public/platform/WebGestureCurve.h"
#include "third_party/WebKit/public/platform/WebGestureCurveTarget.h"
#include "third_party/WebKit/public/platform/WebSize.h"
using blink::WebFloatPoint;
using blink::WebFloatSize;
using blink::WebGestureCurve;
using blink::WebGestureCurveTarget;
using blink::WebSize;
namespace {
const char* kCurveName = "TouchFlingGestureCurve";
inline double position(double t, float* p) {
return p[0] * exp(-p[2] * t) - p[1] * t - p[0];
}
inline double velocity(double t, float* p) {
return -p[0] * p[2] * exp(-p[2] * t) - p[1];
}
inline double timeAtVelocity(double v, float* p) {
DCHECK(p[0]);
DCHECK(p[2]);
return -log((v + p[1]) / (-p[0] * p[2])) / p[2];
}
}
namespace content {
WebGestureCurve* TouchFlingGestureCurve::Create(
const WebFloatPoint& initial_velocity,
float p0,
float p1,
float p2,
const WebSize& cumulative_scroll) {
return new TouchFlingGestureCurve(initial_velocity, p0, p1, p2,
cumulative_scroll);
}
TouchFlingGestureCurve::TouchFlingGestureCurve(
const WebFloatPoint& initial_velocity,
float alpha,
float beta,
float gamma,
const WebSize& cumulative_scroll)
: cumulative_scroll_(WebFloatSize(cumulative_scroll.width,
cumulative_scroll.height)) {
DCHECK(initial_velocity != WebFloatPoint());
coefficients_[0] = alpha;
coefficients_[1] = beta;
coefficients_[2] = gamma;
curve_duration_ = timeAtVelocity(0, coefficients_);
DCHECK(curve_duration_ > 0);
float max_start_velocity = std::max(fabs(initial_velocity.x),
fabs(initial_velocity.y));
if (max_start_velocity > velocity(0, coefficients_))
max_start_velocity = velocity(0, coefficients_);
if (max_start_velocity < 0)
max_start_velocity = 0;
displacement_ratio_ = WebFloatPoint(initial_velocity.x / max_start_velocity,
initial_velocity.y / max_start_velocity);
time_offset_ = timeAtVelocity(max_start_velocity, coefficients_);
position_offset_ = position(time_offset_, coefficients_);
TRACE_EVENT_ASYNC_BEGIN1("input", "GestureAnimation", this, "curve",
kCurveName);
}
TouchFlingGestureCurve::~TouchFlingGestureCurve() {
TRACE_EVENT_ASYNC_END0("input", "GestureAnimation", this);
}
bool TouchFlingGestureCurve::apply(double time, WebGestureCurveTarget* target) {
float displacement;
float speed;
if (time < 0) {
displacement = 0.f;
speed = 0.f;
} else if (time + time_offset_ < curve_duration_) {
displacement =
position(time + time_offset_, coefficients_) - position_offset_;
speed = velocity(time + time_offset_, coefficients_);
} else {
displacement = position(curve_duration_, coefficients_) - position_offset_;
speed = 0.f;
}
WebFloatSize scroll(displacement * displacement_ratio_.x,
displacement * displacement_ratio_.y);
WebFloatSize scroll_increment(scroll.width - cumulative_scroll_.width,
scroll.height - cumulative_scroll_.height);
WebFloatSize scroll_velocity(speed * displacement_ratio_.x,
speed * displacement_ratio_.y);
cumulative_scroll_ = scroll;
if (time + time_offset_ < curve_duration_ ||
scroll_increment != WebFloatSize()) {
return target->scrollBy(scroll_increment, scroll_velocity);
}
return false;
}
}