• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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/engine/functions/js_gesture_function.h"
17 
18 #include "base/log/log.h"
19 #include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
20 
21 namespace OHOS::Ace::Framework {
22 
Execute()23 void JsGestureFunction::Execute()
24 {
25     JsFunction::Execute();
26 }
27 
Execute(const GestureEvent & info)28 void JsGestureFunction::Execute(const GestureEvent& info)
29 {
30     JSRef<JSVal> param = JSRef<JSObject>::Cast(CreateGestureEvent(info));
31     JsFunction::ExecuteJS(1, &param);
32 }
33 
CreateGestureEvent(const GestureEvent & info)34 JSRef<JSObject> JsGestureFunction::CreateGestureEvent(const GestureEvent& info)
35 {
36     JSRef<JSObject> gestureInfoObj = JSRef<JSObject>::New();
37     gestureInfoObj->SetProperty<bool>("repeat", info.GetRepeat());
38     gestureInfoObj->SetProperty<double>("offsetX", SystemProperties::Px2Vp(info.GetOffsetX()));
39     gestureInfoObj->SetProperty<double>("offsetY", SystemProperties::Px2Vp(info.GetOffsetY()));
40     gestureInfoObj->SetProperty<double>("scale", info.GetScale());
41     gestureInfoObj->SetProperty<double>("angle", info.GetAngle());
42     gestureInfoObj->SetProperty<double>("speed", info.GetSpeed());
43     gestureInfoObj->SetProperty<double>("timestamp", info.GetTimeStamp().time_since_epoch().count());
44     gestureInfoObj->SetProperty<double>("globalX", SystemProperties::Px2Vp(info.GetGlobalLocation().GetX()));
45     gestureInfoObj->SetProperty<double>("globalY", SystemProperties::Px2Vp(info.GetGlobalLocation().GetY()));
46     gestureInfoObj->SetProperty<double>("localX", SystemProperties::Px2Vp(info.GetLocalLocation().GetX()));
47     gestureInfoObj->SetProperty<double>("localY", SystemProperties::Px2Vp(info.GetLocalLocation().GetY()));
48     gestureInfoObj->SetProperty<double>("pinchCenterX", SystemProperties::Px2Vp(info.GetPinchCenter().GetX()));
49     gestureInfoObj->SetProperty<double>("pinchCenterY", SystemProperties::Px2Vp(info.GetPinchCenter().GetY()));
50     gestureInfoObj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
51     gestureInfoObj->SetProperty<double>("pressure", info.GetForce());
52     if (info.GetTiltX().has_value()) {
53         gestureInfoObj->SetProperty<double>("tiltX", info.GetTiltX().value());
54     }
55     if (info.GetTiltY().has_value()) {
56         gestureInfoObj->SetProperty<double>("tiltY", info.GetTiltY().value());
57     }
58     gestureInfoObj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
59 
60     gestureInfoObj->SetProperty<double>("velocityX", SystemProperties::Px2Vp(info.GetVelocity().GetVelocityX()));
61     gestureInfoObj->SetProperty<double>("velocityY", SystemProperties::Px2Vp(info.GetVelocity().GetVelocityY()));
62     gestureInfoObj->SetProperty<double>("velocity", SystemProperties::Px2Vp(info.GetVelocity().GetVelocityValue()));
63 
64     JSRef<JSArray> fingerArr = JSRef<JSArray>::New();
65     const std::list<FingerInfo>& fingerList = info.GetFingerList();
66     std::list<FingerInfo> notTouchFingerList;
67     int32_t maxFingerId = -1;
68     for (const FingerInfo& fingerInfo : fingerList) {
69         JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
70         if (fingerInfo.sourceType_ == SourceType::TOUCH && fingerInfo.sourceTool_ == SourceTool::FINGER) {
71             fingerArr->SetValueAt(fingerInfo.fingerId_, element);
72             if (fingerInfo.fingerId_ > maxFingerId) {
73                 maxFingerId = fingerInfo.fingerId_;
74             }
75         } else {
76             notTouchFingerList.emplace_back(fingerInfo);
77         }
78     }
79     auto idx = maxFingerId + 1;
80     for (const FingerInfo& fingerInfo : notTouchFingerList) {
81         JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
82         fingerArr->SetValueAt(idx++, element);
83     }
84     gestureInfoObj->SetPropertyObject("fingerList", fingerArr);
85 
86     auto target = CreateEventTargetObject(info);
87     gestureInfoObj->SetPropertyObject("target", target);
88     return gestureInfoObj;
89 }
90 
CreateFingerInfo(const FingerInfo & fingerInfo)91 JSRef<JSObject> JsGestureFunction::CreateFingerInfo(const FingerInfo& fingerInfo)
92 {
93     JSRef<JSObject> fingerInfoObj = JSRef<JSObject>::New();
94     const OHOS::Ace::Offset& globalLocation = fingerInfo.globalLocation_;
95     const OHOS::Ace::Offset& localLocation = fingerInfo.localLocation_;
96     fingerInfoObj->SetProperty<int32_t>("id", fingerInfo.fingerId_);
97     fingerInfoObj->SetProperty<double>("globalX", SystemProperties::Px2Vp(globalLocation.GetX()));
98     fingerInfoObj->SetProperty<double>("globalY", SystemProperties::Px2Vp(globalLocation.GetY()));
99     fingerInfoObj->SetProperty<double>("localX", SystemProperties::Px2Vp(localLocation.GetX()));
100     fingerInfoObj->SetProperty<double>("localY", SystemProperties::Px2Vp(localLocation.GetY()));
101     return fingerInfoObj;
102 }
103 
104 } // namespace OHOS::Ace::Framework
105