• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_property_animation.h"
17 
18 #include "animation/rs_animation_manager_map.h"
19 #include "animation/rs_render_animation.h"
20 #include "animation/rs_ui_animation_manager.h"
21 #include "modifier/rs_modifier.h"
22 #include "modifier/rs_property.h"
23 #include "ui/rs_node.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 
RSPropertyAnimation(std::shared_ptr<RSPropertyBase> property)28 RSPropertyAnimation::RSPropertyAnimation(std::shared_ptr<RSPropertyBase> property) : property_(property)
29 {
30     InitAdditiveMode();
31 }
32 
SetIsCustom(const bool isCustom)33 void RSPropertyAnimation::SetIsCustom(const bool isCustom)
34 {
35     isCustom_ = isCustom;
36 }
37 
SetAdditive(bool isAdditive)38 void RSPropertyAnimation::SetAdditive(bool isAdditive)
39 {
40     isAdditive_ = isAdditive;
41 }
42 
GetAdditive() const43 bool RSPropertyAnimation::GetAdditive() const
44 {
45     return isAdditive_;
46 }
47 
GetOriginValue() const48 const std::shared_ptr<RSPropertyBase> RSPropertyAnimation::GetOriginValue() const
49 {
50     return originValue_;
51 }
52 
SetPropertyValue(const std::shared_ptr<RSPropertyBase> & value)53 void RSPropertyAnimation::SetPropertyValue(const std::shared_ptr<RSPropertyBase>& value)
54 {
55     if (property_ != nullptr) {
56         property_->SetValue(value);
57     }
58 }
59 
GetPropertyValue() const60 const std::shared_ptr<RSPropertyBase> RSPropertyAnimation::GetPropertyValue() const
61 {
62     if (property_ != nullptr) {
63         return property_->Clone();
64     }
65 
66     return nullptr;
67 }
68 
GetPropertyId() const69 PropertyId RSPropertyAnimation::GetPropertyId() const
70 {
71     if (property_ != nullptr) {
72         return property_->GetId();
73     }
74 
75     return {};
76 }
77 
OnStart()78 void RSPropertyAnimation::OnStart()
79 {
80     if (!hasOriginValue_) {
81         originValue_ = GetPropertyValue();
82         hasOriginValue_ = true;
83     }
84     InitInterpolationValue();
85 }
86 
SetOriginValue(const std::shared_ptr<RSPropertyBase> & originValue)87 void RSPropertyAnimation::SetOriginValue(const std::shared_ptr<RSPropertyBase>& originValue)
88 {
89     if (!hasOriginValue_) {
90         originValue_ = originValue->Clone();
91         hasOriginValue_ = true;
92     }
93 }
94 
InitInterpolationValue()95 void RSPropertyAnimation::InitInterpolationValue()
96 {
97     if (isDelta_) {
98         startValue_ = originValue_->Clone();
99         endValue_ = originValue_ + byValue_;
100     } else {
101         byValue_ = endValue_ - startValue_;
102     }
103 }
104 
OnUpdateStagingValue(bool isFirstStart)105 void RSPropertyAnimation::OnUpdateStagingValue(bool isFirstStart)
106 {
107     auto startValue = startValue_;
108     auto endValue = endValue_;
109     if (!GetDirection()) {
110         std::swap(startValue, endValue);
111     }
112     auto byValue = endValue - startValue;
113     auto targetValue = endValue;
114     if (isFirstStart) {
115         if (GetAutoReverse() && GetRepeatCount() % 2 == 0) {
116             targetValue = startValue;
117         } else {
118             targetValue = endValue;
119         }
120     } else {
121         auto currentValue = GetPropertyValue();
122         if (GetAutoReverse() && GetRepeatCount() % 2 == 0) {
123             targetValue = IsReversed() ? currentValue + byValue : currentValue - byValue;
124         } else {
125             targetValue = IsReversed() ? currentValue - byValue : currentValue + byValue;
126         }
127     }
128 
129     SetPropertyValue(targetValue);
130 }
131 
StartCustomPropertyAnimation(const std::shared_ptr<RSRenderAnimation> & animation)132 void RSPropertyAnimation::StartCustomPropertyAnimation(const std::shared_ptr<RSRenderAnimation>& animation)
133 {
134     auto target = GetTarget().lock();
135     if (target == nullptr) {
136         ROSEN_LOGE("Failed to start custom animation, target is null!");
137         return;
138     }
139 
140     auto animationManager = RSAnimationManagerMap::Instance()->GetAnimationManager(gettid());
141     if (animationManager == nullptr) {
142         ROSEN_LOGE("Failed to start custom animation, UI animation manager is null!");
143         return;
144     }
145 
146     if (property_ == nullptr) {
147         return;
148     }
149 
150     auto renderProperty = animationManager->GetRenderProperty(property_->GetId());
151     if (renderProperty == nullptr) {
152         renderProperty = property_->CreateRenderProperty();
153     }
154     animationManager->AddAnimatableProp(property_->GetId(), property_, renderProperty);
155     animation->AttachRenderProperty(renderProperty);
156     animation->Start();
157     animationManager->AddAnimation(animation, shared_from_this());
158 }
159 
SetPropertyOnAllAnimationFinish()160 void RSPropertyAnimation::SetPropertyOnAllAnimationFinish()
161 {
162     if (property_ != nullptr) {
163         property_->UpdateOnAllAnimationFinish();
164     }
165 }
166 
InitAdditiveMode()167 void RSPropertyAnimation::InitAdditiveMode()
168 {
169     if (property_ == nullptr) {
170         return;
171     }
172 
173     switch (property_->type_) {
174         case RSModifierType::QUATERNION:
175             SetAdditive(false);
176             break;
177         default:
178             break;
179     }
180 }
181 } // namespace Rosen
182 } // namespace OHOS
183