• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::vector<RefPtr<ResourceObject>> xResObjArray;
50     std::vector<RefPtr<ResourceObject>> yResObjArray;
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         RefPtr<ResourceObject> xResObj;
55         RefPtr<ResourceObject> yResObj;
56         if (!ArkTSUtils::ParseJsDimensionVpNG(vm, xValue, x, xResObj, false) ||
57             !ArkTSUtils::ParseJsDimensionVpNG(vm, yValue, y, yResObj, false)) {
58             flag = false;
59             break;
60         }
61 
62         xPointValues.push_back(x.Value());
63         yPointValues.push_back(y.Value());
64         xResObjArray.push_back(xResObj);
65         yResObjArray.push_back(yResObj);
66     }
67 
68     if (flag) {
69         GetArkUINodeModifiers()->getPolygonModifier()->setPolygonPoints(
70             nativeNode, xPointValues.data(), yPointValues.data(), xlength, xResObjArray.data(), yResObjArray.data());
71     } else {
72         GetArkUINodeModifiers()->getPolygonModifier()->resetPolygonPoints(nativeNode);
73     }
74 
75     return panda::JSValueRef::Undefined(vm);
76 }
77 
ResetPolygonPoints(ArkUIRuntimeCallInfo * runtimeCallInfo)78 ArkUINativeModuleValue PolygonBridge::ResetPolygonPoints(ArkUIRuntimeCallInfo* runtimeCallInfo)
79 {
80     EcmaVM* vm = runtimeCallInfo->GetVM();
81     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
82     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
83     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
84     GetArkUINodeModifiers()->getPolygonModifier()->resetPolygonPoints(nativeNode);
85     return panda::JSValueRef::Undefined(vm);
86 }
87 }
88