• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_GESTURE_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_GESTURE_RECOGNIZER_H
18 
19 #include <memory>
20 
21 #include "base/memory/referenced.h"
22 #include "core/components_ng/gestures/gesture_info.h"
23 #include "core/components_ng/gestures/gesture_referee.h"
24 #include "core/event/axis_event.h"
25 #include "core/event/touch_event.h"
26 #include "frameworks/base/geometry/ng/point_t.h"
27 
28 namespace OHOS::Ace::NG {
29 
30 struct DelayedTask {
31     WeakPtr<NGGestureRecognizer> recognizer;
32     int64_t timeStamp = 0;
33     int32_t time = 0;
34     std::function<void()> task;
35 };
36 
37 enum class RefereeState { READY, DETECTING, PENDING, PENDING_BLOCKED, SUCCEED_BLOCKED, SUCCEED, FAIL };
38 
39 class ACE_EXPORT NGGestureRecognizer : public TouchEventTarget {
40     DECLARE_ACE_TYPE(NGGestureRecognizer, TouchEventTarget)
41 
42 public:
43     static std::unordered_map<int, TransformConfig>& GetGlobalTransCfg();
44 
45     static std::unordered_map<int, AncestorNodeInfo>& GetGlobalTransIds();
46 
47     static void ResetGlobalTransCfg();
48 
49     // Triggered when the gesture referee finishes collecting gestures and begin a gesture referee.
50     void BeginReferee(int32_t touchId, bool needUpdateChild = false)
51     {
52         OnBeginGestureReferee(touchId, needUpdateChild);
53     }
54 
55     // Triggered when the Gesture referee ends a gesture referee.
56     void FinishReferee(int32_t touchId, bool isBlocked = false)
57     {
58         OnFinishGestureReferee(touchId, isBlocked);
59     }
60 
61     // Called when request of handling gesture sequence is accepted by gesture referee.
62     virtual void OnAccepted() = 0;
63 
64     // Called when request of handling gesture sequence is rejected by gesture referee.
65     virtual void OnRejected() = 0;
66 
67     // Called when request of handling gesture sequence is pending by gesture referee.
OnPending()68     virtual void OnPending()
69     {
70         refereeState_ = RefereeState::PENDING;
71     }
72 
73     // Called when request of handling gesture sequence is blocked by gesture referee.
OnBlocked()74     virtual void OnBlocked()
75     {
76         if (disposal_ == GestureDisposal::ACCEPT) {
77             refereeState_ = RefereeState::SUCCEED_BLOCKED;
78         }
79         if (disposal_ == GestureDisposal::PENDING) {
80             refereeState_ = RefereeState::PENDING_BLOCKED;
81         }
82     }
83 
84     // Reconcile the state from the given recognizer into this. The
85     // implementation must check that the given recognizer type matches the
86     // current one. The return value should be false if the reconciliation fails
87     // and true if it succeeds
ReconcileFrom(const RefPtr<NGGestureRecognizer> & recognizer)88     virtual bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer)
89     {
90         return true;
91     }
92 
DispatchEvent(const TouchEvent & point)93     bool DispatchEvent(const TouchEvent& point) override
94     {
95         return true;
96     }
97     bool HandleEvent(const TouchEvent& point) override;
98     bool HandleEvent(const AxisEvent& event) override;
99 
GetPriority()100     GesturePriority GetPriority() const
101     {
102         return priority_;
103     }
104 
SetPriority(GesturePriority priority)105     void SetPriority(GesturePriority priority)
106     {
107         priority_ = priority;
108     }
109 
GetPriorityMask()110     GestureMask GetPriorityMask() const
111     {
112         return priorityMask_;
113     }
114 
SetPriorityMask(GestureMask priorityMask)115     void SetPriorityMask(GestureMask priorityMask)
116     {
117         priorityMask_ = priorityMask;
118     }
119 
GetGestureDisposal()120     GestureDisposal GetGestureDisposal() const
121     {
122         return disposal_;
123     }
124 
GetRefereeState()125     RefereeState GetRefereeState() const
126     {
127         return refereeState_;
128     }
129 
SetGestureGroup(const WeakPtr<NGGestureRecognizer> & gestureGroup)130     void SetGestureGroup(const WeakPtr<NGGestureRecognizer>& gestureGroup)
131     {
132         gestureGroup_ = gestureGroup;
133     }
134 
SetOnAction(const GestureEventFunc & onAction)135     void SetOnAction(const GestureEventFunc& onAction)
136     {
137         onAction_ = std::make_unique<GestureEventFunc>(onAction);
138     }
139 
SetOnActionStart(const GestureEventFunc & onActionStart)140     void SetOnActionStart(const GestureEventFunc& onActionStart)
141     {
142         onActionStart_ = std::make_unique<GestureEventFunc>(onActionStart);
143     }
144 
SetOnActionUpdate(const GestureEventFunc & onActionUpdate)145     void SetOnActionUpdate(const GestureEventFunc& onActionUpdate)
146     {
147         onActionUpdate_ = std::make_unique<GestureEventFunc>(onActionUpdate);
148     }
149 
SetOnActionEnd(const GestureEventFunc & onActionEnd)150     void SetOnActionEnd(const GestureEventFunc& onActionEnd)
151     {
152         onActionEnd_ = std::make_unique<GestureEventFunc>(onActionEnd);
153     }
154 
SetOnActionCancel(const GestureEventNoParameter & onActionCancel)155     void SetOnActionCancel(const GestureEventNoParameter& onActionCancel)
156     {
157         onActionCancel_ = std::make_unique<GestureEventNoParameter>(onActionCancel);
158     }
159 
SendCancelMsg()160     inline void SendCancelMsg()
161     {
162         if (onActionCancel_ && *onActionCancel_) {
163             (*onActionCancel_)();
164         }
165     }
166 
SetIsExternalGesture(bool isExternalGesture)167     void SetIsExternalGesture(bool isExternalGesture)
168     {
169         isExternalGesture_ = isExternalGesture;
170     }
171 
GetIsExternalGesture()172     bool GetIsExternalGesture() const
173     {
174         return isExternalGesture_;
175     }
176 
IsPending()177     bool IsPending() const
178     {
179         return (refereeState_ == RefereeState::PENDING) || (refereeState_ == RefereeState::PENDING_BLOCKED);
180     }
181 
IsRefereeFinished()182     bool IsRefereeFinished() const
183     {
184         return (refereeState_ == RefereeState::SUCCEED) || (refereeState_ == RefereeState::FAIL) ||
185                (refereeState_ == RefereeState::SUCCEED_BLOCKED);
186     }
187 
188     // called when gesture scope is closed.
189     void ResetStatusOnFinish(bool isBlocked = false)
190     {
191         if (isBlocked && refereeState_ == RefereeState::SUCCEED) {
192             OnSucceedCancel();
193         }
194         refereeState_ = RefereeState::READY;
195         OnResetStatus();
196     }
197 
198     // called to reset status manually without rejected callback.
ResetStatus()199     void ResetStatus()
200     {
201         refereeState_ = RefereeState::READY;
202         OnResetStatus();
203     }
204     virtual bool CheckTouchId(int32_t touchId) = 0;
205 
getDeviceType()206     SourceType getDeviceType()
207     {
208         return deviceType_;
209     }
210 
SetSize(std::optional<double> recognizerTargetAreaHeight,std::optional<double> recognizerTargetAreaWidth)211     void SetSize(std::optional<double> recognizerTargetAreaHeight, std::optional<double> recognizerTargetAreaWidth)
212     {
213         EventTarget recognizerTarget;
214         recognizerTarget.area.SetHeight(Dimension(recognizerTargetAreaHeight.value()));
215         recognizerTarget.area.SetWidth(Dimension(recognizerTargetAreaWidth.value()));
216         recognizerTarget_ = recognizerTarget;
217     }
218 
219     void SetTransInfo(int id);
220     void Transform(PointF& windowPointF, PointF& originPointF);
221 protected:
Adjudicate(const RefPtr<NGGestureRecognizer> & recognizer,GestureDisposal disposal)222     void Adjudicate(const RefPtr<NGGestureRecognizer>& recognizer, GestureDisposal disposal)
223     {
224         disposal_ = disposal;
225         BatchAdjudicate(recognizer, disposal);
226     }
227     virtual void BatchAdjudicate(const RefPtr<NGGestureRecognizer>& recognizer, GestureDisposal disposal);
228 
229     virtual void OnBeginGestureReferee(int32_t touchId, bool needUpdateChild = false) {}
230     virtual void OnFinishGestureReferee(int32_t touchId, bool isBlocked = false) {}
231 
232     virtual void HandleTouchDownEvent(const TouchEvent& event) = 0;
233     virtual void HandleTouchUpEvent(const TouchEvent& event) = 0;
234     virtual void HandleTouchMoveEvent(const TouchEvent& event) = 0;
235     virtual void HandleTouchCancelEvent(const TouchEvent& event) = 0;
HandleTouchDownEvent(const AxisEvent & event)236     virtual void HandleTouchDownEvent(const AxisEvent& event) {}
HandleTouchUpEvent(const AxisEvent & event)237     virtual void HandleTouchUpEvent(const AxisEvent& event) {}
HandleTouchMoveEvent(const AxisEvent & event)238     virtual void HandleTouchMoveEvent(const AxisEvent& event) {}
HandleTouchCancelEvent(const AxisEvent & event)239     virtual void HandleTouchCancelEvent(const AxisEvent& event) {}
240 
241     virtual void OnResetStatus() = 0;
242 
OnSucceedCancel()243     virtual void OnSucceedCancel() {}
244 
245     RefereeState refereeState_ = RefereeState::READY;
246 
247     GestureDisposal disposal_ = GestureDisposal::NONE;
248 
249     GesturePriority priority_ = GesturePriority::Low;
250 
251     GestureMask priorityMask_ = GestureMask::Normal;
252 
253     bool isExternalGesture_ = false;
254 
255     std::unique_ptr<GestureEventFunc> onAction_;
256     std::unique_ptr<GestureEventFunc> onActionStart_;
257     std::unique_ptr<GestureEventFunc> onActionUpdate_;
258     std::unique_ptr<GestureEventFunc> onActionEnd_;
259     std::unique_ptr<GestureEventNoParameter> onActionCancel_;
260 
261     int64_t deviceId_ = 0;
262     SourceType deviceType_ = SourceType::NONE;
263     // size of recognizer target.
264     std::optional<EventTarget> recognizerTarget_ = std::nullopt;
265     int32_t transId_ = 0;
266 
267     int32_t currentFingers_ = 0;
268 private:
269     WeakPtr<NGGestureRecognizer> gestureGroup_;
270 };
271 
272 } // namespace OHOS::Ace::NG
273 
274 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_GESTURE_RECOGNIZER_H
275