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 #include "animation/rs_implicit_animation_param.h"
17
18 #include "animation/rs_curve_animation.h"
19 #include "animation/rs_keyframe_animation.h"
20 #include "animation/rs_motion_path_option.h"
21 #include "animation/rs_path_animation.h"
22 #include "animation/rs_spring_animation.h"
23 #include "animation/rs_transition.h"
24 #include "platform/common/rs_log.h"
25
26 namespace OHOS {
27 namespace Rosen {
RSImplicitAnimationParam(const RSAnimationTimingProtocol & timingProtocol)28 RSImplicitAnimationParam::RSImplicitAnimationParam(const RSAnimationTimingProtocol& timingProtocol)
29 : timingProtocol_(timingProtocol)
30 {}
31
GetType() const32 ImplicitAnimationParamType RSImplicitAnimationParam::GetType() const
33 {
34 return animationType_;
35 }
36
ApplyTimingProtocol(const std::shared_ptr<RSAnimation> & animation) const37 void RSImplicitAnimationParam::ApplyTimingProtocol(const std::shared_ptr<RSAnimation>& animation) const
38 {
39 animation->SetDuration(timingProtocol_.GetDuration());
40 animation->SetStartDelay(timingProtocol_.GetStartDelay());
41 animation->SetSpeed(timingProtocol_.GetSpeed());
42 animation->SetDirection(timingProtocol_.GetDirection());
43 animation->SetAutoReverse(timingProtocol_.GetAutoReverse());
44 animation->SetRepeatCount(timingProtocol_.GetRepeatCount());
45 animation->SetFillMode(timingProtocol_.GetFillMode());
46 }
47
RSImplicitCurveAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)48 RSImplicitCurveAnimationParam::RSImplicitCurveAnimationParam(
49 const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
50 : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve)
51 {
52 animationType_ = ImplicitAnimationParamType::CURVE;
53 }
54
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const55 std::shared_ptr<RSAnimation> RSImplicitCurveAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
56 const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
57 {
58 auto curveAnimation = std::make_shared<RSCurveAnimation>(property, endValue - startValue);
59 curveAnimation->SetTimingCurve(timingCurve_);
60 curveAnimation->SetIsCustom(property->GetIsCustom());
61 ApplyTimingProtocol(curveAnimation);
62 return curveAnimation;
63 }
64
RSImplicitKeyframeAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,float fraction)65 RSImplicitKeyframeAnimationParam::RSImplicitKeyframeAnimationParam(
66 const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve, float fraction)
67 : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve), fraction_(fraction)
68 {
69 animationType_ = ImplicitAnimationParamType::KEYFRAME;
70 }
71
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const72 std::shared_ptr<RSAnimation> RSImplicitKeyframeAnimationParam::CreateAnimation(
73 std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& startValue,
74 const std::shared_ptr<RSPropertyBase>& endValue) const
75 {
76 auto keyFrameAnimation = std::make_shared<RSKeyframeAnimation>(property);
77 keyFrameAnimation->AddKeyFrame(fraction_, endValue, timingCurve_);
78 keyFrameAnimation->SetOriginValue(startValue);
79 keyFrameAnimation->SetIsCustom(property->GetIsCustom());
80 ApplyTimingProtocol(keyFrameAnimation);
81 return keyFrameAnimation;
82 }
83
AddKeyframe(std::shared_ptr<RSAnimation> & animation,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const84 void RSImplicitKeyframeAnimationParam::AddKeyframe(std::shared_ptr<RSAnimation>& animation,
85 const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
86 {
87 if (animation == nullptr) {
88 return;
89 }
90
91 auto keyframeAnimation = std::static_pointer_cast<RSKeyframeAnimation>(animation);
92 if (keyframeAnimation != nullptr) {
93 keyframeAnimation->AddKeyFrame(fraction_, endValue, timingCurve_);
94 }
95 }
96
RSImplicitPathAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::shared_ptr<RSMotionPathOption> & motionPathOption)97 RSImplicitPathAnimationParam::RSImplicitPathAnimationParam(const RSAnimationTimingProtocol& timingProtocol,
98 const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<RSMotionPathOption>& motionPathOption)
99 : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve), motionPathOption_(motionPathOption)
100 {
101 animationType_ = ImplicitAnimationParamType::PATH;
102 }
103
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const104 std::shared_ptr<RSAnimation> RSImplicitPathAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
105 const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
106 {
107 if (motionPathOption_ == nullptr) {
108 ROSEN_LOGE("Failed to create path animation, motion path option is null!");
109 return nullptr;
110 }
111
112 auto pathAnimation =
113 std::make_shared<RSPathAnimation>(property, motionPathOption_->GetPath(), startValue, endValue);
114 pathAnimation->SetBeginFraction(motionPathOption_->GetBeginFraction());
115 pathAnimation->SetEndFraction(motionPathOption_->GetEndFraction());
116 pathAnimation->SetRotationMode(motionPathOption_->GetRotationMode());
117 pathAnimation->SetPathNeedAddOrigin(motionPathOption_->GetPathNeedAddOrigin());
118 pathAnimation->SetTimingCurve(timingCurve_);
119 ApplyTimingProtocol(pathAnimation);
120 return pathAnimation;
121 }
122
RSImplicitSpringAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)123 RSImplicitSpringAnimationParam::RSImplicitSpringAnimationParam(
124 const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
125 : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve)
126 {
127 animationType_ = ImplicitAnimationParamType::SPRING;
128 }
129
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const130 std::shared_ptr<RSAnimation> RSImplicitSpringAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
131 const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
132 {
133 auto springAnimation = std::make_shared<RSSpringAnimation>(property, startValue, endValue);
134 springAnimation->SetTimingCurve(timingCurve_);
135 springAnimation->SetIsCustom(property->GetIsCustom());
136 ApplyTimingProtocol(springAnimation);
137 return springAnimation;
138 }
139
RSImplicitTransitionParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::shared_ptr<const RSTransitionEffect> & effect)140 RSImplicitTransitionParam::RSImplicitTransitionParam(const RSAnimationTimingProtocol& timingProtocol,
141 const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<const RSTransitionEffect>& effect)
142 : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve), effect_(effect)
143 {
144 animationType_ = ImplicitAnimationParamType::TRANSITION;
145 }
146
CreateAnimation(bool appearing)147 std::shared_ptr<RSAnimation> RSImplicitTransitionParam::CreateAnimation(bool appearing)
148 {
149 auto transition = std::make_shared<RSTransition>(effect_, appearing);
150 transition->SetTimingCurve(timingCurve_);
151 ApplyTimingProtocol(transition);
152 return transition;
153 }
154 } // namespace Rosen
155 } // namespace OHOS
156