1 /* 2 * Copyright (c) 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_RECOGNIZER_GROUP_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_RECOGNIZER_GROUP_H 18 19 #include <list> 20 21 #include "base/geometry/ng/size_t.h" 22 #include "base/memory/ace_type.h" 23 #include "base/memory/referenced.h" 24 #include "core/components_ng/event/touch_event.h" 25 #include "core/components_ng/gestures/gesture_info.h" 26 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h" 27 28 namespace OHOS::Ace::NG { 29 30 class ACE_EXPORT RecognizerGroup : public MultiFingersRecognizer { 31 DECLARE_ACE_TYPE(RecognizerGroup, MultiFingersRecognizer); 32 33 public: RecognizerGroup(const std::vector<RefPtr<NGGestureRecognizer>> & recognizers)34 explicit RecognizerGroup(const std::vector<RefPtr<NGGestureRecognizer>>& recognizers) 35 { 36 for (const auto& recognizer : recognizers) { 37 if (recognizer && recognizer->SetGestureGroup(AceType::WeakClaim(this))) { 38 recognizers_.emplace_back(recognizer); 39 } 40 } 41 } 42 RecognizerGroup(std::list<RefPtr<NGGestureRecognizer>> && recognizers)43 explicit RecognizerGroup(std::list<RefPtr<NGGestureRecognizer>>&& recognizers) 44 { 45 recognizers_.clear(); 46 for (const auto& recognizer : recognizers) { 47 if (recognizer && recognizer->SetGestureGroup(AceType::WeakClaim(this))) { 48 recognizers_.emplace_back(recognizer); 49 } 50 } 51 } 52 53 ~RecognizerGroup() override = default; 54 55 void AddChildren(const std::list<RefPtr<NGGestureRecognizer>>& recognizers); 56 void RemoveRecognizerInGroup(const RefPtr<NGGestureRecognizer>& recognizer); 57 58 void OnFlushTouchEventsBegin() override; 59 void OnFlushTouchEventsEnd() override; 60 virtual RefereeState CheckStates(size_t touchId); 61 void ForceReject(); 62 RemainChildOnResetStatus()63 void RemainChildOnResetStatus() 64 { 65 remainChildOnResetStatus_ = true; 66 } 67 SetInnerFlag(bool value)68 virtual void SetInnerFlag(bool value) override { 69 NGGestureRecognizer::SetInnerFlag(value); 70 for (auto& recognizer : recognizers_) { 71 if (recognizer) { 72 recognizer->SetInnerFlag(value); 73 } 74 } 75 } 76 AttachFrameNode(const WeakPtr<NG::FrameNode> & node)77 void AttachFrameNode(const WeakPtr<NG::FrameNode>& node) override 78 { 79 TouchEventTarget::AttachFrameNode(node); 80 auto recognizers = GetGroupRecognizer(); 81 for (const auto& recognizer : recognizers) { 82 recognizer->AttachFrameNode(node); 83 } 84 } 85 86 const std::list<RefPtr<NGGestureRecognizer>>& GetGroupRecognizer(); 87 GetAxisDirection()88 Axis GetAxisDirection() override 89 { 90 uint8_t horizontalFlag = 0; 91 uint8_t verticalFlag = 0; 92 uint8_t freeFlag = 0; 93 for (const auto& recognizer : recognizers_) { 94 if (!recognizer) { 95 continue; 96 } 97 auto direction = recognizer->GetAxisDirection(); 98 switch (direction) { 99 case Axis::HORIZONTAL: 100 horizontalFlag = 0x1; 101 break; 102 case Axis::VERTICAL: 103 verticalFlag = 0x2; 104 break; 105 case Axis::FREE: 106 freeFlag = 0x3; 107 break; 108 default: 109 break; 110 } 111 } 112 uint8_t directionFlag = horizontalFlag | verticalFlag | freeFlag; 113 switch (directionFlag) { 114 case 0x1: 115 return Axis::HORIZONTAL; 116 case 0x2: 117 return Axis::VERTICAL; 118 case 0x3: 119 return Axis::FREE; 120 default: 121 return Axis::NONE; 122 } 123 } 124 SetChildrenTargetComponent(const RefPtr<TargetComponent> & targetComponent)125 void SetChildrenTargetComponent(const RefPtr<TargetComponent>& targetComponent) 126 { 127 for (const auto& child : recognizers_) { 128 if (child) { 129 child->SetTargetComponent(targetComponent); 130 } 131 } 132 } 133 ForceCleanRecognizer()134 void ForceCleanRecognizer() override 135 { 136 for (const auto& child : recognizers_) { 137 if (child) { 138 child->ForceCleanRecognizer(); 139 } 140 } 141 touchPoints_.clear(); 142 fingersId_.clear(); 143 fingerList_.clear(); 144 activeFingers_.clear(); 145 currentFingers_ = 0; 146 refereeState_ = RefereeState::READY; 147 disposal_ = GestureDisposal::NONE; 148 } 149 150 void ForceCleanRecognizerWithGroup() override; 151 152 void CleanRecognizerState() override; 153 SetIsPostEventResultRecursively(bool isPostEventResult)154 void SetIsPostEventResultRecursively(bool isPostEventResult) 155 { 156 for (const auto& item : recognizers_) { 157 item->SetIsPostEventResult(isPostEventResult); 158 auto group = AceType::DynamicCast<RecognizerGroup>(item); 159 if (group) { 160 group->SetIsPostEventResultRecursively(isPostEventResult); 161 } 162 } 163 } 164 SetResponseLinkRecognizersRecursively(const ResponseLinkResult & responseLinkResult)165 void SetResponseLinkRecognizersRecursively(const ResponseLinkResult& responseLinkResult) 166 { 167 for (const auto& item : recognizers_) { 168 auto group = AceType::DynamicCast<RecognizerGroup>(item); 169 if (group) { 170 group->SetResponseLinkRecognizersRecursively(responseLinkResult); 171 continue; 172 } 173 item->SetResponseLinkRecognizers(responseLinkResult); 174 } 175 } 176 CollectResponseLinkRecognizersRecursively(ResponseLinkResult & responseLinkResult)177 void CollectResponseLinkRecognizersRecursively(ResponseLinkResult& responseLinkResult) 178 { 179 for (const auto& item : recognizers_) { 180 auto group = AceType::DynamicCast<RecognizerGroup>(item); 181 if (group) { 182 group->CollectResponseLinkRecognizersRecursively(responseLinkResult); 183 continue; 184 } 185 auto recognizer = AceType::DynamicCast<NG::NGGestureRecognizer>(item); 186 if (recognizer) { 187 responseLinkResult.emplace_back(recognizer); 188 } 189 } 190 } 191 192 RefPtr<Gesture> CreateGestureFromRecognizer() const override; 193 virtual GestureMode GetGestureMode() const = 0; 194 195 void SetRecognizerInfoRecursively(const Offset& coordinateOffset, const RefPtr<NG::FrameNode>& node, 196 const RefPtr<NG::TargetComponent>& targetComponent, const GetEventTargetImpl& getEventTargetImpl); 197 198 void AddHittedRecognizerType(std::map<std::string, std::list<TouchTestResultInfo>>& hittedRecognizerInfo); 199 200 bool IsReady() override; 201 CheckGroupState()202 virtual bool CheckGroupState() 203 { 204 return false; 205 } 206 CheckAndSetRecognizerCleanFlag(const RefPtr<NGGestureRecognizer> & recognizer)207 virtual void CheckAndSetRecognizerCleanFlag(const RefPtr<NGGestureRecognizer>& recognizer) {} 208 209 protected: 210 void OnBeginGestureReferee(int32_t touchId, bool needUpdateChild = false) override; 211 void OnFinishGestureReferee(int32_t touchId, bool isBlocked = false) override; 212 void GroupAdjudicate(const RefPtr<NGGestureRecognizer>& recognizer, GestureDisposal disposal); 213 214 bool Existed(const RefPtr<NGGestureRecognizer>& recognizer); 215 bool CheckAllFailed(); 216 217 void OnResetStatus() override; 218 219 std::list<RefPtr<NGGestureRecognizer>> recognizers_; 220 bool remainChildOnResetStatus_ = false; 221 }; 222 223 } // namespace OHOS::Ace::NG 224 225 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_RECOGNIZER_GROUP_H 226