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_polyline_bridge.h"
16
17 #include "core/interfaces/native/node/api.h"
18 #include "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
SetPoints(ArkUIRuntimeCallInfo * runtimeCallInfo)25 ArkUINativeModuleValue PolylineBridge::SetPoints(ArkUIRuntimeCallInfo* runtimeCallInfo)
26 {
27 EcmaVM* vm = runtimeCallInfo->GetVM();
28 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
29 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
30 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
31 Local<JSValueRef> xPoint = runtimeCallInfo->GetCallArgRef(NUM_1);
32 Local<JSValueRef> yPoint = runtimeCallInfo->GetCallArgRef(NUM_2);
33 if (!xPoint->IsArray(vm) || !yPoint->IsArray(vm)) {
34 return panda::JSValueRef::Undefined(vm);
35 }
36
37 auto xPointArray = panda::Local<panda::ArrayRef>(xPoint);
38 auto yPointArray = panda::Local<panda::ArrayRef>(yPoint);
39 auto xlength = xPointArray->Length(vm);
40 auto ylength = yPointArray->Length(vm);
41 if (xlength <= 0 || xlength != ylength) {
42 GetArkUIInternalNodeAPI()->GetPolylineModifier().ResetPoints(nativeNode);
43 return panda::JSValueRef::Undefined(vm);
44 }
45
46 bool flag = true;
47 CalcDimension x;
48 CalcDimension y;
49 std::vector<double> xPointValues;
50 std::vector<double> yPointValues;
51 for (size_t i = 0; i < xlength; i++) {
52 Local<JSValueRef> xValue = panda::ArrayRef::GetValueAt(vm, xPointArray, i);
53 Local<JSValueRef> yValue = panda::ArrayRef::GetValueAt(vm, yPointArray, i);
54 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, xValue, x, false) ||
55 !ArkTSUtils::ParseJsDimensionVpNG(vm, yValue, y, false)) {
56 flag = false;
57 break;
58 }
59
60 xPointValues.push_back(x.Value());
61 yPointValues.push_back(y.Value());
62 }
63
64 if (flag) {
65 GetArkUIInternalNodeAPI()->GetPolylineModifier().
66 SetPoints(nativeNode, xPointValues.data(), yPointValues.data(), xlength);
67 } else {
68 GetArkUIInternalNodeAPI()->GetPolylineModifier().ResetPoints(nativeNode);
69 }
70
71 return panda::JSValueRef::Undefined(vm);
72 }
73
ResetPoints(ArkUIRuntimeCallInfo * runtimeCallInfo)74 ArkUINativeModuleValue PolylineBridge::ResetPoints(ArkUIRuntimeCallInfo* runtimeCallInfo)
75 {
76 EcmaVM* vm = runtimeCallInfo->GetVM();
77 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
78 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
79 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
80 GetArkUIInternalNodeAPI()->GetPolylineModifier().ResetPoints(nativeNode);
81 return panda::JSValueRef::Undefined(vm);
82 }
83 }
84