1 // Copyright 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 CC_ANIMATION_TIMING_FUNCTION_H_ 6 #define CC_ANIMATION_TIMING_FUNCTION_H_ 7 8 #include <memory> 9 10 #include "cc/animation/animation_export.h" 11 #include "ui/gfx/geometry/cubic_bezier.h" 12 13 namespace cc { 14 15 // See http://www.w3.org/TR/css3-transitions/. 16 class CC_ANIMATION_EXPORT TimingFunction { 17 public: 18 virtual ~TimingFunction(); 19 20 TimingFunction& operator=(const TimingFunction&) = delete; 21 22 // Note that LINEAR is a nullptr TimingFunction (for now). 23 enum class Type { LINEAR, CUBIC_BEZIER, STEPS }; 24 25 // Which limit to apply at a discontinuous boundary. 26 enum class LimitDirection { LEFT, RIGHT }; 27 28 virtual Type GetType() const = 0; 29 virtual double GetValue(double t) const = 0; 30 virtual double Velocity(double time) const = 0; 31 virtual std::unique_ptr<TimingFunction> Clone() const = 0; 32 33 protected: 34 TimingFunction(); 35 }; 36 37 class CC_ANIMATION_EXPORT CubicBezierTimingFunction : public TimingFunction { 38 public: 39 enum class EaseType { EASE, EASE_IN, EASE_OUT, EASE_IN_OUT, CUSTOM }; 40 41 static std::unique_ptr<CubicBezierTimingFunction> CreatePreset( 42 EaseType ease_type); 43 static std::unique_ptr<CubicBezierTimingFunction> Create(double x1, 44 double y1, 45 double x2, 46 double y2); 47 ~CubicBezierTimingFunction() override; 48 49 CubicBezierTimingFunction& operator=(const CubicBezierTimingFunction&) = 50 delete; 51 52 // TimingFunction implementation. 53 Type GetType() const override; 54 double GetValue(double time) const override; 55 double Velocity(double time) const override; 56 std::unique_ptr<TimingFunction> Clone() const override; 57 ease_type()58 EaseType ease_type() const { return ease_type_; } bezier()59 const gfx::CubicBezier& bezier() const { return bezier_; } 60 61 private: 62 CubicBezierTimingFunction(EaseType ease_type, 63 double x1, 64 double y1, 65 double x2, 66 double y2); 67 68 gfx::CubicBezier bezier_; 69 EaseType ease_type_; 70 }; 71 72 class CC_ANIMATION_EXPORT StepsTimingFunction : public TimingFunction { 73 public: 74 // step-timing-function values 75 // https://drafts.csswg.org/css-easing-1/#typedef-step-timing-function 76 enum class StepPosition { 77 START, // Discontinuity at progress = 0. 78 // Alias for jump-start. Maintaining a separate enumerated value 79 // for serialization. 80 END, // Discontinuity at progress = 1. 81 // Alias for jump-end. Maintaining a separate enumerated value 82 // for serialization. 83 JUMP_BOTH, // Discontinuities at progress = 0 and 1. 84 JUMP_END, // Discontinuity at progress = 1. 85 JUMP_NONE, // Continuous at progress = 0 and 1. 86 JUMP_START // Discontinuity at progress = 0. 87 }; 88 89 static std::unique_ptr<StepsTimingFunction> Create( 90 int steps, 91 StepPosition step_position); 92 ~StepsTimingFunction() override; 93 94 StepsTimingFunction& operator=(const StepsTimingFunction&) = delete; 95 96 // TimingFunction implementation. 97 Type GetType() const override; 98 double GetValue(double t) const override; 99 std::unique_ptr<TimingFunction> Clone() const override; 100 double Velocity(double time) const override; 101 steps()102 int steps() const { return steps_; } step_position()103 StepPosition step_position() const { return step_position_; } 104 double GetPreciseValue(double t, LimitDirection limit_direction) const; 105 106 private: 107 StepsTimingFunction(int steps, StepPosition step_position); 108 109 // The number of jumps is the number of discontinuities in the timing 110 // function. There is a subtle distinction between the number of steps and 111 // jumps. The number of steps is the number of intervals in the timing 112 // function. The number of jumps differs from the number of steps when either 113 // both or neither end point has a discontinuity. 114 // https://drafts.csswg.org/css-easing-1/#step-easing-functions 115 int NumberOfJumps() const; 116 117 float GetStepsStartOffset() const; 118 119 int steps_; 120 StepPosition step_position_; 121 }; 122 123 class CC_ANIMATION_EXPORT LinearTimingFunction : public TimingFunction { 124 public: 125 static std::unique_ptr<LinearTimingFunction> Create(); 126 ~LinearTimingFunction() override; 127 128 // TimingFunction implementation. 129 Type GetType() const override; 130 double GetValue(double t) const override; 131 std::unique_ptr<TimingFunction> Clone() const override; 132 double Velocity(double time) const override; 133 134 private: 135 LinearTimingFunction(); 136 }; 137 138 } // namespace cc 139 140 #endif // CC_ANIMATION_TIMING_FUNCTION_H_ 141