This source file includes following definitions.
- NormalizeFromViewport
- DenormalizeToViewport
- InterpolateBetween
- Create
- timing_function_
- ZoomTo
- ZoomWithAnchor
- InferTargetScrollOffsetFromStartAnchor
- InferTargetAnchorFromScrollOffsets
- ClampTargetScrollOffset
- StartViewportSize
- TargetViewportSize
- ViewportSizeAt
- IsAnimationStarted
- StartAnimation
- ScrollOffsetAtTime
- PageScaleFactorAtTime
- IsAnimationCompleteAtTime
- InterpAtTime
- ScrollOffsetAt
- AnchorAt
- ViewportRelativeAnchorAt
- PageScaleFactorAt
#include "cc/input/page_scale_animation.h"
#include <math.h>
#include "base/logging.h"
#include "cc/animation/timing_function.h"
#include "ui/gfx/point_f.h"
#include "ui/gfx/rect_f.h"
#include "ui/gfx/vector2d_conversions.h"
namespace {
gfx::Vector2dF NormalizeFromViewport(const gfx::Vector2dF& denormalized,
const gfx::SizeF& viewport_size) {
return gfx::ScaleVector2d(denormalized,
1.f / viewport_size.width(),
1.f / viewport_size.height());
}
gfx::Vector2dF DenormalizeToViewport(const gfx::Vector2dF& normalized,
const gfx::SizeF& viewport_size) {
return gfx::ScaleVector2d(normalized,
viewport_size.width(),
viewport_size.height());
}
gfx::Vector2dF InterpolateBetween(const gfx::Vector2dF& start,
const gfx::Vector2dF& end,
float interp) {
return start + gfx::ScaleVector2d(end - start, interp);
}
}
namespace cc {
scoped_ptr<PageScaleAnimation> PageScaleAnimation::Create(
const gfx::Vector2dF& start_scroll_offset,
float start_page_scale_factor,
const gfx::SizeF& viewport_size,
const gfx::SizeF& root_layer_size,
scoped_ptr<TimingFunction> timing_function) {
return make_scoped_ptr(new PageScaleAnimation(start_scroll_offset,
start_page_scale_factor,
viewport_size,
root_layer_size,
timing_function.Pass()));
}
PageScaleAnimation::PageScaleAnimation(
const gfx::Vector2dF& start_scroll_offset,
float start_page_scale_factor,
const gfx::SizeF& viewport_size,
const gfx::SizeF& root_layer_size,
scoped_ptr<TimingFunction> timing_function)
: start_page_scale_factor_(start_page_scale_factor),
target_page_scale_factor_(0.f),
start_scroll_offset_(start_scroll_offset),
start_anchor_(),
target_anchor_(),
viewport_size_(viewport_size),
root_layer_size_(root_layer_size),
start_time_(-1.0),
duration_(0.0),
timing_function_(timing_function.Pass()) {}
PageScaleAnimation::~PageScaleAnimation() {}
void PageScaleAnimation::ZoomTo(const gfx::Vector2dF& target_scroll_offset,
float target_page_scale_factor,
double duration) {
target_page_scale_factor_ = target_page_scale_factor;
target_scroll_offset_ = target_scroll_offset;
ClampTargetScrollOffset();
duration_ = duration;
if (start_page_scale_factor_ == target_page_scale_factor) {
start_anchor_ = start_scroll_offset_;
target_anchor_ = target_scroll_offset;
return;
}
InferTargetAnchorFromScrollOffsets();
start_anchor_ = target_anchor_;
}
void PageScaleAnimation::ZoomWithAnchor(const gfx::Vector2dF& anchor,
float target_page_scale_factor,
double duration) {
start_anchor_ = anchor;
target_page_scale_factor_ = target_page_scale_factor;
duration_ = duration;
InferTargetScrollOffsetFromStartAnchor();
ClampTargetScrollOffset();
if (start_page_scale_factor_ == target_page_scale_factor_) {
target_anchor_ = start_anchor_;
return;
}
InferTargetAnchorFromScrollOffsets();
}
void PageScaleAnimation::InferTargetScrollOffsetFromStartAnchor() {
gfx::Vector2dF normalized = NormalizeFromViewport(
start_anchor_ - start_scroll_offset_, StartViewportSize());
target_scroll_offset_ =
start_anchor_ - DenormalizeToViewport(normalized, TargetViewportSize());
}
void PageScaleAnimation::InferTargetAnchorFromScrollOffsets() {
float width_scale =
1.f / (TargetViewportSize().width() - StartViewportSize().width());
float height_scale =
1.f / (TargetViewportSize().height() - StartViewportSize().height());
gfx::Vector2dF normalized = gfx::ScaleVector2d(
start_scroll_offset_ - target_scroll_offset_, width_scale, height_scale);
target_anchor_ =
target_scroll_offset_ + DenormalizeToViewport(normalized,
TargetViewportSize());
}
void PageScaleAnimation::ClampTargetScrollOffset() {
gfx::Vector2dF max_scroll_offset =
gfx::RectF(root_layer_size_).bottom_right() -
gfx::RectF(TargetViewportSize()).bottom_right();
target_scroll_offset_.SetToMax(gfx::Vector2dF());
target_scroll_offset_.SetToMin(max_scroll_offset);
}
gfx::SizeF PageScaleAnimation::StartViewportSize() const {
return gfx::ScaleSize(viewport_size_, 1.f / start_page_scale_factor_);
}
gfx::SizeF PageScaleAnimation::TargetViewportSize() const {
return gfx::ScaleSize(viewport_size_, 1.f / target_page_scale_factor_);
}
gfx::SizeF PageScaleAnimation::ViewportSizeAt(float interp) const {
return gfx::ScaleSize(viewport_size_, 1.f / PageScaleFactorAt(interp));
}
bool PageScaleAnimation::IsAnimationStarted() const {
return start_time_ >= 0;
}
void PageScaleAnimation::StartAnimation(double time) {
DCHECK_GT(0, start_time_);
start_time_ = time;
}
gfx::Vector2dF PageScaleAnimation::ScrollOffsetAtTime(double time) const {
DCHECK_GE(start_time_, 0);
return ScrollOffsetAt(InterpAtTime(time));
}
float PageScaleAnimation::PageScaleFactorAtTime(double time) const {
DCHECK_GE(start_time_, 0);
return PageScaleFactorAt(InterpAtTime(time));
}
bool PageScaleAnimation::IsAnimationCompleteAtTime(double time) const {
DCHECK_GE(start_time_, 0);
return time >= end_time();
}
float PageScaleAnimation::InterpAtTime(double time) const {
DCHECK_GE(start_time_, 0);
DCHECK_GE(time, start_time_);
if (IsAnimationCompleteAtTime(time))
return 1.f;
const double normalized_time = (time - start_time_) / duration_;
return timing_function_->GetValue(normalized_time);
}
gfx::Vector2dF PageScaleAnimation::ScrollOffsetAt(float interp) const {
if (interp <= 0.f)
return start_scroll_offset_;
if (interp >= 1.f)
return target_scroll_offset_;
return AnchorAt(interp) - ViewportRelativeAnchorAt(interp);
}
gfx::Vector2dF PageScaleAnimation::AnchorAt(float interp) const {
return InterpolateBetween(start_anchor_, target_anchor_, interp);
}
gfx::Vector2dF PageScaleAnimation::ViewportRelativeAnchorAt(
float interp) const {
gfx::Vector2dF start_normalized =
NormalizeFromViewport(start_anchor_ - start_scroll_offset_,
StartViewportSize());
gfx::Vector2dF target_normalized =
NormalizeFromViewport(target_anchor_ - target_scroll_offset_,
TargetViewportSize());
gfx::Vector2dF interp_normalized =
InterpolateBetween(start_normalized, target_normalized, interp);
return DenormalizeToViewport(interp_normalized, ViewportSizeAt(interp));
}
float PageScaleAnimation::PageScaleFactorAt(float interp) const {
if (interp <= 0.f)
return start_page_scale_factor_;
if (interp >= 1.f)
return target_page_scale_factor_;
float diff = target_page_scale_factor_ / start_page_scale_factor_;
float log_diff = log(diff);
log_diff *= interp;
diff = exp(log_diff);
return start_page_scale_factor_ * diff;
}
}