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 OHOS_ACE_FRAMEWORK_CJ_ANIMATOR_H
17 #define OHOS_ACE_FRAMEWORK_CJ_ANIMATOR_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 
23 #include "ffi_remote_data.h"
24 #include "base/memory/referenced.h"
25 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_macro.h"
26 #include "core/animation/animator.h"
27 #include "core/animation/curve.h"
28 #include "core/animation/curve_animation.h"
29 #include "core/animation/spring_motion.h"
30 namespace OHOS::Ace {
31 
32 struct AnimatorOption {
33     int32_t duration = 0;
34     int32_t delay = 0;
35     int32_t iterations = 0;
36     double begin = 0.0;
37     double end = 0.0;
38     std::string easing = "ease";
39     std::string fill = "none";
40     std::string direction = "normal";
41 
ToStringAnimatorOption42     std::string ToString() const
43     {
44         return "AnimatorOption:[" + std::to_string(duration) + "," + std::to_string(delay) + "," +
45                std::to_string(iterations) + "," + std::to_string(begin) + "," + std::to_string(end) + "," + easing +
46                "," + fill + "," + direction + "]";
47     }
48 };
49 
50 extern RefPtr<Motion> ParseOptionToMotion(const std::shared_ptr<AnimatorOption>& option);
51 class AnimatorResultImpl : public OHOS::FFI::FFIData {
52     DECL_TYPE(AnimatorResultImpl, OHOS::FFI::FFIData);
53 public:
54     AnimatorResultImpl() = default;
AnimatorResultImpl(RefPtr<Animator> animator,std::shared_ptr<AnimatorOption> option)55     AnimatorResultImpl(RefPtr<Animator> animator, std::shared_ptr<AnimatorOption> option)
56         : animator_(std::move(animator)), option_(std::move(option))
57     {
58         ApplyOption();
59     }
~AnimatorResultImpl()60     ~AnimatorResultImpl()
61     {
62         Destroy();
63     }
64 
GetAnimator()65     RefPtr<Animator> GetAnimator() const
66     {
67         return animator_;
68     }
69 
GetAnimatorOption()70     std::shared_ptr<AnimatorOption> GetAnimatorOption() const
71     {
72         return option_;
73     }
74 
GetMotion()75     const RefPtr<Motion>& GetMotion() const
76     {
77         return motion_;
78     }
79 
SetMotion(const RefPtr<Motion> & motion)80     void SetMotion(const RefPtr<Motion>& motion)
81     {
82         motion_ = motion;
83     }
84 
GetOnframeRef()85     std::function<void(double)> GetOnframeRef() const
86     {
87         return onframe_;
88     }
89 
90     void SetOnframe(int64_t funcId);
91 
92     void SetOnfinish(int64_t funcId);
93 
94     void SetOncancel(int64_t funcId);
95 
96     void SetOnrepeat(int64_t funcId);
97 
98     void ApplyOption();
99 
100     void Destroy();
101 
102 private:
103     RefPtr<Animator> animator_;
104     RefPtr<Motion> motion_;
105     std::shared_ptr<AnimatorOption> option_;
106     std::function<void(double)> onframe_ = nullptr;
107     std::function<void(void)> onfinish_ = nullptr;
108     std::function<void(void)> oncancel_ = nullptr;
109     std::function<void(void)> onrepeat_ = nullptr;
110 };
111 
112 } // namespace OHOS::Ace
113 
114 #endif // #define OHOS_ACE_FRAMEWORK_CJ_ANIMATOR_H
115