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_SLIDE_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_SLIDE_RECOGNIZER_H
18 
19 #include <cmath>
20 #include <functional>
21 #include <optional>
22 
23 #include "base/utils/type_definition.h"
24 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h"
25 #include "core/event/touch_event.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 class SwipeRecognizer : public MultiFingersRecognizer {
30     DECLARE_ACE_TYPE(SwipeRecognizer, MultiFingersRecognizer);
31 
32 public:
SwipeRecognizer(int32_t fingers,const SwipeDirection & direction,double speed)33     SwipeRecognizer(int32_t fingers, const SwipeDirection& direction, double speed)
34         : MultiFingersRecognizer(fingers), direction_(direction), speed_(speed)
35     {}
36     ~SwipeRecognizer() override = default;
37 
38     void OnAccepted() override;
39     void OnRejected() override;
40 
GetAxisDirection()41     Axis GetAxisDirection() override
42     {
43         switch (direction_.type) {
44             case SwipeDirection::HORIZONTAL:
45                 return Axis::HORIZONTAL;
46             case SwipeDirection::VERTICAL:
47                 return Axis::VERTICAL;
48             case SwipeDirection::ALL:
49                 return Axis::FREE;
50             default:
51                 return Axis::NONE;
52         }
53     }
54 
55     virtual RefPtr<GestureSnapshot> Dump() const override;
56 
57 private:
58     void HandleTouchDownEvent(const TouchEvent& event) override;
59     void HandleTouchUpEvent(const TouchEvent& event) override;
60     void HandleTouchMoveEvent(const TouchEvent& event) override;
61     void HandleTouchCancelEvent(const TouchEvent& event) override;
62     void HandleTouchDownEvent(const AxisEvent& event) override;
63     void HandleTouchUpEvent(const AxisEvent& event) override;
64     void HandleTouchMoveEvent(const AxisEvent& event) override;
65     void HandleTouchCancelEvent(const AxisEvent& event) override;
66     bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override;
67 
68     void OnResetStatus() override;
69 
70     void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback);
71     GestureJudgeResult TriggerGestureJudgeCallback();
72 
73     bool CheckAngle(double angle);
74 
75     SwipeDirection direction_;
76     double speed_ = 0.0;
77     TouchEvent lastTouchEvent_;
78     std::map<int32_t, TouchEvent> downEvents_;
79 
80     AxisEvent axisEventStart_;
81     AxisEvent lastAxisEvent_;
82     Offset axisOffset_;
83 
84     TimeStamp time_;
85     TimeStamp touchDownTime_;
86 
87     Point globalPoint_;
88 
89     std::optional<double> prevAngle_;
90 
91     double resultSpeed_ = 0.0;
92 
93     std::set<int32_t> matchedTouch_;
94 };
95 
96 } // namespace OHOS::Ace::NG
97 
98 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_SLIDE_RECOGNIZER_H
99