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_GESTURES_SLIDE_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_SLIDE_RECOGNIZER_H
18 
19 #include <cmath>
20 #include <functional>
21 #include <map>
22 
23 #include "core/gestures/multi_fingers_recognizer.h"
24 #include "core/pipeline/pipeline_context.h"
25 
26 namespace OHOS::Ace {
27 
28 class SlideRecognizer : public MultiFingersRecognizer {
29     DECLARE_ACE_TYPE(SlideRecognizer, MultiFingersRecognizer);
30 
31 public:
SlideRecognizer(const WeakPtr<PipelineBase> & context,int32_t fingers,const SwipeDirection & direction,double speed)32     SlideRecognizer(
33         const WeakPtr<PipelineBase>& context, int32_t fingers, const SwipeDirection& direction, double speed)
34         : direction_(direction), speed_(speed), context_(context)
35     {
36         fingers_ = fingers;
37         newSpeed_ = speed_;
38         newFingers_ = fingers_;
39         newDirection_ = direction_;
40     }
41 
42     void OnAccepted() override;
43     void OnRejected() override;
~SlideRecognizer()44     ~SlideRecognizer() override {}
45 
46 private:
47     enum class GestureAcceptResult {
48         ACCEPT,
49         REJECT,
50         DETECTING,
51     };
52     void HandleTouchDownEvent(const TouchEvent& event) override;
53     void HandleTouchUpEvent(const TouchEvent& event) override;
54     void HandleTouchMoveEvent(const TouchEvent& event) override;
55     void HandleTouchCancelEvent(const TouchEvent& event) override;
56     void HandleTouchDownEvent(const AxisEvent& event) override;
57     void HandleTouchUpEvent(const AxisEvent& event) override;
58     void HandleTouchMoveEvent(const AxisEvent& event) override;
59     void HandleTouchCancelEvent(const AxisEvent& event) override;
60     bool ReconcileFrom(const RefPtr<GestureRecognizer>& recognizer) override;
61     GestureAcceptResult ParseFingersOffset() const;
62     GestureAcceptResult ParseAxisOffset() const;
63     void Reset();
64     void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback);
65     void ChangeFingers(int32_t fingers);
66     void ChangeDirection(const SwipeDirection& direction);
67     void ChangeSpeed(double speed);
68     double ComputeAngle();
69     double ComputeAngle(AxisEvent event);
70 
GetTouchRestrict()71     const TouchRestrict& GetTouchRestrict() const
72     {
73         return touchRestrict_;
74     }
75 
76     SwipeDirection direction_;
77     double speed_ = 0.0;
78     WeakPtr<PipelineBase> context_;
79     std::map<int32_t, TouchEvent> touchPoints_;
80     std::map<int32_t, Offset> fingersDistance_;
81     TouchEvent lastTouchEvent_;
82     AxisEvent axisEventStart_;
83     double axisVerticalTotal_ = 0.0;
84     double axisHorizontalTotal_ = 0.0;
85     TimeStamp time_;
86     TimeStamp touchDownTime_;
87     bool slidingEnd_ = false;
88     bool slidingCancel_ = false;
89     Point globalPoint_;
90     OnSwipeFingersFunc onChangeFingers_;
91     OnSwipeDirectionFunc onChangeDirection_;
92     OnSwipeSpeedFunc onChangeSpeed_;
93     int32_t newFingers_ = 1;
94 
95     double angle_ = 0.0;
96     double initialAngle_ = 0.0;
97     double currentAngle_ = 0.0;
98     double resultAngle_ = 0.0;
99     double resultSpeed_ = 0.0;
100 
101     double newSpeed_ = 0.0;
102     SwipeDirection newDirection_;
103 };
104 
105 } // namespace OHOS::Ace
106 
107 #endif
108