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_SWIPE_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_SWIPE_RECOGNIZER_H
18 
19 #include <functional>
20 #include <unordered_map>
21 
22 #include "core/event/touch_event.h"
23 #include "core/gestures/raw_recognizer.h"
24 
25 namespace OHOS::Ace {
26 
27 class ACE_FORCE_EXPORT SwipeEventInfo : public BaseEventInfo {
28     DECLARE_RELATIONSHIP_OF_CLASSES(SwipeEventInfo, BaseEventInfo)
29 
30 public:
31     enum class SwipeDirection {
32         LEFT = 0,
33         RIGHT,
34         UP,
35         DOWN,
36         NONE,
37     };
SwipeEventInfo(SwipeDirection swipeDirection,float distance)38     explicit SwipeEventInfo(SwipeDirection swipeDirection, float distance)
39         : BaseEventInfo("onswipe"), swipeDirection_(swipeDirection), distance_(distance)
40     {}
41     ~SwipeEventInfo() override = default;
42 
43     std::string ToJsonParamInfo() const;
44 
45 private:
46     SwipeDirection swipeDirection_ = SwipeDirection::LEFT;
47     float distance_ = 0.0f;
48 };
49 
50 using SwipeCallback = std::function<void(const SwipeEventInfo&)>;
51 using CatchSwipeCallback = std::function<void(const SwipeEventInfo&)>;
52 
53 class SwipeRecognizer : public TouchEventTarget {
54     DECLARE_ACE_TYPE(SwipeRecognizer, TouchEventTarget)
55 
56 public:
57     bool HandleEvent(const TouchEvent& point) override;
58     bool HandleSwipeEvent(const TouchEvent& point, uint32_t stage);
59 
60     bool DispatchEvent(const TouchEvent& point) override;
61 
SetSwipeCallback(const SwipeCallback & eventCallback,uint32_t stage)62     void SetSwipeCallback(const SwipeCallback& eventCallback, uint32_t stage)
63     {
64         swipeCallback_[stage] = eventCallback;
65     }
66 
SetCatchEventCallback(const CatchSwipeCallback & eventCallback,uint32_t stage)67     void SetCatchEventCallback(const CatchSwipeCallback& eventCallback, uint32_t stage)
68     {
69         swipeCatchCallback_[stage] = eventCallback;
70     }
71 
72 private:
73     std::unordered_map<int32_t, std::pair<TouchEvent, bool>> statusMap_;
74     SwipeCallback swipeCallback_[EventStage::SIZE];
75     CatchSwipeCallback swipeCatchCallback_[EventStage::SIZE];
76 };
77 
78 } // namespace OHOS::Ace
79 
80 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_SWIPE_RECOGNIZER_H
81