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 "bridge/declarative_frontend/jsview/models/polygon_model_impl.h"
19 #include "core/common/container.h"
20 #include "core/components_ng/pattern/shape/polygon_model.h"
21 #include "core/components_ng/pattern/shape/polygon_model_ng.h"
22
23 namespace OHOS::Ace {
GetInstance()24 PolygonModel* PolygonModel::GetInstance()
25 {
26 #ifdef NG_BUILD
27 static NG::PolygonModelNG instance;
28 return &instance;
29 #else
30 if (Container::IsCurrentUseNewPipeline()) {
31 static NG::PolygonModelNG instance;
32 return &instance;
33 } else {
34 static Framework::PolygonModelImpl instance;
35 return &instance;
36 }
37 #endif
38 }
39 } // namespace OHOS::Ace
40
41 namespace OHOS::Ace::Framework {
42
Create(const JSCallbackInfo& info)43 void JSPolygon::Create(const JSCallbackInfo& info)
44 {
45 PolygonModel::GetInstance()->Create(true);
46 JSShapeAbstract::SetSize(info);
47 }
48
JSBind(BindingTarget globalObj)49 void JSPolygon::JSBind(BindingTarget globalObj)
50 {
51 JSClass<JSPolygon>::Declare("Polygon");
52 MethodOptions opt = MethodOptions::NONE;
53 JSClass<JSPolygon>::StaticMethod("create", &JSPolygon::Create, opt);
54
55 JSClass<JSPolygon>::StaticMethod("width", &JSShapeAbstract::JsWidth);
56 JSClass<JSPolygon>::StaticMethod("height", &JSShapeAbstract::JsHeight);
57 JSClass<JSPolygon>::StaticMethod("points", &JSPolygon::JsPoints);
58
59 JSClass<JSPolygon>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
60 JSClass<JSPolygon>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
61 JSClass<JSPolygon>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
62 JSClass<JSPolygon>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
63
64 JSClass<JSPolygon>::InheritAndBind<JSShapeAbstract>(globalObj);
65 }
66
JsPoints(const JSCallbackInfo& info)67 void JSPolygon::JsPoints(const JSCallbackInfo& info)
68 {
69 ShapePoints shapePoints;
70 PolygonModel::GetInstance()->SetPoints(shapePoints);
71 if (info.Length() < 1 || !info[0]->IsArray()) {
72 return;
73 }
74 ShapePoint shapePoint;
75 JSRef<JSArray> pointsArray = JSRef<JSArray>::Cast(info[0]);
76 if (pointsArray->Length() < 2) {
77 return;
78 }
79 for (size_t i = 0; i < pointsArray->Length(); i++) {
80 JSRef<JSVal> val = pointsArray->GetValueAt(i);
81 if (!val->IsArray()) {
82 continue;
83 }
84 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(val);
85 if (pointArray->GetValueAt(0)->IsNumber()) {
86 shapePoint.first = Dimension(pointArray->GetValueAt(0)->ToNumber<double>(), DimensionUnit::VP);
87 } else if (pointArray->GetValueAt(0)->IsString()) {
88 shapePoint.first = StringUtils::StringToDimension(pointArray->GetValueAt(0)->ToString(), true);
89 } else {
90 return;
91 }
92 if (pointArray->GetValueAt(1)->IsNumber()) {
93 shapePoint.second = Dimension(pointArray->GetValueAt(1)->ToNumber<double>(), DimensionUnit::VP);
94 } else if (pointArray->GetValueAt(1)->IsString()) {
95 shapePoint.second = StringUtils::StringToDimension(pointArray->GetValueAt(1)->ToString(), true);
96 } else {
97 return;
98 }
99 shapePoints.push_back(shapePoint);
100 }
101 PolygonModel::GetInstance()->SetPoints(shapePoints);
102 }
103
104 } // namespace OHOS::Ace::Framework
105