• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "frameworks/bridge/declarative_frontend/jsview/js_polygon.h"
17 
18 #include "core/components/box/box_component.h"
19 #include "core/components/shape/shape_component.h"
20 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
21 
22 namespace OHOS::Ace::Framework {
23 
Create(const JSCallbackInfo & info)24 void JSPolygon::Create(const JSCallbackInfo& info)
25 {
26     RefPtr<Component> polygonComponent = AceType::MakeRefPtr<OHOS::Ace::ShapeComponent>(ShapeType::POLYGON);
27     ViewStackProcessor::GetInstance()->Push(polygonComponent);
28     JSShapeAbstract::SetSize(info);
29 }
30 
JSBind(BindingTarget globalObj)31 void JSPolygon::JSBind(BindingTarget globalObj)
32 {
33     JSClass<JSPolygon>::Declare("Polygon");
34     MethodOptions opt = MethodOptions::NONE;
35     JSClass<JSPolygon>::StaticMethod("create", &JSPolygon::Create, opt);
36 
37     JSClass<JSPolygon>::StaticMethod("width", &JSShapeAbstract::JsWidth);
38     JSClass<JSPolygon>::StaticMethod("height", &JSShapeAbstract::JsHeight);
39     JSClass<JSPolygon>::StaticMethod("points", &JSPolygon::JsPoints);
40 
41     JSClass<JSPolygon>::Inherit<JSShapeAbstract>();
42     JSClass<JSPolygon>::Bind(globalObj);
43 }
44 
JsPoints(const JSCallbackInfo & info)45 void JSPolygon::JsPoints(const JSCallbackInfo& info)
46 {
47     if (info.Length() < 1) {
48         LOGE("The arg is wrong, it is supposed to have atleast 1 argument.");
49         return;
50     }
51     auto polygon = AceType::DynamicCast<ShapeComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
52     ShapePoint shapePoint;
53     ShapePoints shapePoints;
54     JSRef<JSArray> pointsArray = JSRef<JSArray>::Cast(info[0]);
55     if (!polygon) {
56         LOGE("ShapeComponent is null.");
57         return;
58     }
59     if (pointsArray->Length() < 3) {
60         LOGE("Less than three parameters");
61         return;
62     } else {
63         for (size_t i = 0; i < pointsArray->Length(); i++) {
64             JSRef<JSArray> pointArray = pointsArray->GetValueAt(i);
65             if (pointArray->GetValueAt(0)->IsNumber()) {
66                 shapePoint.first = Dimension(pointArray->GetValueAt(0)->ToNumber<double>(), DimensionUnit::VP);
67             } else if (pointArray->GetValueAt(0)->IsString()) {
68                 shapePoint.first = StringUtils::StringToDimension(pointArray->GetValueAt(0)->ToString(), true);
69             } else {
70                 LOGE("Polygon point should be Number or String");
71                 return;
72             }
73             if (pointArray->GetValueAt(1)->IsNumber()) {
74                 shapePoint.second = Dimension(pointArray->GetValueAt(1)->ToNumber<double>(), DimensionUnit::VP);
75             } else if (pointArray->GetValueAt(1)->IsString()) {
76                 shapePoint.second = StringUtils::StringToDimension(pointArray->GetValueAt(1)->ToString(), true);
77             } else {
78                 LOGE("Polygon point should be Number or String");
79                 return;
80             }
81             shapePoints.push_back(shapePoint);
82         }
83         polygon->SetPoints(shapePoints);
84     }
85 }
86 
87 } // namespace OHOS::Ace::Framework
88