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