• 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_MULTI_FINGERS_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_MULTI_FINGERS_RECOGNIZER_H
18 
19 #include <list>
20 #include <map>
21 
22 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h"
23 #include "core/event/touch_event.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 class MultiFingersRecognizer : public NGGestureRecognizer {
28     DECLARE_ACE_TYPE(MultiFingersRecognizer, NGGestureRecognizer);
29 
30 public:
31     MultiFingersRecognizer() = default;
32     explicit MultiFingersRecognizer(int32_t fingers, bool isLimitFingerCount = false);
33 
34     ~MultiFingersRecognizer() override = default;
35 
36     void UpdateFingerListInfo();
37 
CheckTouchId(int32_t touchId)38     bool CheckTouchId(int32_t touchId) override
39     {
40         return touchPoints_.find(touchId) != touchPoints_.end();
41     }
42 
GetTouchPoints()43     std::map<int32_t, TouchEvent> GetTouchPoints() override
44     {
45         return touchPoints_;
46     }
47 
GetFingers()48     int GetFingers()
49     {
50         return fingers_;
51     }
52 
SetLimitFingerCount(bool limitFingerCount)53     void SetLimitFingerCount(bool limitFingerCount)
54     {
55         isLimitFingerCount_ = limitFingerCount;
56     }
57 
GetLimitFingerCount()58     bool GetLimitFingerCount() const
59     {
60         return isLimitFingerCount_;
61     }
62 
CheckLimitFinger()63     bool CheckLimitFinger()
64     {
65         if (isLimitFingerCount_) {
66             if (static_cast<int32_t>(touchPoints_.size()) != fingers_) {
67                 return true;
68             }
69         }
70         return false;
71     }
72 
ForceCleanRecognizer()73     void ForceCleanRecognizer() override
74     {
75         for (const auto& iter : touchPoints_) {
76             touchPoints_[iter.first] = {};
77         }
78         fingersId_.clear();
79         fingerList_.clear();
80         activeFingers_.clear();
81         currentFingers_ = 0;
82         lastRefereeState_ = RefereeState::READY;
83         refereeState_ = RefereeState::READY;
84         disposal_ = GestureDisposal::NONE;
85         lastPointEvent_.reset();
86         backupTouchPointsForSucceedBlock_.reset();
87         preventBegin_ = false;
88     }
89 
90     void CleanRecognizerState() override;
91 
GetValidFingersCount()92     int32_t GetValidFingersCount() const
93     {
94         return std::count_if(touchPoints_.begin(), touchPoints_.end(),
95             [](const auto& item) { return item.second.type != TouchType::UNKNOWN; });
96     }
97 
GetTouchPointsSize()98     int32_t GetTouchPointsSize() const
99     {
100         return static_cast<int32_t>(touchPoints_.size());
101     }
102 
SetTouchPointsForSucceedBlock()103     void SetTouchPointsForSucceedBlock()
104     {
105         backupTouchPointsForSucceedBlock_ = touchPoints_;
106     }
107 
ResetTouchPointsForSucceedBlock()108     void ResetTouchPointsForSucceedBlock()
109     {
110         backupTouchPointsForSucceedBlock_.reset();
111     }
112 
113 protected:
114     void OnBeginGestureReferee(int32_t touchId, bool needUpdateChild = false) override
115     {
116         touchPoints_[touchId] = {};
117     }
118 
RemoveUnsupportEvent(int32_t touchId)119     void RemoveUnsupportEvent(int32_t touchId) override
120     {
121         if (touchPoints_.empty() || touchPoints_.find(touchId) == touchPoints_.end()) {
122             return;
123         }
124         touchPoints_.erase(touchId);
125     }
126 
127     void UpdateTouchPointWithAxisEvent(const AxisEvent& event);
128 
129     void OnFinishGestureReferee(int32_t touchId, bool isBlocked) override;
130 
OnResetStatus()131     void OnResetStatus() override
132     {
133         touchPoints_.clear();
134         fingersId_.clear();
135         fingerList_.clear();
136         activeFingers_.clear();
137         lastPointEvent_.reset();
138         currentFingers_ = 0;
139         lastRefereeState_ = RefereeState::READY;
140         refereeState_ = RefereeState::READY;
141         disposal_ = GestureDisposal::NONE;
142         backupTouchPointsForSucceedBlock_.reset();
143         preventBegin_ = false;
144     }
145 
146     bool IsNeedResetStatus();
147 
IsActiveFinger(int32_t touchId)148     bool IsActiveFinger(int32_t touchId) const
149     {
150         return std::find(activeFingers_.begin(), activeFingers_.end(), touchId) != activeFingers_.end();
151     }
152 
153     bool CheckFingerListInDownFingers(int32_t pointId) const;
154 
155     std::string DumpGestureInfo() const;
156 
157     std::map<int32_t, TouchEvent> touchPoints_;
158     std::list<FingerInfo> fingerList_;
159     std::list<int32_t> activeFingers_;
160     std::shared_ptr<MMI::PointerEvent> lastPointEvent_;
161     int32_t fingers_ = 1;
162     bool isLimitFingerCount_ = false;
163     std::optional<std::map<int32_t, TouchEvent>> backupTouchPointsForSucceedBlock_;
164 };
165 
166 } // namespace OHOS::Ace::NG
167 
168 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_MULTI_FINGERS_RECOGNIZER_H
169