1 /*
2 * Copyright (c) 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 "core/components_ng/gestures/pan_gesture.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "core/components_ng/gestures/recognizers/pan_recognizer.h"
20 #include "core/pipeline_ng/pipeline_context.h"
21 
22 namespace OHOS::Ace::NG {
23 
CreateRecognizer()24 RefPtr<NGGestureRecognizer> PanGesture::CreateRecognizer()
25 {
26     auto context = PipelineContext::GetCurrentContextSafely();
27     CHECK_NULL_RETURN(context, nullptr);
28 
29     RefPtr<PanRecognizer> panRecognizer;
30     if (panGestureOption_) {
31         panRecognizer = AceType::MakeRefPtr<PanRecognizer>(panGestureOption_);
32     } else {
33         panRecognizer = AceType::MakeRefPtr<PanRecognizer>(fingers_, direction_, distance_);
34     }
35     if (onActionStartId_) {
36         panRecognizer->SetOnActionStart(*onActionStartId_);
37     }
38 
39     if (onActionUpdateId_) {
40         panRecognizer->SetOnActionUpdate(*onActionUpdateId_);
41     }
42 
43     if (onActionEndId_) {
44         panRecognizer->SetOnActionEnd(*onActionEndId_);
45     }
46 
47     if (onActionCancelId_) {
48         panRecognizer->SetOnActionCancel(*onActionCancelId_);
49     }
50 
51     panRecognizer->SetPriority(priority_);
52     panRecognizer->SetPriorityMask(gestureMask_);
53     panRecognizer->SetGestureInfo(gestureInfo_);
54     if (gestureInfo_) {
55         gestureInfo_->SetDisposeTag(false);
56     }
57     panRecognizer->SetUserData(userData_);
58     return panRecognizer;
59 }
60 
SerializeTo(char * buff)61 void PanGesture::SerializeTo(char* buff)
62 {
63     *reinterpret_cast<int32_t*>(buff) = fingers_;
64     buff += sizeof(int32_t);
65     *reinterpret_cast<GesturePriority*>(buff) = priority_;
66     buff += sizeof(GesturePriority);
67     *reinterpret_cast<GestureMask*>(buff) = gestureMask_;
68     buff += sizeof(GestureMask);
69     *reinterpret_cast<PanDirection*>(buff) = direction_;
70     buff += sizeof(PanDirection);
71     *reinterpret_cast<double*>(buff) = distance_;
72     buff += sizeof(double);
73     double* matrix = reinterpret_cast<double*>(buff);
74     for (int i = 0; i < Matrix4::DIMENSION; i++) {
75         for (int j = 0; j < Matrix4::DIMENSION; j++) {
76             matrix[i * Matrix4::DIMENSION + j] = matrix_.Get(i, j);
77         }
78     }
79 }
80 
SizeofMe()81 int32_t PanGesture::SizeofMe()
82 {
83     return sizeof(int32_t) + sizeof(GestureType) + sizeof(PanDirection) + sizeof(double) + sizeof(Matrix4) +
84            sizeof(fingers_) + sizeof(priority_) + sizeof(gestureMask_);
85 }
86 
Serialize(char * panGesture)87 int32_t PanGesture::Serialize(char* panGesture)
88 {
89     if (panGesture == nullptr) {
90         return -1;
91     }
92     int sizePan = SizeofMe();
93     panGesture = SetHeader(panGesture, GestureType::PAN, sizePan);
94     SerializeTo(panGesture);
95     return sizePan;
96 }
97 
Deserialize(const char * buff)98 int32_t PanGesture::Deserialize(const char* buff)
99 {
100     if (buff == nullptr) {
101         return -1;
102     }
103     buff += sizeof(GestureType) + sizeof(int32_t);
104     fingers_ = *reinterpret_cast<int32_t*>(const_cast<char*>(buff));
105     buff += sizeof(int32_t);
106     priority_ = *reinterpret_cast<GesturePriority*>(const_cast<char*>(buff));
107     buff += sizeof(GesturePriority);
108     gestureMask_ = *reinterpret_cast<GestureMask*>(const_cast<char*>(buff));
109     buff += sizeof(GestureMask);
110     direction_ = *reinterpret_cast<PanDirection*>(const_cast<char*>(buff));
111     buff += sizeof(PanDirection);
112     distance_ = *reinterpret_cast<double*>(const_cast<char*>(buff));
113     buff += sizeof(double);
114     double* matrix = reinterpret_cast<double*>(const_cast<char*>(buff));
115     for (int i = 0; i < Matrix4::DIMENSION; i++) {
116         for (int j = 0; j < Matrix4::DIMENSION; j++) {
117             matrix_.Set(i, j, matrix[i * Matrix4::DIMENSION + j]);
118         }
119     }
120     return SizeofMe();
121 }
122 
123 
124 } // namespace OHOS::Ace::NG
125