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