1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_IMPLICIT_ANIMATION_PARAM_H 17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_IMPLICIT_ANIMATION_PARAM_H 18 19 #include <functional> 20 #include <memory> 21 22 #include "animation/rs_animation_timing_curve.h" 23 #include "animation/rs_animation_timing_protocol.h" 24 #include "animation/rs_curve_animation.h" 25 #include "animation/rs_keyframe_animation.h" 26 #include "animation/rs_motion_path_option.h" 27 #include "animation/rs_path_animation.h" 28 #include "animation/rs_transition.h" 29 30 namespace OHOS { 31 namespace Rosen { 32 enum class ImplicitAnimationParamType { NONE, CURVE, KEYFRAME, PATH, SPRING, TRANSITION }; 33 34 class RSImplicitAnimationParam { 35 public: 36 explicit RSImplicitAnimationParam(const RSAnimationTimingProtocol& timingProtocol); 37 virtual ~RSImplicitAnimationParam() = default; 38 ImplicitAnimationParamType GetType() const; 39 40 template<typename T> CreateAnimation(const RSAnimatableProperty & property,const T & startValue,const T & endValue)41 std::shared_ptr<RSAnimation> CreateAnimation( 42 const RSAnimatableProperty& property, const T& startValue, const T& endValue) const 43 { 44 return nullptr; 45 } 46 47 protected: 48 void ApplyTimingProtocol(const std::shared_ptr<RSAnimation>& animation) const; 49 ImplicitAnimationParamType animationType_ { ImplicitAnimationParamType::NONE }; 50 51 private: 52 RSAnimationTimingProtocol timingProtocol_; 53 }; 54 55 class RSImplicitCurveAnimationParam : public RSImplicitAnimationParam { 56 public: 57 RSImplicitCurveAnimationParam( 58 const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve); 59 60 virtual ~RSImplicitCurveAnimationParam() = default; 61 62 template<typename T> CreateAnimation(const RSAnimatableProperty & property,const T & startValue,const T & endValue)63 std::shared_ptr<RSAnimation> CreateAnimation( 64 const RSAnimatableProperty& property, const T& startValue, const T& endValue) const 65 { 66 auto curveAnimation = std::make_shared<RSCurveAnimation<T>>(property, endValue - startValue); 67 curveAnimation->SetTimingCurve(timingCurve_); 68 ApplyTimingProtocol(curveAnimation); 69 return curveAnimation; 70 } 71 72 private: 73 RSAnimationTimingCurve timingCurve_; 74 }; 75 76 class RSImplicitKeyframeAnimationParam : public RSImplicitAnimationParam { 77 public: 78 RSImplicitKeyframeAnimationParam( 79 const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve, float fraction); 80 81 virtual ~RSImplicitKeyframeAnimationParam() = default; 82 83 template<typename T> CreateAnimation(const RSAnimatableProperty & property,const T & startValue,const T & endValue)84 std::shared_ptr<RSAnimation> CreateAnimation( 85 const RSAnimatableProperty& property, const T& startValue, const T& endValue) const 86 { 87 auto keyFrameAnimation = std::make_shared<RSKeyframeAnimation<T>>(property); 88 keyFrameAnimation->AddKeyFrame(fraction_, endValue, timingCurve_); 89 keyFrameAnimation->SetOriginValue(startValue); 90 ApplyTimingProtocol(keyFrameAnimation); 91 return keyFrameAnimation; 92 } 93 94 template<typename T> AddKeyframe(std::shared_ptr<RSAnimation> & animation,const T & startValue,const T & endValue)95 void AddKeyframe(std::shared_ptr<RSAnimation>& animation, const T& startValue, const T& endValue) const 96 { 97 if (animation == nullptr) { 98 return; 99 } 100 101 auto keyframeAnimation = std::static_pointer_cast<RSKeyframeAnimation<T>>(animation); 102 if (keyframeAnimation != nullptr) { 103 keyframeAnimation->AddKeyFrame(fraction_, endValue, timingCurve_); 104 } 105 } 106 107 private: 108 RSAnimationTimingCurve timingCurve_; 109 float fraction_; 110 }; 111 112 class RSImplicitPathAnimationParam : public RSImplicitAnimationParam { 113 public: 114 RSImplicitPathAnimationParam(const RSAnimationTimingProtocol& timingProtocol, 115 const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<RSMotionPathOption>& motionPathOption); 116 117 virtual ~RSImplicitPathAnimationParam() = default; 118 119 template<typename T> CreateAnimation(const RSAnimatableProperty & property,const T & startValue,const T & endValue)120 std::shared_ptr<RSAnimation> CreateAnimation( 121 const RSAnimatableProperty& property, const T& startValue, const T& endValue) const 122 { 123 return nullptr; 124 } 125 126 std::shared_ptr<RSAnimation> CreateAnimation( 127 const RSAnimatableProperty& property, const Vector2f& startValue, const Vector2f& endValue) const; 128 129 private: 130 RSAnimationTimingCurve timingCurve_; 131 std::shared_ptr<RSMotionPathOption> motionPathOption_; 132 }; 133 134 class RSImplicitTransitionParam : public RSImplicitAnimationParam { 135 public: 136 RSImplicitTransitionParam(const RSAnimationTimingProtocol& timingProtocol, 137 const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<const RSTransitionEffect>& effect); 138 virtual ~RSImplicitTransitionParam() = default; 139 140 std::shared_ptr<RSAnimation> CreateAnimation(bool isTransitionIn); 141 142 private: 143 RSAnimationTimingCurve timingCurve_; 144 const std::shared_ptr<const RSTransitionEffect> effect_; 145 }; 146 } // namespace Rosen 147 } // namespace OHOS 148 149 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_IMPLICIT_ANIMATION_PARAM_H 150