• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_QUICKJS_ANIMATOR_BRIDGE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_QUICKJS_ANIMATOR_BRIDGE_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "core/animation/keyframe_animation.h"
23 #include "frameworks/bridge/js_frontend/engine/common/base_animation_bridge.h"
24 #include "frameworks/bridge/js_frontend/engine/quickjs/qjs_utils.h"
25 #include "frameworks/bridge/js_frontend/frontend_delegate.h"
26 #include "frameworks/bridge/js_frontend/js_command.h"
27 
28 namespace OHOS::Ace::Framework {
29 
30 class AnimatorBridgeUtils {
31 public:
32     static JSValue CreateAnimatorContext(JSContext* ctx, int32_t pageId, int32_t bridgeId);
33     static JSValue JsAnimatorPlay(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv);
34     static JSValue JsAnimatorFinish(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv);
35     static JSValue JsAnimatorPause(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv);
36     static JSValue JsAnimatorCancel(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv);
37     static JSValue JsAnimatorReverse(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv);
38     static JSValue JsAnimatorUpdate(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv);
39     static int32_t JsCreateBridgeId();
40 };
41 
42 class AnimatorBridge : public BaseAnimationBridge {
43     DECLARE_ACE_TYPE(AnimatorBridge, BaseAnimationBridge)
44 public:
45     AnimatorBridge(JSContext* ctx, JSValue animationContext);
46     ~AnimatorBridge() override;
47     void OnJsEngineDestroy() override;
GetJsObject()48     JSValue GetJsObject()
49     {
50         return animationContext_;
51     }
52 
GetContext()53     JSContext* GetContext()
54     {
55         return ctx_;
56     }
57 
JsGetAnimator()58     RefPtr<Animator> JsGetAnimator() override
59     {
60         return animator_;
61     }
62 
63     void JsCreateAnimation(const std::string& param);
64 
65 private:
66     RefPtr<KeyframeAnimation<double>> CreateDoubleAnimation(
67         const std::unordered_map<std::string, double>& animationParams, const RefPtr<Curve>& curve);
68 
69     JSContext* ctx_ = nullptr;
70     JSValue animationContext_;
71     RefPtr<Animator> animator_;
72 };
73 
74 class AnimatorTaskCreate : public AnimatorBridgeTask {
75     DECLARE_ACE_TYPE(AnimatorTaskCreate, AnimatorBridgeTask)
76 public:
77     AnimatorTaskCreate(const RefPtr<AnimatorBridge>& bridge, const std::string& param);
78     ~AnimatorTaskCreate() override = default;
79     void AnimatorBridgeTaskFunc(const RefPtr<JsAcePage>& page, int32_t bridgeId) override;
80 
81 private:
82     RefPtr<AnimatorBridge> bridge_;
83     std::string param_;
84 };
85 
86 class AnimatorTaskOperation : public AnimatorBridgeTask {
DECLARE_ACE_TYPE(AnimatorTaskOperation,AnimatorBridgeTask)87     DECLARE_ACE_TYPE(AnimatorTaskOperation, AnimatorBridgeTask)
88 public:
89     explicit AnimatorTaskOperation(AnimatorOperation operation) : operation_(operation) {}
90     ~AnimatorTaskOperation() override = default;
91     void AnimatorBridgeTaskFunc(const RefPtr<JsAcePage>& page, int32_t bridgeId) override;
92 
93 private:
94     AnimatorOperation operation_ = AnimatorOperation::NONE;
95 };
96 
97 class AnimatorTaskUpdate : public AnimatorBridgeTask {
DECLARE_ACE_TYPE(AnimatorTaskUpdate,AnimatorBridgeTask)98     DECLARE_ACE_TYPE(AnimatorTaskUpdate, AnimatorBridgeTask)
99 public:
100     explicit AnimatorTaskUpdate(const std::unordered_map<std::string, std::string>& params) : params_(params) {}
101     ~AnimatorTaskUpdate() override = default;
102     void AnimatorBridgeTaskFunc(const RefPtr<JsAcePage>& page, int32_t bridgeId) override;
103 
104 private:
105     void UpdateAnimator(const RefPtr<Animator>& animator, const RefPtr<AnimatorBridge>& bridge,
106         JSContext* ctx, const std::unordered_map<std::string, std::string>& params);
107     RefPtr<KeyframeAnimation<double>> CreateDoubleAnimation(double begin, double end, const RefPtr<Curve>& curve);
108 
109     std::unordered_map<std::string, std::string> params_;
110 };
111 
112 } // namespace OHOS::Ace::Framework
113 
114 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_QUICKJS_ANIMATOR_BRIDGE_H