diff options
-rw-r--r-- | cc/animation/animation_curve.cc | 12 | ||||
-rw-r--r-- | cc/animation/animation_curve.h | 6 | ||||
-rw-r--r-- | cc/animation/scroll_offset_animation_curve.cc | 61 | ||||
-rw-r--r-- | cc/animation/scroll_offset_animation_curve.h | 52 | ||||
-rw-r--r-- | cc/animation/scroll_offset_animation_curve_unittest.cc | 80 | ||||
-rw-r--r-- | cc/cc.gyp | 2 | ||||
-rw-r--r-- | cc/cc_tests.gyp | 1 | ||||
-rw-r--r-- | cc/test/geometry_test_utils.h | 6 |
8 files changed, 219 insertions, 1 deletions
diff --git a/cc/animation/animation_curve.cc b/cc/animation/animation_curve.cc index 65c21db..3acff5d 100644 --- a/cc/animation/animation_curve.cc +++ b/cc/animation/animation_curve.cc @@ -5,6 +5,7 @@ #include "cc/animation/animation_curve.h" #include "base/logging.h" +#include "cc/animation/scroll_offset_animation_curve.h" namespace cc { @@ -43,4 +44,15 @@ AnimationCurve::CurveType FilterAnimationCurve::Type() const { return Filter; } +const ScrollOffsetAnimationCurve* AnimationCurve::ToScrollOffsetAnimationCurve() + const { + DCHECK(Type() == AnimationCurve::ScrollOffset); + return static_cast<const ScrollOffsetAnimationCurve*>(this); +} + +ScrollOffsetAnimationCurve* AnimationCurve::ToScrollOffsetAnimationCurve() { + DCHECK(Type() == AnimationCurve::ScrollOffset); + return static_cast<ScrollOffsetAnimationCurve*>(this); +} + } // namespace cc diff --git a/cc/animation/animation_curve.h b/cc/animation/animation_curve.h index f84bb1a..9bcccba 100644 --- a/cc/animation/animation_curve.h +++ b/cc/animation/animation_curve.h @@ -19,13 +19,14 @@ namespace cc { class ColorAnimationCurve; class FilterAnimationCurve; class FloatAnimationCurve; +class ScrollOffsetAnimationCurve; class TransformAnimationCurve; class TransformOperations; // An animation curve is a function that returns a value given a time. class CC_EXPORT AnimationCurve { public: - enum CurveType { Color, Float, Transform, Filter }; + enum CurveType { Color, Float, Transform, Filter, ScrollOffset }; virtual ~AnimationCurve() {} @@ -37,6 +38,9 @@ class CC_EXPORT AnimationCurve { const FloatAnimationCurve* ToFloatAnimationCurve() const; const TransformAnimationCurve* ToTransformAnimationCurve() const; const FilterAnimationCurve* ToFilterAnimationCurve() const; + const ScrollOffsetAnimationCurve* ToScrollOffsetAnimationCurve() const; + + ScrollOffsetAnimationCurve* ToScrollOffsetAnimationCurve(); }; class CC_EXPORT ColorAnimationCurve : public AnimationCurve { diff --git a/cc/animation/scroll_offset_animation_curve.cc b/cc/animation/scroll_offset_animation_curve.cc new file mode 100644 index 0000000..d5cfd1c --- /dev/null +++ b/cc/animation/scroll_offset_animation_curve.cc @@ -0,0 +1,61 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cc/animation/scroll_offset_animation_curve.h" + +#include "base/logging.h" +#include "cc/animation/timing_function.h" +#include "ui/gfx/animation/tween.h" + +namespace cc { + +scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create( + gfx::Vector2dF target_value, + scoped_ptr<TimingFunction> timing_function) { + return make_scoped_ptr( + new ScrollOffsetAnimationCurve(target_value, timing_function.Pass())); +} + +ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve( + gfx::Vector2dF target_value, + scoped_ptr<TimingFunction> timing_function) + : target_value_(target_value), + duration_(0.0), + timing_function_(timing_function.Pass()) {} + +ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} + +gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const { + if (t <= 0) + return initial_value_; + + if (t >= duration_) + return target_value_; + + double progress = timing_function_->GetValue(t / duration_); + return gfx::Vector2dF(gfx::Tween::FloatValueBetween( + progress, initial_value_.x(), target_value_.x()), + gfx::Tween::FloatValueBetween( + progress, initial_value_.y(), target_value_.y())); +} + +double ScrollOffsetAnimationCurve::Duration() const { + return duration_; +} + +AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { + return ScrollOffset; +} + +scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { + scoped_ptr<TimingFunction> timing_function( + static_cast<TimingFunction*>(timing_function_->Clone().release())); + scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = + Create(target_value_, timing_function.Pass()); + curve_clone->set_initial_value(initial_value_); + curve_clone->set_duration(duration_); + return curve_clone.PassAs<AnimationCurve>(); +} + +} // namespace cc diff --git a/cc/animation/scroll_offset_animation_curve.h b/cc/animation/scroll_offset_animation_curve.h new file mode 100644 index 0000000..ede9b65c5 --- /dev/null +++ b/cc/animation/scroll_offset_animation_curve.h @@ -0,0 +1,52 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CC_ANIMATION_SCROLL_OFFSET_ANIMATION_CURVE_H_ +#define CC_ANIMATION_SCROLL_OFFSET_ANIMATION_CURVE_H_ + +#include "base/memory/scoped_ptr.h" +#include "cc/animation/animation_curve.h" +#include "cc/base/cc_export.h" + +namespace cc { + +class TimingFunction; + +class CC_EXPORT ScrollOffsetAnimationCurve : public AnimationCurve { + public: + static scoped_ptr<ScrollOffsetAnimationCurve> Create( + gfx::Vector2dF target_value, + scoped_ptr<TimingFunction> timing_function); + + virtual ~ScrollOffsetAnimationCurve(); + + void set_duration(double duration) { duration_ = duration; } + + void set_initial_value(gfx::Vector2dF initial_value) { + initial_value_ = initial_value; + } + + virtual gfx::Vector2dF GetValue(double t) const; + + // AnimationCurve implementation + virtual double Duration() const OVERRIDE; + virtual CurveType Type() const OVERRIDE; + virtual scoped_ptr<AnimationCurve> Clone() const OVERRIDE; + + private: + ScrollOffsetAnimationCurve(gfx::Vector2dF target_value, + scoped_ptr <TimingFunction> timing_function); + + gfx::Vector2dF initial_value_; + gfx::Vector2dF target_value_; + double duration_; + + scoped_ptr<TimingFunction> timing_function_; + + DISALLOW_COPY_AND_ASSIGN(ScrollOffsetAnimationCurve); +}; + +} // namespace cc + +#endif // CC_ANIMATION_SCROLL_OFFSET_ANIMATION_CURVE_H_ diff --git a/cc/animation/scroll_offset_animation_curve_unittest.cc b/cc/animation/scroll_offset_animation_curve_unittest.cc new file mode 100644 index 0000000..40b0be5 --- /dev/null +++ b/cc/animation/scroll_offset_animation_curve_unittest.cc @@ -0,0 +1,80 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cc/animation/scroll_offset_animation_curve.h" + +#include "cc/animation/timing_function.h" +#include "cc/test/geometry_test_utils.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace cc { +namespace { + +TEST(ScrollOffsetAnimationCurveTest, GetValue) { + gfx::Vector2dF initial_value(2.f, 40.f); + gfx::Vector2dF target_value(10.f, 20.f); + double duration = 10.0; + scoped_ptr<ScrollOffsetAnimationCurve> curve( + ScrollOffsetAnimationCurve::Create( + target_value, + EaseInOutTimingFunction::Create().Pass())); + curve->set_duration(duration); + curve->set_initial_value(initial_value); + + EXPECT_EQ(AnimationCurve::ScrollOffset, curve->Type()); + EXPECT_EQ(duration, curve->Duration()); + + EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(-1.0)); + EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(0.0)); + EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(6.f, 30.f), curve->GetValue(duration/2.0)); + EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration)); + EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration+1.0)); + + // Verify that GetValue takes the timing function into account. + gfx::Vector2dF value = curve->GetValue(duration/4.0); + EXPECT_NEAR(3.0333f, value.x(), 0.00015f); + EXPECT_NEAR(37.4168f, value.y(), 0.00015f); +} + +// Verify that a clone behaves exactly like the original. +TEST(ScrollOffsetAnimationCurveTest, Clone) { + gfx::Vector2dF initial_value(2.f, 40.f); + gfx::Vector2dF target_value(10.f, 20.f); + double duration = 10.0; + scoped_ptr<ScrollOffsetAnimationCurve> curve( + ScrollOffsetAnimationCurve::Create( + target_value, + EaseInOutTimingFunction::Create().Pass())); + curve->set_duration(duration); + curve->set_initial_value(initial_value); + + scoped_ptr<AnimationCurve> clone(curve->Clone().Pass()); + + EXPECT_EQ(AnimationCurve::ScrollOffset, clone->Type()); + EXPECT_EQ(duration, clone->Duration()); + + EXPECT_VECTOR2DF_EQ(initial_value, + clone->ToScrollOffsetAnimationCurve()->GetValue(-1.0)); + EXPECT_VECTOR2DF_EQ(initial_value, + clone->ToScrollOffsetAnimationCurve()->GetValue(0.0)); + EXPECT_VECTOR2DF_EQ( + gfx::Vector2dF(6.f, 30.f), + clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 2.0)); + EXPECT_VECTOR2DF_EQ( + target_value, + clone->ToScrollOffsetAnimationCurve()->GetValue(duration)); + EXPECT_VECTOR2DF_EQ( + target_value, + clone->ToScrollOffsetAnimationCurve()->GetValue(duration + 1.0)); + + // Verify that the timing function was cloned correctly. + gfx::Vector2dF value = + clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 4.0); + EXPECT_NEAR(3.0333f, value.x(), 0.00015f); + EXPECT_NEAR(37.4168f, value.y(), 0.00015f); +} + + +} // namespace +} // namespace cc @@ -42,6 +42,8 @@ 'animation/layer_animation_controller.h', 'animation/layer_animation_event_observer.h', 'animation/layer_animation_value_observer.h', + 'animation/scroll_offset_animation_curve.cc', + 'animation/scroll_offset_animation_curve.h', 'animation/scrollbar_animation_controller.h', 'animation/scrollbar_animation_controller_linear_fade.cc', 'animation/scrollbar_animation_controller_linear_fade.h', diff --git a/cc/cc_tests.gyp b/cc/cc_tests.gyp index 4683294..1699577 100644 --- a/cc/cc_tests.gyp +++ b/cc/cc_tests.gyp @@ -9,6 +9,7 @@ 'animation/animation_unittest.cc', 'animation/keyframed_animation_curve_unittest.cc', 'animation/layer_animation_controller_unittest.cc', + 'animation/scroll_offset_animation_curve_unittest.cc', 'animation/scrollbar_animation_controller_linear_fade_unittest.cc', 'animation/scrollbar_animation_controller_thinning_unittest.cc', 'animation/timing_function_unittest.cc', diff --git a/cc/test/geometry_test_utils.h b/cc/test/geometry_test_utils.h index cc0d7b9..12ca18d 100644 --- a/cc/test/geometry_test_utils.h +++ b/cc/test/geometry_test_utils.h @@ -62,6 +62,12 @@ do { \ EXPECT_EQ((expected).y(), (actual).y()); \ } while (false) +#define EXPECT_VECTOR2DF_EQ(expected, actual) \ +do { \ + EXPECT_FLOAT_EQ((expected).x(), (actual).x()); \ + EXPECT_FLOAT_EQ((expected).y(), (actual).y()); \ +} while (false) + #define EXPECT_FLOAT_ARRAY_EQ(expected, actual, count) \ do { \ for (int i = 0; i < count; i++) { \ |