• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_animation_group.h"
17 
18 #include "animation/rs_animation_callback.h"
19 #include "platform/common/rs_log.h"
20 #include "ui/rs_node.h"
21 
22 namespace OHOS {
23 namespace Rosen {
AddAnimation(const std::shared_ptr<RSAnimation> & animation)24 void RSAnimationGroup::AddAnimation(const std::shared_ptr<RSAnimation>& animation)
25 {
26     if (animation == nullptr) {
27         ROSEN_LOGE("Failed to add animation, adding animation is null!");
28         return;
29     }
30 
31     if (IsStarted()) {
32         ROSEN_LOGE("Failed to add animation, group animation has started!");
33         return;
34     }
35 
36     if (animation->IsStarted()) {
37         ROSEN_LOGE("Failed to add animation, adding animation has started!");
38         return;
39     }
40 
41     auto iter = find(animations_.begin(), animations_.end(), animation);
42     if (iter != animations_.end()) {
43         ROSEN_LOGE("Failed to add animation, animation already exists!");
44         return;
45     }
46 
47     animations_.emplace_back(animation);
48 }
49 
RemoveAnimation(const std::shared_ptr<RSAnimation> & animation)50 void RSAnimationGroup::RemoveAnimation(const std::shared_ptr<RSAnimation>& animation)
51 {
52     if (animation == nullptr) {
53         ROSEN_LOGE("Failed to remove animation, animation is null!");
54         return;
55     }
56 
57     if (IsStarted()) {
58         ROSEN_LOGE("Failed to remove animation, group animation has started!");
59         return;
60     }
61 
62     if (animation->IsStarted()) {
63         ROSEN_LOGE("Failed to remove animation, removing animation has started!");
64         return;
65     }
66 
67     auto iter = find(animations_.begin(), animations_.end(), animation);
68     if (iter == animations_.end()) {
69         ROSEN_LOGE("Failed to remove animation, animation not exists!");
70         return;
71     }
72 
73     animations_.erase(iter);
74 }
75 
OnStart()76 void RSAnimationGroup::OnStart()
77 {
78     if (animations_.empty()) {
79         ROSEN_LOGD("Failed to start animations, animations is empty!");
80         return;
81     }
82 
83     auto target = target_.lock();
84     if (target == nullptr) {
85         ROSEN_LOGE("Failed to start group animation, target is null!");
86         return;
87     }
88 
89     std::shared_ptr<AnimationFinishCallback> finishCallback =
90         std::make_shared<AnimationFinishCallback>([weakTarget = target_, weakAnimation = weak_from_this()]() {
91             auto target = weakTarget.lock();
92             if (target == nullptr) {
93                 ROSEN_LOGE("Failed to finish group animation, target is null!");
94                 return;
95             }
96 
97             auto animation = weakAnimation.lock();
98             if (animation == nullptr) {
99                 ROSEN_LOGE("Failed to finish group animation, group animation is null!");
100                 return;
101             }
102 
103             target->RemoveAnimation(animation);
104         });
105 
106     for (auto& animation : animations_) {
107         int duration = duration_ < animation->GetDuration() ? duration_ : animation->GetDuration();
108         int startDelay = startDelay_ + animation->GetStartDelay();
109 
110         animation->SetDuration(duration);
111         animation->SetStartDelay(startDelay);
112         animation->SetFinishCallback(finishCallback);
113         animation->Start(target);
114     }
115 }
116 
OnPause()117 void RSAnimationGroup::OnPause()
118 {
119     if (animations_.empty()) {
120         ROSEN_LOGE("Failed to pause animation, group animation is empty!");
121         return;
122     }
123 
124     for (auto& animation : animations_) {
125         animation->Pause();
126     }
127 }
128 
OnResume()129 void RSAnimationGroup::OnResume()
130 {
131     if (animations_.empty()) {
132         ROSEN_LOGE("Failed to resume animation, group animation is empty!");
133         return;
134     }
135 
136     for (auto& animation : animations_) {
137         animation->Resume();
138     }
139 }
140 
OnFinish()141 void RSAnimationGroup::OnFinish()
142 {
143     if (animations_.empty()) {
144         ROSEN_LOGE("Failed to finish animation, group animation is empty!");
145         return;
146     }
147 
148     for (auto& animation : animations_) {
149         animation->Finish();
150     }
151 }
152 
OnReverse()153 void RSAnimationGroup::OnReverse()
154 {
155     if (animations_.empty()) {
156         ROSEN_LOGE("Failed to reverse animation, group animation is empty!");
157         return;
158     }
159 
160     for (auto& animation : animations_) {
161         animation->Reverse();
162     }
163 }
164 
OnSetFraction(float fraction)165 void RSAnimationGroup::OnSetFraction(float fraction)
166 {
167     if (animations_.empty()) {
168         ROSEN_LOGE("Failed to set fraction, group animation is empty!");
169         return;
170     }
171 
172     for (auto& animation : animations_) {
173         animation->SetFraction(fraction);
174     }
175 }
176 
GetPropertyId() const177 PropertyId RSAnimationGroup::GetPropertyId() const
178 {
179     return 0;
180 }
181 } // namespace Rosen
182 } // namespace OHOS
183