1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef UI_GFX_ANIMATION_TWEEN_H_ 6 #define UI_GFX_ANIMATION_TWEEN_H_ 7 8 #include "base/macros.h" 9 #include "third_party/skia/include/core/SkColor.h" 10 #include "ui/gfx/animation/animation_export.h" 11 #include "ui/gfx/geometry/rect.h" 12 #include "ui/gfx/geometry/rect_f.h" 13 #include "ui/gfx/geometry/size_f.h" 14 #include "ui/gfx/transform.h" 15 16 namespace base { 17 class TimeTicks; 18 } 19 20 namespace gfx { 21 22 class ANIMATION_EXPORT Tween { 23 public: 24 enum Type { 25 LINEAR, // Linear. 26 EASE_OUT, // Fast in, slow out (default). 27 EASE_OUT_2, // Variant of EASE_OUT that ends slower than EASE_OUT. 28 EASE_IN, // Slow in, fast out. 29 EASE_IN_2, // Variant of EASE_IN that starts out slower than 30 // EASE_IN. 31 EASE_IN_OUT, // Slow in and out, fast in the middle. 32 EASE_IN_OUT_2, // Variant of EASE_IN_OUT that starts and ends slower 33 // than EASE_IN_OUT. 34 SMOOTH_IN_OUT, // Smooth, consistent speeds in and out (sine wave). 35 FAST_OUT_SLOW_IN, // Variant of EASE_IN_OUT which should be used in most 36 // cases. 37 FAST_OUT_SLOW_IN_2, // Variant of FAST_OUT_SLOW_IN that starts out quicker. 38 LINEAR_OUT_SLOW_IN, // Variant of EASE_OUT which should be used for 39 // fading in from 0% or motion when entering a scene. 40 SLOW_OUT_LINEAR_IN, // Reverse of LINEAR_OUT_SLOW_IN which should be used 41 // in reverse animation to create a rubberband effect. 42 FAST_OUT_LINEAR_IN, // Variant of EASE_IN which should should be used for 43 // fading out to 0% or motion when exiting a scene. 44 ZERO, // Returns a value of 0 always. 45 }; 46 47 // Returns the value based on the tween type. |state| is from 0-1. 48 static double CalculateValue(Type type, double state); 49 50 // Conveniences for getting a value between a start and end point. 51 static SkColor ColorValueBetween(double value, SkColor start, SkColor target); 52 static double DoubleValueBetween(double value, double start, double target); 53 static float FloatValueBetween(double value, float start, float target); 54 static float ClampedFloatValueBetween(const base::TimeTicks& time, 55 const base::TimeTicks& start_time, 56 float start, 57 const base::TimeTicks& target_time, 58 float target); 59 60 // Interpolated between start and target, with every integer in this range 61 // given equal weight. 62 static int IntValueBetween(double value, int start, int target); 63 64 // Interpolates between start and target as real numbers, and rounds the 65 // result to the nearest integer, with ties broken by rounding towards 66 // positive infinity. This gives start and target half the weight of the 67 // other integers in the range. This is the integer interpolation approach 68 // specified by www.w3.org/TR/css3-transitions. 69 static int LinearIntValueBetween(double value, int start, int target); 70 71 // Interpolates between |start| and |target| rects, animating the rect corners 72 // (as opposed to animating the rect origin and size) to minimize rounding 73 // error accumulation at intermediate stages. 74 static gfx::Rect RectValueBetween(double value, 75 const gfx::Rect& start, 76 const gfx::Rect& target); 77 78 static gfx::RectF RectFValueBetween(double value, 79 const gfx::RectF& start, 80 const gfx::RectF& target); 81 82 static gfx::Transform TransformValueBetween(double value, 83 const gfx::Transform& start, 84 const gfx::Transform& target); 85 86 static gfx::Size SizeValueBetween(double value, 87 const gfx::Size& start, 88 const gfx::Size& target); 89 90 static gfx::SizeF SizeFValueBetween(double value, 91 const gfx::SizeF& start, 92 const gfx::SizeF& target); 93 94 private: 95 Tween(); 96 ~Tween(); 97 98 DISALLOW_COPY_AND_ASSIGN(Tween); 99 }; 100 101 } // namespace gfx 102 103 #endif // UI_GFX_ANIMATION_TWEEN_H_ 104