1 /*
2  * Copyright (c) 2024 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 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_INTERACTIVE_IMPLICT_ANIMATOR_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_INTERACTIVE_IMPLICT_ANIMATOR_H
18 
19 #include <vector>
20 
21 #include "animation/rs_animation.h"
22 #include "animation/rs_animation_timing_curve.h"
23 #include "animation/rs_animation_timing_protocol.h"
24 #include "common/rs_macros.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 
29 enum class RSInteractiveAnimationState {
30     INACTIVE,
31     ACTIVE,
32     RUNNING,
33     PAUSED,
34 };
35 
36 class RSC_EXPORT RSInteractiveImplictAnimator : public std::enable_shared_from_this<RSInteractiveImplictAnimator> {
37 public:
38     virtual ~RSInteractiveImplictAnimator();
39     static std::shared_ptr<RSInteractiveImplictAnimator> Create(
40         const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve);
41 
42     /*
43      * @brief add animations form callback
44      * @param callback use property set change or RSNode::Animate to create aniamtion
45      *                 property set use animator protocal and curve create animation
46      *                 RSNode::Animate use self Animate protocal and curve crate aniamtion
47      *
48      */
49     size_t AddImplictAnimation(std::function<void()> callback);
50 
51     /*
52      * @brief add animations form callback
53      * @param callback use RSNode::Animate to create aniamtion
54      *                 just use RSNode::Animate can create animation
55      *                 RSNode::Animate use self Animate protocal and curve crate aniamtion
56      *
57      */
58     size_t AddAnimation(std::function<void()> callback);
59 
60     // aniamtor operation
61     int32_t StartAnimation();
62     void PauseAnimation();
63     void ContinueAnimation();
64 
65     /*
66      * @brief finsih all animation in animator
67      * @param position position is START or CURRENT or END
68      *                  START all animaton in animator finish on startvalue and sync stagevalue
69      *                  CURRENT all animaton in animator finish on currentvalue and sync stagevalue
70      *                  START all animaton in animator finish on endvalue and sync stagevalue
71      */
72     void FinishAnimation(RSInteractiveAnimationPosition position);
73     void ReverseAnimation();
74 
75     /*
76      * @brief set value fraction for all animations
77      *          but just support curve animation and interpolating spring aniamtion
78      *          curve animation not support step interpolater and custom interpolater
79      * @param fraction animation value faraction
80      */
81     void SetFraction(float fraction);
82     float GetFraction();
83 
GetStatus()84     RSInteractiveAnimationState GetStatus() const
85     {
86         return state_;
87     }
88 
89     /*
90      * @brief set callback of all animation finish
91      * @param finishCallback all animations in animator use this finishcallback, just one
92      */
93     void SetFinishCallBack(const std::function<void()>& finishCallback);
94 protected:
95     explicit RSInteractiveImplictAnimator(
96         const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve);
97 private:
98     static InteractiveImplictAnimatorId GenerateId();
99     const InteractiveImplictAnimatorId id_;
100 
101     static void InitUniRenderEnabled();
102     bool IsUniRenderEnabled() const;
103     void FinishOnCurrent();
104     void CallFinishCallback();
105     std::shared_ptr<InteractiveAnimatorFinishCallback> GetAnimatorFinishCallback();
106 
107     RSInteractiveAnimationState state_ { RSInteractiveAnimationState::INACTIVE };
108     RSAnimationTimingProtocol timingProtocol_;
109     RSAnimationTimingCurve timingCurve_;
110 
111     std::vector<std::pair<std::weak_ptr<RSAnimation>, NodeId>> animations_;
112     std::function<void()> finishCallback_;
113     std::weak_ptr<InteractiveAnimatorFinishCallback> animatorCallback_;
114     AnimationId fractionAnimationId_ { 0 };
115     NodeId fractionNodeId_ { 0 };
116 };
117 } // namespace Rosen
118 } // namespace OHOS
119 
120 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_INTERACTIVE_IMPLICT_ANIMATOR_H
121