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_PAN_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PAN_RECOGNIZER_H
18 
19 #include <map>
20 
21 #include "core/components_ng/event/drag_event.h"
22 #include "core/components_ng/gestures/pan_gesture.h"
23 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 class PanRecognizer : public MultiFingersRecognizer {
28     DECLARE_ACE_TYPE(PanRecognizer, MultiFingersRecognizer);
29 
30 public:
31     PanRecognizer(int32_t fingers, const PanDirection& direction, double distance);
32 
33     explicit PanRecognizer(const RefPtr<PanGestureOption>& panGestureOption);
34 
~PanRecognizer()35     ~PanRecognizer() override
36     {
37         if (panGestureOption_ == nullptr) {
38             return;
39         }
40         panGestureOption_->GetOnPanFingersIds().erase(onChangeFingers_.GetId());
41         panGestureOption_->GetOnPanDirectionIds().erase(onChangeDirection_.GetId());
42         panGestureOption_->GetOnPanDistanceIds().erase(onChangeDistance_.GetId());
43     }
44 
45     void OnAccepted() override;
46     void OnRejected() override;
47 
48     void OnFlushTouchEventsBegin() override;
49     void OnFlushTouchEventsEnd() override;
50 
51     Axis GetAxisDirection() override;
52 
53     void SetDirection(const PanDirection& direction);
54 
SetIsForDrag(bool isForDrag)55     void SetIsForDrag(bool isForDrag)
56     {
57         isForDrag_ = isForDrag;
58     }
59 
SetMouseDistance(double distance)60     void SetMouseDistance(double distance)
61     {
62         mouseDistance_ = distance;
63     }
64 
SetIsAllowMouse(bool isAllowMouse)65     void SetIsAllowMouse(bool isAllowMouse)
66     {
67         isAllowMouse_ = isAllowMouse;
68     }
69 
70     virtual RefPtr<GestureSnapshot> Dump() const override;
71     RefPtr<Gesture> CreateGestureFromRecognizer() const override;
72     void ForceCleanRecognizer() override;
73 
GetDistance()74     double GetDistance() const
75     {
76         return distance_;
77     }
78 
GetDirection()79     PanDirection GetDirection() const
80     {
81         return direction_;
82     }
83 
84 private:
85     class PanVelocity {
86     public:
87         Velocity GetVelocity();
88         double GetMainAxisVelocity();
89         void UpdateTouchPoint(int32_t id, const TouchEvent& event, bool end);
90         void Reset(int32_t id);
91         void ResetAll();
92         void SetDirection(int32_t directionType);
GetVelocityMap()93         const std::map<int32_t, VelocityTracker>& GetVelocityMap() const
94         {
95             return trackerMap_;
96         }
97 
98     private:
99         int32_t GetFastestTracker(std::function<double(VelocityTracker&)>&& func);
100         std::map<int32_t, VelocityTracker> trackerMap_;
101         Axis axis_ = Axis::FREE;
102     };
103 
104     enum class GestureAcceptResult {
105         ACCEPT,
106         REJECT,
107         DETECTING,
108     };
109     void HandleTouchDownEvent(const TouchEvent& event) override;
110     void HandleTouchUpEvent(const TouchEvent& event) override;
111     void HandleTouchMoveEvent(const TouchEvent& event) override;
112     void HandleTouchCancelEvent(const TouchEvent& event) override;
113     void HandleTouchDownEvent(const AxisEvent& event) override;
114     void HandleTouchUpEvent(const AxisEvent& event) override;
115     void HandleTouchMoveEvent(const AxisEvent& event) override;
116     void HandleTouchCancelEvent(const AxisEvent& event) override;
117     void UpdateTouchEventInfo(const TouchEvent& event);
118 
119     bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override;
120     GestureAcceptResult IsPanGestureAccept() const;
121     bool CalculateTruthFingers(bool isDirectionUp) const;
122     void UpdateTouchPointInVelocityTracker(const TouchEvent& touchEvent);
123     void UpdateAxisPointInVelocityTracker(const AxisEvent& event, bool end = false);
124 
125     Offset GetRawGlobalLocation(int32_t postEventNodeId);
126 
127     void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback);
128     GestureJudgeResult TriggerGestureJudgeCallback();
129     void ChangeFingers(int32_t fingers);
130     void ChangeDirection(const PanDirection& direction);
131     void ChangeDistance(double distance);
132     double GetMainAxisDelta();
133     RefPtr<DragEventActuator> GetDragEventActuator();
134     bool HandlePanAccept();
135     GestureEvent GetGestureEventInfo();
136 
137     void OnResetStatus() override;
138     void OnSucceedCancel() override;
139 
140     void AddOverTimeTrace();
141 
142     void DispatchPanStartedToPerf(const TouchEvent& event);
143 
GetTouchRestrict()144     const TouchRestrict& GetTouchRestrict() const
145     {
146         return touchRestrict_;
147     }
148 
149     PanDirection direction_;
150     double distance_ = 0.0;
151     double mouseDistance_ = 0.0;
152     AxisEvent lastAxisEvent_;
153     Offset averageDistance_;
154     std::map<int32_t, Offset> touchPointsDistance_;
155     Offset delta_;
156     double mainDelta_ = 0.0;
157     PanVelocity panVelocity_;
158     TimeStamp time_;
159 
160     Point globalPoint_;
161     TouchEvent lastTouchEvent_;
162     RefPtr<PanGestureOption> panGestureOption_;
163     OnPanFingersFunc onChangeFingers_;
164     OnPanDirectionFunc onChangeDirection_;
165     OnPanDistanceFunc onChangeDistance_;
166 
167     int32_t newFingers_ = 1;
168     double newDistance_ = 0.0;
169     PanDirection newDirection_;
170     bool isFlushTouchEventsEnd_ = false;
171     bool isForDrag_ = false;
172     bool isAllowMouse_ = true;
173     bool isStartTriggered_ = false;
174 };
175 
176 } // namespace OHOS::Ace::NG
177 
178 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PAN_RECOGNIZER_H
179