1 /*
2 * Copyright (c) 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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_shape_bridge.h"
16
17 #include "core/interfaces/native/node/api.h"
18 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19
20 namespace OHOS::Ace::NG {
21 constexpr int NUM_0 = 0;
22 constexpr int NUM_1 = 1;
23 constexpr int NUM_2 = 2;
24 constexpr int NUM_3 = 3;
25 constexpr int NUM_4 = 4;
26
SetViewPort(ArkUIRuntimeCallInfo * runtimeCallInfo)27 ArkUINativeModuleValue ShapeBridge::SetViewPort(ArkUIRuntimeCallInfo* runtimeCallInfo)
28 {
29 EcmaVM* vm = runtimeCallInfo->GetVM();
30 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
31 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
32 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
33 Local<JSValueRef> xArg = runtimeCallInfo->GetCallArgRef(NUM_1);
34 Local<JSValueRef> yArg = runtimeCallInfo->GetCallArgRef(NUM_2);
35 Local<JSValueRef> widthArg = runtimeCallInfo->GetCallArgRef(NUM_3);
36 Local<JSValueRef> heightArg = runtimeCallInfo->GetCallArgRef(NUM_4);
37 CalcDimension dimLeft;
38 ArkTSUtils::ParseJsDimensionVp(vm, xArg, dimLeft);
39 CalcDimension dimTop;
40 ArkTSUtils::ParseJsDimensionVp(vm, yArg, dimTop);
41 CalcDimension dimWidth;
42 ArkTSUtils::ParseJsDimensionVp(vm, widthArg, dimWidth);
43 CalcDimension dimHeight;
44 ArkTSUtils::ParseJsDimensionVp(vm, heightArg, dimHeight);
45 std::vector<double> dimValues;
46 std::vector<int32_t> dimUnits;
47 dimValues.push_back(dimLeft.Value());
48 dimValues.push_back(dimTop.Value());
49 dimValues.push_back(dimWidth.Value());
50 dimValues.push_back(dimHeight.Value());
51 dimUnits.push_back(static_cast<int32_t>(dimLeft.Unit()));
52 dimUnits.push_back(static_cast<int32_t>(dimTop.Unit()));
53 dimUnits.push_back(static_cast<int32_t>(dimWidth.Unit()));
54 dimUnits.push_back(static_cast<int32_t>(dimHeight.Unit()));
55 GetArkUIInternalNodeAPI()->GetShapeModifier().SetShapeViewPort(nativeNode, dimValues.data(), dimUnits.data());
56 return panda::JSValueRef::Undefined(vm);
57 }
58
ResetViewPort(ArkUIRuntimeCallInfo * runtimeCallInfo)59 ArkUINativeModuleValue ShapeBridge::ResetViewPort(ArkUIRuntimeCallInfo* runtimeCallInfo)
60 {
61 EcmaVM* vm = runtimeCallInfo->GetVM();
62 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
63 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
64 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
65 GetArkUIInternalNodeAPI()->GetShapeModifier().ResetShapeViewPort(nativeNode);
66 return panda::JSValueRef::Undefined(vm);
67 }
68
SetMesh(ArkUIRuntimeCallInfo * runtimeCallInfo)69 ArkUINativeModuleValue ShapeBridge::SetMesh(ArkUIRuntimeCallInfo* runtimeCallInfo)
70 {
71 EcmaVM* vm = runtimeCallInfo->GetVM();
72 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
73 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
74 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
75 Local<JSValueRef> valueArrayArg = runtimeCallInfo->GetCallArgRef(NUM_1);
76 Local<JSValueRef> columnArg = runtimeCallInfo->GetCallArgRef(NUM_2);
77 Local<JSValueRef> rowArg = runtimeCallInfo->GetCallArgRef(NUM_3);
78 std::vector<double> mesh;
79 if (valueArrayArg->IsArray(vm)) {
80 auto arrayVal = panda::Local<panda::ArrayRef>(valueArrayArg);
81 auto length = arrayVal->Length(vm);
82 if (length <= 0) {
83 return panda::JSValueRef::Undefined(vm);
84 }
85 for (size_t i = 0; i < length; i++) {
86 Local<JSValueRef> radiusItem = panda::ArrayRef::GetValueAt(vm, arrayVal, i);
87 double vert;
88 if (ArkTSUtils::ParseJsDouble(vm, radiusItem, vert)) {
89 mesh.push_back(vert);
90 }
91 }
92 }
93 int32_t column = 0;
94 int32_t row = 0;
95 if (!ArkTSUtils::ParseJsInteger(vm, columnArg, column)) {
96 return panda::JSValueRef::Undefined(vm);
97 }
98 if (!ArkTSUtils::ParseJsInteger(vm, rowArg, row)) {
99 return panda::JSValueRef::Undefined(vm);
100 }
101 GetArkUIInternalNodeAPI()->GetShapeModifier().SetShapeMesh(nativeNode, mesh.data(), mesh.size(), column, row);
102 return panda::JSValueRef::Undefined(vm);
103 }
104
ResetMesh(ArkUIRuntimeCallInfo * runtimeCallInfo)105 ArkUINativeModuleValue ShapeBridge::ResetMesh(ArkUIRuntimeCallInfo* runtimeCallInfo)
106 {
107 EcmaVM* vm = runtimeCallInfo->GetVM();
108 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
109 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
110 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
111 GetArkUIInternalNodeAPI()->GetShapeModifier().ResetShapeMesh(nativeNode);
112 return panda::JSValueRef::Undefined(vm);
113 }
114 } // namespace OHOS::Ace::NG
115