• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_RECOGNIZER_H
18 
19 #include <map>
20 #include <memory>
21 #include <set>
22 
23 #include "gesture_referee.h"
24 
25 #include "core/event/touch_event.h"
26 #include "core/gestures/gesture_info.h"
27 
28 namespace OHOS::Ace {
29 
30 enum class DetectState { READY, DETECTING, DETECTED };
31 
32 enum class RefereeState { DETECTING, PENDING, BLOCKED, SUCCEED, FAIL };
33 
34 class ACE_EXPORT GestureRecognizer : public TouchEventTarget {
35     DECLARE_ACE_TYPE(GestureRecognizer, TouchEventTarget)
36 
37 public:
38     // Called when request of handling gesture sequence is accepted by gesture referee.
39     virtual void OnAccepted(size_t touchId) = 0;
40 
41     // Called when request of handling gesture sequence is rejected by gesture referee.
42     virtual void OnRejected(size_t touchId) = 0;
43 
44     // Called when request of handling gesture sequence is rejected by gesture referee.
OnPending(size_t touchId)45     virtual void OnPending(size_t touchId) {}
46 
47     // Reconiciles the state from the given recognizer into this. The
48     // implementation must check that the given recognizer type matches the
49     // current one. The return value should be false if the reconciliation fails
50     // and true if it succeeds
ReconcileFrom(const RefPtr<GestureRecognizer> & recognizer)51     virtual bool ReconcileFrom(const RefPtr<GestureRecognizer>& recognizer)
52     {
53         return true;
54     }
55 
DispatchEvent(const TouchEvent & point)56     bool DispatchEvent(const TouchEvent& point) override
57     {
58         return true;
59     }
60     bool HandleEvent(const TouchEvent& point) override;
61 
62     // Coordinate offset is used to calculate the local location of the touch point in the render node.
SetCoordinateOffset(const Offset & coordinateOffset)63     void SetCoordinateOffset(const Offset& coordinateOffset)
64     {
65         coordinateOffset_ = coordinateOffset;
66     }
67 
68     // Gets the coordinate offset to calculate the local location of the touch point by manually.
GetCoordinateOffset()69     const Offset& GetCoordinateOffset() const
70     {
71         return coordinateOffset_;
72     }
73 
GetPriority()74     GesturePriority GetPriority() const
75     {
76         return priority_;
77     }
78 
SetPriority(GesturePriority priority)79     void SetPriority(GesturePriority priority)
80     {
81         priority_ = priority;
82     }
83 
GetPriorityMask()84     GestureMask GetPriorityMask() const
85     {
86         return priorityMask_;
87     }
88 
SetPriorityMask(GestureMask priorityMask)89     void SetPriorityMask(GestureMask priorityMask)
90     {
91         priorityMask_ = priorityMask;
92     }
93 
GetRefereeState()94     RefereeState GetRefereeState() const
95     {
96         return refereeState_;
97     }
98 
SetRefereeState(RefereeState refereeState)99     void SetRefereeState(RefereeState refereeState)
100     {
101         refereeState_ = refereeState;
102     }
103 
GetDetectState()104     DetectState GetDetectState() const
105     {
106         return state_;
107     }
108 
SetGestureGroup(const WeakPtr<GestureRecognizer> & gestureGroup)109     void SetGestureGroup(const WeakPtr<GestureRecognizer>& gestureGroup)
110     {
111         gestureGroup_ = gestureGroup;
112     }
113 
SetOnAction(const GestureEventFunc & onAction)114     void SetOnAction(const GestureEventFunc& onAction)
115     {
116         onAction_ = std::make_unique<GestureEventFunc>(onAction);
117     }
118 
SetOnActionStart(const GestureEventFunc & onActionStart)119     void SetOnActionStart(const GestureEventFunc& onActionStart)
120     {
121         onActionStart_ = std::make_unique<GestureEventFunc>(onActionStart);
122     }
123 
SetOnActionUpdate(const GestureEventFunc & onActionUpdate)124     void SetOnActionUpdate(const GestureEventFunc& onActionUpdate)
125     {
126         onActionUpdate_ = std::make_unique<GestureEventFunc>(onActionUpdate);
127     }
128 
SetOnActionEnd(const GestureEventFunc & onActionEnd)129     void SetOnActionEnd(const GestureEventFunc& onActionEnd)
130     {
131         onActionEnd_ = std::make_unique<GestureEventFunc>(onActionEnd);
132     }
133 
SetOnActionCancel(const GestureEventNoParameter & onActionCancel)134     void SetOnActionCancel(const GestureEventNoParameter& onActionCancel)
135     {
136         onActionCancel_ = std::make_unique<GestureEventNoParameter>(onActionCancel);
137     }
138 
SendCancelMsg()139     inline void SendCancelMsg()
140     {
141         if (onActionCancel_ && *onActionCancel_) {
142             (*onActionCancel_)();
143         }
144     }
145 
SetIsExternalGesture(bool isExternalGesture)146     void SetIsExternalGesture(bool isExternalGesture)
147     {
148         isExternalGesture_ = isExternalGesture;
149     }
150 
GetIsExternalGesture()151     bool GetIsExternalGesture() const
152     {
153         return isExternalGesture_;
154     }
155 
156 protected:
157     virtual void HandleTouchDownEvent(const TouchEvent& event) = 0;
158     virtual void HandleTouchUpEvent(const TouchEvent& event) = 0;
159     virtual void HandleTouchMoveEvent(const TouchEvent& event) = 0;
160     virtual void HandleTouchCancelEvent(const TouchEvent& event) = 0;
161 
162     virtual void AddToReferee(size_t touchId, const RefPtr<GestureRecognizer>& recognizer);
163     virtual void DelFromReferee(size_t touchId, const RefPtr<GestureRecognizer>& recognizer);
164     virtual void BatchAdjudicate(
165         const std::set<size_t>& touchIds, const RefPtr<GestureRecognizer>& recognizer, GestureDisposal disposal);
166 
167     Offset coordinateOffset_;
168     DetectState state_ { DetectState::READY };
169     RefereeState refereeState_ { RefereeState::DETECTING };
170     GesturePriority priority_ = GesturePriority::Low;
171     GestureMask priorityMask_ = GestureMask::Normal;
172 
173     int32_t fingers_ = 1;
174     std::list<FingerInfo> fingerList_;
175     bool isExternalGesture_ = false;
176 
177     std::unique_ptr<GestureEventFunc> onAction_;
178     std::unique_ptr<GestureEventFunc> onActionStart_;
179     std::unique_ptr<GestureEventFunc> onActionUpdate_;
180     std::unique_ptr<GestureEventFunc> onActionEnd_;
181     std::unique_ptr<GestureEventNoParameter> onActionCancel_;
182 
183     int64_t deviceId_ = 0;
184     SourceType deviceType_ = SourceType::NONE;
185 
186 private:
187     WeakPtr<GestureRecognizer> gestureGroup_;
188 };
189 
190 } // namespace OHOS::Ace
191 
192 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_RECOGNIZER_H
193