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_render_interactive_implict_animator.h"
17 #include "pipeline/rs_context.h"
18 
19 namespace OHOS {
20 namespace Rosen {
RSRenderInteractiveImplictAnimator(InteractiveImplictAnimatorId id,const std::weak_ptr<RSContext> & context)21 RSRenderInteractiveImplictAnimator::RSRenderInteractiveImplictAnimator(
22     InteractiveImplictAnimatorId id, const std::weak_ptr<RSContext>& context) : id_(id), context_(context)
23 {}
24 
AddAnimations(std::vector<std::pair<NodeId,AnimationId>> animations)25 void RSRenderInteractiveImplictAnimator::AddAnimations(std::vector<std::pair<NodeId, AnimationId>> animations)
26 {
27     auto context = context_.lock();
28     if (context == nullptr) {
29         return;
30     }
31     animations_.clear();
32     for (auto& [nodeId, animationId] : animations) {
33         auto node = context->GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
34         if (node == nullptr) {
35             continue;
36         }
37         auto animation = node->GetAnimationManager().GetAnimation(animationId);
38         if (animation == nullptr) {
39             continue;
40         }
41         animations_.emplace_back(nodeId, animationId);
42     }
43 }
44 
PauseAnimator()45 void RSRenderInteractiveImplictAnimator::PauseAnimator()
46 {
47     auto context = context_.lock();
48     if (context == nullptr) {
49         return;
50     }
51     for (auto& [nodeId, animationId] : animations_) {
52         auto node = context->GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
53         if (node == nullptr) {
54             continue;
55         }
56         auto animation = node->GetAnimationManager().GetAnimation(animationId);
57         if (animation == nullptr) {
58             continue;
59         }
60         animation->Pause();
61     }
62 }
63 
ContinueAnimator()64 void RSRenderInteractiveImplictAnimator::ContinueAnimator()
65 {
66     auto context = context_.lock();
67     if (context == nullptr) {
68         return;
69     }
70     for (auto& [nodeId, animationId] : animations_) {
71         auto node = context->GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
72         if (node == nullptr) {
73             continue;
74         }
75         auto animation = node->GetAnimationManager().GetAnimation(animationId);
76         if (animation == nullptr) {
77             return;
78         }
79         animation->Resume();
80         // register node on animation start or resume
81         context->RegisterAnimatingRenderNode(node);
82     }
83 }
84 
FinishAnimator(RSInteractiveAnimationPosition finishPos)85 void RSRenderInteractiveImplictAnimator::FinishAnimator(RSInteractiveAnimationPosition finishPos)
86 {
87     auto context = context_.lock();
88     if (context == nullptr) {
89         return;
90     }
91     for (auto& [nodeId, animationId] : animations_) {
92         auto node = context->GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
93         if (node == nullptr) {
94             continue;
95         }
96         auto animation = node->GetAnimationManager().GetAnimation(animationId);
97         if (animation == nullptr) {
98             return;
99         }
100         animation->FinishOnPosition(finishPos);
101     }
102 }
103 
ReverseAnimator()104 void RSRenderInteractiveImplictAnimator::ReverseAnimator()
105 {
106     auto context = context_.lock();
107     if (context == nullptr) {
108         return;
109     }
110     for (auto& [nodeId, animationId] : animations_) {
111         auto node = context->GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
112         if (node == nullptr) {
113             continue;
114         }
115         auto animation = node->GetAnimationManager().GetAnimation(animationId);
116         if (animation == nullptr) {
117             return;
118         }
119         animation->SetReversedAndContinue();
120         context->RegisterAnimatingRenderNode(node);
121     }
122 }
123 
SetFractionAnimator(float fraction)124 void RSRenderInteractiveImplictAnimator::SetFractionAnimator(float fraction)
125 {
126     auto context = context_.lock();
127     if (context == nullptr) {
128         return;
129     }
130     for (auto& [nodeId, animationId] : animations_) {
131         auto node = context->GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
132         if (node == nullptr) {
133             continue;
134         }
135         auto animation = node->GetAnimationManager().GetAnimation(animationId);
136         if (animation == nullptr) {
137             return;
138         }
139         animation->SetFraction(fraction);
140     }
141 }
142 
143 } // namespace Rosen
144 } // namespace OHOS
145