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);
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 
GetFingers()43     int GetFingers()
44     {
45         return fingers_;
46     }
47 
ForceCleanRecognizer()48     void ForceCleanRecognizer() override
49     {
50         for (const auto& iter : touchPoints_) {
51             touchPoints_[iter.first] = {};
52         }
53         fingersId_.clear();
54         fingerList_.clear();
55         activeFingers_.clear();
56         currentFingers_ = 0;
57         refereeState_ = RefereeState::READY;
58         disposal_ = GestureDisposal::NONE;
59         lastPointEvent_.reset();
60         backupTouchPointsForSucceedBlock_.reset();
61     }
62 
63     void CleanRecognizerState() override;
64 
GetValidFingersCount()65     int32_t GetValidFingersCount() const
66     {
67         return std::count_if(touchPoints_.begin(), touchPoints_.end(),
68             [](const auto& item) { return item.second.type != TouchType::UNKNOWN; });
69     }
70 
GetTouchPointsSize()71     int32_t GetTouchPointsSize() const
72     {
73         return static_cast<int32_t>(touchPoints_.size());
74     }
75 
SetTouchPointsForSucceedBlock()76     void SetTouchPointsForSucceedBlock()
77     {
78         backupTouchPointsForSucceedBlock_ = touchPoints_;
79     }
80 
ResetTouchPointsForSucceedBlock()81     void ResetTouchPointsForSucceedBlock()
82     {
83         backupTouchPointsForSucceedBlock_.reset();
84     }
85 
86 protected:
87     void OnBeginGestureReferee(int32_t touchId, bool needUpdateChild = false) override
88     {
89         touchPoints_[touchId] = {};
90     }
91 
RemoveUnsupportEvent(int32_t touchId)92     void RemoveUnsupportEvent(int32_t touchId) override
93     {
94         if (touchPoints_.empty() || touchPoints_.find(touchId) == touchPoints_.end()) {
95             return;
96         }
97         touchPoints_.erase(touchId);
98     }
99 
100     void UpdateTouchPointWithAxisEvent(const AxisEvent& event);
101 
102     void OnFinishGestureReferee(int32_t touchId, bool isBlocked) override;
103 
OnResetStatus()104     void OnResetStatus() override
105     {
106         touchPoints_.clear();
107         fingersId_.clear();
108         fingerList_.clear();
109         activeFingers_.clear();
110         lastPointEvent_.reset();
111         currentFingers_ = 0;
112         refereeState_ = RefereeState::READY;
113         disposal_ = GestureDisposal::NONE;
114         backupTouchPointsForSucceedBlock_.reset();
115     }
116 
117     bool IsNeedResetStatus();
118 
IsActiveFinger(int32_t touchId)119     bool IsActiveFinger(int32_t touchId) const
120     {
121         return std::find(activeFingers_.begin(), activeFingers_.end(), touchId) != activeFingers_.end();
122     }
123 
124     std::string DumpGestureInfo() const;
125 
126     std::map<int32_t, TouchEvent> touchPoints_;
127     std::list<FingerInfo> fingerList_;
128     std::list<int32_t> activeFingers_;
129     std::shared_ptr<MMI::PointerEvent> lastPointEvent_;
130     int32_t fingers_ = 1;
131     std::optional<std::map<int32_t, TouchEvent>> backupTouchPointsForSucceedBlock_;
132 };
133 
134 } // namespace OHOS::Ace::NG
135 
136 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_MULTI_FINGERS_RECOGNIZER_H
137