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_OUT_3, // Variant of EASE_OUT that ends slower than EASE_OUT_2. 29 EASE_IN, // Slow in, fast out. 30 EASE_IN_2, // Variant of EASE_IN that starts out slower than 31 // EASE_IN. 32 EASE_IN_OUT, // Slow in and out, fast in the middle. 33 EASE_IN_OUT_2, // Variant of EASE_IN_OUT that starts and ends slower 34 // than EASE_IN_OUT. 35 SMOOTH_IN_OUT, // Smooth, consistent speeds in and out (sine wave). 36 FAST_OUT_SLOW_IN, // Variant of EASE_IN_OUT which should be used in most 37 // cases. 38 FAST_OUT_SLOW_IN_2, // Variant of FAST_OUT_SLOW_IN that starts out quicker. 39 LINEAR_OUT_SLOW_IN, // Variant of EASE_OUT which should be used for 40 // fading in from 0% or motion when entering a scene. 41 SLOW_OUT_LINEAR_IN, // Reverse of LINEAR_OUT_SLOW_IN which should be used 42 // in reverse animation to create a rubberband effect. 43 FAST_OUT_LINEAR_IN, // Variant of EASE_IN which should should be used for 44 // fading out to 0% or motion when exiting a scene. 45 ZERO, // Returns a value of 0 always. 46 }; 47 48 // Returns the value based on the tween type. |state| is from 0-1. 49 static double CalculateValue(Type type, double state); 50 51 // Conveniences for getting a value between a start and end point. 52 static SkColor ColorValueBetween(double value, SkColor start, SkColor target); 53 static double DoubleValueBetween(double value, double start, double target); 54 static float FloatValueBetween(double value, float start, float target); 55 static float ClampedFloatValueBetween(const base::TimeTicks& time, 56 const base::TimeTicks& start_time, 57 float start, 58 const base::TimeTicks& target_time, 59 float target); 60 61 // Interpolated between start and target, with every integer in this range 62 // given equal weight. 63 static int IntValueBetween(double value, int start, int target); 64 65 // Interpolates between start and target as real numbers, and rounds the 66 // result to the nearest integer, with ties broken by rounding towards 67 // positive infinity. This gives start and target half the weight of the 68 // other integers in the range. This is the integer interpolation approach 69 // specified by www.w3.org/TR/css3-transitions. 70 static int LinearIntValueBetween(double value, int start, int target); 71 72 // Interpolates between |start| and |target| rects, animating the rect corners 73 // (as opposed to animating the rect origin and size) to minimize rounding 74 // error accumulation at intermediate stages. 75 static gfx::Rect RectValueBetween(double value, 76 const gfx::Rect& start, 77 const gfx::Rect& target); 78 79 static gfx::RectF RectFValueBetween(double value, 80 const gfx::RectF& start, 81 const gfx::RectF& target); 82 83 static gfx::Transform TransformValueBetween(double value, 84 const gfx::Transform& start, 85 const gfx::Transform& target); 86 87 static gfx::Size SizeValueBetween(double value, 88 const gfx::Size& start, 89 const gfx::Size& target); 90 91 static gfx::SizeF SizeFValueBetween(double value, 92 const gfx::SizeF& start, 93 const gfx::SizeF& target); 94 95 private: 96 Tween(); 97 ~Tween(); 98 99 DISALLOW_COPY_AND_ASSIGN(Tween); 100 }; 101 102 } // namespace gfx 103 104 #endif // UI_GFX_ANIMATION_TWEEN_H_ 105