• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_RAW_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_RAW_RECOGNIZER_H
18 
19 #include <functional>
20 
21 #include "core/gestures/gesture_recognizer.h"
22 
23 namespace OHOS::Ace {
24 
25 class TouchEventInfo : public BaseEventInfo {
26     DECLARE_RELATIONSHIP_OF_CLASSES(TouchEventInfo, BaseEventInfo);
27 
28 public:
TouchEventInfo(const std::string & type)29     explicit TouchEventInfo(const std::string& type) : BaseEventInfo(type) {}
30     ~TouchEventInfo() override = default;
31 
AddTouchLocationInfo(TouchLocationInfo && info)32     void AddTouchLocationInfo(TouchLocationInfo&& info)
33     {
34         touches_.emplace_back(info);
35     }
AddChangedTouchLocationInfo(TouchLocationInfo && info)36     void AddChangedTouchLocationInfo(TouchLocationInfo&& info)
37     {
38         changedTouches_.emplace_back(info);
39     }
40 
GetTouches()41     const std::list<TouchLocationInfo>& GetTouches() const
42     {
43         return touches_;
44     }
GetChangedTouches()45     const std::list<TouchLocationInfo>& GetChangedTouches() const
46     {
47         return changedTouches_;
48     }
49 
50 private:
51     std::list<TouchLocationInfo> touches_;
52     std::list<TouchLocationInfo> changedTouches_;
53 };
54 
55 using OnTouchEventCallback = std::function<void(const TouchEventInfo&)>;
56 using CatchTouchEventCallback = std::function<void()>;
57 
58 namespace EventAction {
59 constexpr uint32_t SIZE = 2;
60 constexpr uint32_t ON = 0;
61 constexpr uint32_t CATCH = 1;
62 } // namespace EventAction
63 
64 namespace EventStage {
65 constexpr uint32_t SIZE = 2;
66 constexpr uint32_t CAPTURE = 0;
67 constexpr uint32_t BUBBLE = 1;
68 } // namespace EventStage
69 
70 namespace EventType {
71 constexpr uint32_t SIZE = 4;
72 constexpr uint32_t TOUCH_DOWN = 0;
73 constexpr uint32_t TOUCH_MOVE = 1;
74 constexpr uint32_t TOUCH_UP = 2;
75 constexpr uint32_t TOUCH_CANCEL = 3;
76 } // namespace EventType
77 
78 // Notice:
79 // The RawRecognizer does not participate in the gesture decision and is not affected by the gesture disambiguation.
80 // If there are other gesture recognizers that have accepted the gesture, the RawRecognizer can also accept the
81 // original event.
82 class RawRecognizer : public TouchEventTarget {
83     DECLARE_ACE_TYPE(RawRecognizer, TouchEventTarget);
84 
85 public:
86     void HandleEvent(const TouchEvent& point, uint32_t stage);
87     bool DispatchEvent(const TouchEvent& point) override;
88     bool HandleEvent(const TouchEvent& point) override;
89 
SetOnEventCallback(const OnTouchEventCallback & eventCallback,uint32_t stage,uint32_t eventType)90     void SetOnEventCallback(
91         const OnTouchEventCallback& eventCallback, uint32_t stage, uint32_t eventType)
92     {
93         onEventCallbacks_[stage][eventType] = eventCallback;
94     }
95 
SetCatchEventCallback(const CatchTouchEventCallback & eventCallback,uint32_t stage,uint32_t eventType)96     void SetCatchEventCallback(
97         const CatchTouchEventCallback& eventCallback, uint32_t stage, uint32_t eventType)
98     {
99         catcheventCallbacks_[stage][eventType] = eventCallback;
100     }
101 
SetOnTouchDown(const OnTouchEventCallback & onTouchDown)102     void SetOnTouchDown(const OnTouchEventCallback& onTouchDown)
103     {
104         onEventCallbacks_[EventStage::BUBBLE][EventType::TOUCH_DOWN] = onTouchDown;
105     }
106 
SetOnTouchMove(const OnTouchEventCallback & onTouchMove)107     void SetOnTouchMove(const OnTouchEventCallback& onTouchMove)
108     {
109         onEventCallbacks_[EventStage::BUBBLE][EventType::TOUCH_MOVE] = onTouchMove;
110     }
111 
SetOnTouchUp(const OnTouchEventCallback & onTouchUp)112     void SetOnTouchUp(const OnTouchEventCallback& onTouchUp)
113     {
114         onEventCallbacks_[EventStage::BUBBLE][EventType::TOUCH_UP] = onTouchUp;
115     }
116 
SetOnTouchCancel(const OnTouchEventCallback & onTouchCancel)117     void SetOnTouchCancel(const OnTouchEventCallback& onTouchCancel)
118     {
119         onEventCallbacks_[EventStage::BUBBLE][EventType::TOUCH_CANCEL] = onTouchCancel;
120     }
121 
122     // Coordinate offset is used to calculate the local location of the touch point in the render node.
SetCoordinateOffset(const Offset & coordinateOffset)123     void SetCoordinateOffset(const Offset& coordinateOffset)
124     {
125         coordinateOffset_ = coordinateOffset;
126     }
127 
128 private:
129     TouchEventInfo CreateTouchEventInfo(
130         const std::string& type, const TouchEvent& point, bool ignoreCurrent = false) const;
131 
132     OnTouchEventCallback onEventCallbacks_[EventStage::SIZE][EventType::SIZE];
133     CatchTouchEventCallback catcheventCallbacks_[EventStage::SIZE][EventType::SIZE];
134     TouchEvent lastPoint_;
135     Offset coordinateOffset_;
136     bool isFirstTrack_ = true;
137 };
138 
139 } // namespace OHOS::Ace
140 
141 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_RAW_RECOGNIZER_H
142