• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_interpolating_spring_animation.h"
20 #include "animation/rs_keyframe_animation.h"
21 #include "animation/rs_motion_path_option.h"
22 #include "animation/rs_path_animation.h"
23 #include "animation/rs_spring_animation.h"
24 #include "animation/rs_transition.h"
25 #include "modifier/rs_extended_modifier.h"
26 #include "platform/common/rs_log.h"
27 
28 namespace OHOS {
29 namespace Rosen {
RSImplicitAnimationParam(const RSAnimationTimingProtocol & timingProtocol)30 RSImplicitAnimationParam::RSImplicitAnimationParam(const RSAnimationTimingProtocol& timingProtocol)
31     : timingProtocol_(timingProtocol)
32 {}
33 
GetType() const34 ImplicitAnimationParamType RSImplicitAnimationParam::GetType() const
35 {
36     return animationType_;
37 }
38 
ApplyTimingProtocol(const std::shared_ptr<RSAnimation> & animation) const39 void RSImplicitAnimationParam::ApplyTimingProtocol(const std::shared_ptr<RSAnimation>& animation) const
40 {
41     animation->SetDuration(timingProtocol_.GetDuration());
42     animation->SetStartDelay(timingProtocol_.GetStartDelay());
43     animation->SetSpeed(timingProtocol_.GetSpeed());
44     animation->SetDirection(timingProtocol_.GetDirection());
45     animation->SetAutoReverse(timingProtocol_.GetAutoReverse());
46     animation->SetRepeatCount(timingProtocol_.GetRepeatCount());
47     animation->SetFillMode(timingProtocol_.GetFillMode());
48     auto range = timingProtocol_.GetFrameRateRange();
49     if (range.IsValid()) {
50         animation->SetFrameRateRange(range);
51     }
52 }
53 
RSImplicitCurveAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)54 RSImplicitCurveAnimationParam::RSImplicitCurveAnimationParam(
55     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
56     : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve)
57 {
58     animationType_ = ImplicitAnimationParamType::CURVE;
59 }
60 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const61 std::shared_ptr<RSAnimation> RSImplicitCurveAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
62     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
63 {
64     auto curveAnimation = std::make_shared<RSCurveAnimation>(property, endValue - startValue);
65     curveAnimation->SetTimingCurve(timingCurve_);
66     curveAnimation->SetIsCustom(property->GetIsCustom());
67     ApplyTimingProtocol(curveAnimation);
68     return curveAnimation;
69 }
70 
RSImplicitKeyframeAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,float fraction)71 RSImplicitKeyframeAnimationParam::RSImplicitKeyframeAnimationParam(
72     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve, float fraction)
73     : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve), fraction_(fraction)
74 {
75     animationType_ = ImplicitAnimationParamType::KEYFRAME;
76 }
77 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const78 std::shared_ptr<RSAnimation> RSImplicitKeyframeAnimationParam::CreateAnimation(
79     std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& startValue,
80     const std::shared_ptr<RSPropertyBase>& endValue) const
81 {
82     auto keyFrameAnimation = std::make_shared<RSKeyframeAnimation>(property);
83     keyFrameAnimation->AddKeyFrame(fraction_, endValue, timingCurve_);
84     keyFrameAnimation->SetOriginValue(startValue);
85     keyFrameAnimation->SetIsCustom(property->GetIsCustom());
86     ApplyTimingProtocol(keyFrameAnimation);
87     return keyFrameAnimation;
88 }
89 
AddKeyframe(std::shared_ptr<RSAnimation> & animation,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const90 void RSImplicitKeyframeAnimationParam::AddKeyframe(std::shared_ptr<RSAnimation>& animation,
91     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
92 {
93     if (animation == nullptr) {
94         return;
95     }
96 
97     auto keyframeAnimation = std::static_pointer_cast<RSKeyframeAnimation>(animation);
98     if (keyframeAnimation != nullptr) {
99         keyframeAnimation->AddKeyFrame(fraction_, endValue, timingCurve_);
100     }
101 }
102 
RSImplicitPathAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::shared_ptr<RSMotionPathOption> & motionPathOption)103 RSImplicitPathAnimationParam::RSImplicitPathAnimationParam(const RSAnimationTimingProtocol& timingProtocol,
104     const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<RSMotionPathOption>& motionPathOption)
105     : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve), motionPathOption_(motionPathOption)
106 {
107     animationType_ = ImplicitAnimationParamType::PATH;
108 }
109 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const110 std::shared_ptr<RSAnimation> RSImplicitPathAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
111         const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
112 {
113     if (motionPathOption_ == nullptr) {
114         ROSEN_LOGE("Failed to create path animation, motion path option is null!");
115         return nullptr;
116     }
117 
118     auto pathAnimation =
119         std::make_shared<RSPathAnimation>(property, motionPathOption_->GetPath(), startValue, endValue);
120     pathAnimation->SetBeginFraction(motionPathOption_->GetBeginFraction());
121     pathAnimation->SetEndFraction(motionPathOption_->GetEndFraction());
122     pathAnimation->SetRotationMode(motionPathOption_->GetRotationMode());
123     pathAnimation->SetPathNeedAddOrigin(motionPathOption_->GetPathNeedAddOrigin());
124     pathAnimation->SetTimingCurve(timingCurve_);
125     ApplyTimingProtocol(pathAnimation);
126     return pathAnimation;
127 }
128 
RSImplicitSpringAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)129 RSImplicitSpringAnimationParam::RSImplicitSpringAnimationParam(
130     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
131     : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve)
132 {
133     animationType_ = ImplicitAnimationParamType::SPRING;
134 }
135 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const136 std::shared_ptr<RSAnimation> RSImplicitSpringAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
137     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
138 {
139     auto springAnimation = std::make_shared<RSSpringAnimation>(property, startValue, endValue);
140     springAnimation->SetTimingCurve(timingCurve_);
141     springAnimation->SetIsCustom(property->GetIsCustom());
142     ApplyTimingProtocol(springAnimation);
143     return springAnimation;
144 }
145 
RSImplicitInterpolatingSpringAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)146 RSImplicitInterpolatingSpringAnimationParam::RSImplicitInterpolatingSpringAnimationParam(
147     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
148     : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve)
149 {
150     animationType_ = ImplicitAnimationParamType::INTERPOLATING_SPRING;
151 }
152 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const153 std::shared_ptr<RSAnimation> RSImplicitInterpolatingSpringAnimationParam::CreateAnimation(
154     std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& startValue,
155     const std::shared_ptr<RSPropertyBase>& endValue) const
156 {
157     auto interpolatingSpringAnimation =
158         std::make_shared<RSInterpolatingSpringAnimation>(property, startValue, endValue);
159     interpolatingSpringAnimation->SetTimingCurve(timingCurve_);
160     interpolatingSpringAnimation->SetIsCustom(property->GetIsCustom());
161     ApplyTimingProtocol(interpolatingSpringAnimation);
162     return interpolatingSpringAnimation;
163 }
164 
RSImplicitTransitionParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::shared_ptr<const RSTransitionEffect> & effect,bool isTransitionIn)165 RSImplicitTransitionParam::RSImplicitTransitionParam(const RSAnimationTimingProtocol& timingProtocol,
166     const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<const RSTransitionEffect>& effect,
167     bool isTransitionIn)
168     : RSImplicitAnimationParam(timingProtocol), timingCurve_(timingCurve),
169       isTransitionIn_(isTransitionIn), effect_(effect)
170 {
171     animationType_ = ImplicitAnimationParamType::TRANSITION;
172 }
173 
CreateAnimation()174 std::shared_ptr<RSAnimation> RSImplicitTransitionParam::CreateAnimation()
175 {
176     if (transition_ == nullptr) {
177         transition_ = std::make_shared<RSTransition>(effect_, isTransitionIn_);
178         transition_->SetTimingCurve(timingCurve_);
179         ApplyTimingProtocol(transition_);
180     }
181     return transition_;
182 }
183 
CreateAnimation(const std::shared_ptr<RSPropertyBase> & property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue)184 std::shared_ptr<RSAnimation> RSImplicitTransitionParam::CreateAnimation(const std::shared_ptr<RSPropertyBase>& property,
185     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue)
186 {
187     auto& customEffects = isTransitionIn_ ? effect_->customTransitionInEffects_ : effect_->customTransitionOutEffects_;
188     for (auto& customEffect : customEffects) {
189         if (property->modifier_.lock() == std::static_pointer_cast<RSModifier>(customEffect->modifier_)) {
190             customEffect->Custom(property, startValue, endValue);
191             break;
192         }
193     }
194     if (transition_ == nullptr) {
195         transition_ = std::make_shared<RSTransition>(effect_, isTransitionIn_);
196         transition_->SetTimingCurve(timingCurve_);
197         transition_->SetIsCustom(true);
198         ApplyTimingProtocol(transition_);
199     }
200     return transition_;
201 }
202 } // namespace Rosen
203 } // namespace OHOS
204