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