• 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_PAN_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PAN_RECOGNIZER_H
18 
19 #include <map>
20 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h"
21 
22 namespace OHOS::Ace::NG {
23 
24 class PanRecognizer : public MultiFingersRecognizer {
25     DECLARE_ACE_TYPE(PanRecognizer, MultiFingersRecognizer);
26 
27 public:
28     PanRecognizer(int32_t fingers, const PanDirection& direction, double distance);
29 
30     explicit PanRecognizer(const RefPtr<PanGestureOption>& panGestureOption);
31 
~PanRecognizer()32     ~PanRecognizer() override
33     {
34         if (panGestureOption_ == nullptr) {
35             return;
36         }
37         panGestureOption_->GetOnPanFingersIds().erase(onChangeFingers_.GetId());
38         panGestureOption_->GetOnPanDirectionIds().erase(onChangeDirection_.GetId());
39         panGestureOption_->GetOnPanDistanceIds().erase(onChangeDistance_.GetId());
40     }
41 
42     void OnAccepted() override;
43     void OnRejected() override;
44 
45     void OnFlushTouchEventsBegin() override;
46     void OnFlushTouchEventsEnd() override;
47 
48     void SetDirection(const PanDirection& direction);
49 
SetIsForDrag(bool isForDrag)50     void SetIsForDrag(bool isForDrag)
51     {
52         isForDrag_ = isForDrag;
53     }
54 
SetMouseDistance(double distance)55     void SetMouseDistance(double distance)
56     {
57         mouseDistance_ = distance;
58     }
59 
SetIsAllowMouse(bool isAllowMouse)60     void SetIsAllowMouse(bool isAllowMouse)
61     {
62         isAllowMouse_ = isAllowMouse;
63     }
64 
65 private:
66     enum class GestureAcceptResult {
67         ACCEPT,
68         REJECT,
69         DETECTING,
70     };
71     void HandleTouchDownEvent(const TouchEvent& event) override;
72     void HandleTouchUpEvent(const TouchEvent& event) override;
73     void HandleTouchMoveEvent(const TouchEvent& event) override;
74     void HandleTouchCancelEvent(const TouchEvent& event) override;
75     void HandleTouchDownEvent(const AxisEvent& event) override;
76     void HandleTouchUpEvent(const AxisEvent& event) override;
77     void HandleTouchMoveEvent(const AxisEvent& event) override;
78     void HandleTouchCancelEvent(const AxisEvent& event) override;
79 
80     bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override;
81     GestureAcceptResult IsPanGestureAccept() const;
82     bool CalculateTruthFingers(bool isDirectionUp) const;
83     void UpdateTouchPointInVelocityTracker(const TouchEvent& event, bool end = false);
84 
85     void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback);
86     void ChangeFingers(int32_t fingers);
87     void ChangeDirection(const PanDirection& direction);
88     void ChangeDistance(double distance);
89     double GetMainAxisDelta();
90 
91     void OnResetStatus() override;
92     void OnSucceedCancel() override;
93 
GetTouchRestrict()94     const TouchRestrict& GetTouchRestrict() const
95     {
96         return touchRestrict_;
97     }
98 
99     PanDirection direction_;
100     double distance_ = 0.0;
101     double mouseDistance_ = 0.0;
102     AxisEvent lastAxisEvent_;
103     Offset averageDistance_;
104     std::map<int32_t, Offset> touchPointsDistance_;
105     Offset delta_;
106     double mainDelta_ = 0.0;
107     VelocityTracker velocityTracker_;
108     TimeStamp time_;
109 
110     Point globalPoint_;
111     TouchEvent lastTouchEvent_;
112     RefPtr<PanGestureOption> panGestureOption_;
113     OnPanFingersFunc onChangeFingers_;
114     OnPanDirectionFunc onChangeDirection_;
115     OnPanDistanceFunc onChangeDistance_;
116 
117     int32_t newFingers_ = 1;
118     double newDistance_ = 0.0;
119     PanDirection newDirection_;
120     bool isFlushTouchEventsEnd_ = false;
121     InputEventType inputEventType_ = InputEventType::TOUCH_SCREEN;
122     bool isForDrag_ = false;
123     bool isAllowMouse_ = true;
124 };
125 
126 } // namespace OHOS::Ace::NG
127 
128 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PAN_RECOGNIZER_H
129