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_PAN_RECOGNIZER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_PAN_RECOGNIZER_H 18 19 #include <cmath> 20 #include <functional> 21 #include <map> 22 23 #include "core/gestures/multi_fingers_recognizer.h" 24 #include "core/pipeline/pipeline_context.h" 25 26 namespace OHOS::Ace { 27 28 class PanRecognizer : public MultiFingersRecognizer { 29 DECLARE_ACE_TYPE(PanRecognizer, MultiFingersRecognizer); 30 31 public: PanRecognizer(const WeakPtr<PipelineContext> & context,int32_t fingers,const PanDirection & direction,double distance)32 PanRecognizer( 33 const WeakPtr<PipelineContext>& context, int32_t fingers, const PanDirection& direction, double distance) 34 : direction_(direction), distance_(distance), context_(context) 35 { 36 fingers_ = fingers; 37 newFingers_ = fingers_; 38 newDistance_ = distance_; 39 newDirection_ = direction_; 40 } PanRecognizer(const WeakPtr<PipelineContext> & context,RefPtr<PanGestureOption> panGestureOption)41 PanRecognizer( 42 const WeakPtr<PipelineContext>& context, RefPtr<PanGestureOption> panGestureOption) 43 : panGestureOption_(panGestureOption) 44 { 45 auto newContext = context.Upgrade(); 46 if (!newContext) { 47 LOGE("fail to create pan recognizer due to context is nullptr"); 48 return; 49 } 50 uint32_t directNum = panGestureOption->GetDirection().type; 51 double distanceNumber = panGestureOption->GetDistance(); 52 int32_t fingersNumber = panGestureOption->GetFingers(); 53 54 double distance = LessNotEqual(distanceNumber, 0.0) ? DEFAULT_PAN_DISTANCE : distanceNumber; 55 distance_ = newContext->NormalizeToPx(Dimension(distance, DimensionUnit::VP)); 56 fingers_ = fingersNumber <= DEFAULT_PAN_FINGER ? DEFAULT_PAN_FINGER : fingersNumber; 57 58 if (directNum >= PanDirection::NONE && directNum <= PanDirection::ALL) { 59 direction_.type = directNum; 60 } 61 62 newFingers_ = fingers_; 63 newDistance_ = distance_; 64 newDirection_ = direction_; 65 66 PanFingersFuncType changeFingers = [weak = AceType::WeakClaim(this)](int32_t fingers) { 67 auto panRecognizer = weak.Upgrade(); 68 if (!panRecognizer) { 69 return; 70 } 71 panRecognizer->ChangeFingers(fingers); 72 }; 73 onChangeFingers_ = OnPanFingersFunc(changeFingers); 74 panGestureOption_->SetOnPanFingersId(onChangeFingers_); 75 76 PanDirectionFuncType changeDirection = [weak = AceType::WeakClaim(this)](const PanDirection& direction) { 77 auto panRecognizer = weak.Upgrade(); 78 if (!panRecognizer) { 79 return; 80 } 81 panRecognizer->ChangeDirection(direction); 82 }; 83 onChangeDirection_ = OnPanDirectionFunc(changeDirection); 84 panGestureOption_->SetOnPanDirectionId(onChangeDirection_); 85 86 PanDistanceFuncType changeDistance = [weak = AceType::WeakClaim(this)](double distance) { 87 auto panRecognizer = weak.Upgrade(); 88 if (!panRecognizer) { 89 return; 90 } 91 panRecognizer->ChangeDistance(distance); 92 }; 93 onChangeDistance_ = OnPanDistanceFunc(changeDistance); 94 panGestureOption_->SetOnPanDistanceId(onChangeDistance_); 95 } ~PanRecognizer()96 ~PanRecognizer() override 97 { 98 if (panGestureOption_ == nullptr) { 99 return; 100 } 101 panGestureOption_->GetOnPanFingersIds().erase(onChangeFingers_.GetId()); 102 panGestureOption_->GetOnPanDirectionIds().erase(onChangeDirection_.GetId()); 103 panGestureOption_->GetOnPanDistanceIds().erase(onChangeDistance_.GetId()); 104 } 105 106 void OnAccepted() override; 107 void OnRejected() override; 108 109 private: 110 enum class GestureAcceptResult { 111 ACCEPT, 112 REJECT, 113 DETECTING, 114 }; 115 void HandleTouchDownEvent(const TouchEvent& event) override; 116 void HandleTouchUpEvent(const TouchEvent& event) override; 117 void HandleTouchMoveEvent(const TouchEvent& event) override; 118 void HandleTouchCancelEvent(const TouchEvent& event) override; 119 bool ReconcileFrom(const RefPtr<GestureRecognizer>& recognizer) override; 120 GestureAcceptResult IsPanGestureAccept() const; 121 void Reset(); 122 void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback); 123 void ChangeFingers(int32_t fingers); 124 void ChangeDirection(const PanDirection& direction); 125 void ChangeDistance(double distance); 126 GetTouchRestrict()127 const TouchRestrict& GetTouchRestrict() const 128 { 129 return touchRestrict_; 130 } 131 132 PanDirection direction_; 133 double distance_ = 0.0; 134 WeakPtr<PipelineContext> context_; 135 std::map<int32_t, TouchEvent> touchPoints_; 136 Offset averageDistance_; 137 TimeStamp time_; 138 bool pendingEnd_ = false; 139 bool pendingCancel_ = false; 140 Point globalPoint_; 141 RefPtr<PanGestureOption> panGestureOption_; 142 OnPanFingersFunc onChangeFingers_; 143 OnPanDirectionFunc onChangeDirection_; 144 OnPanDistanceFunc onChangeDistance_; 145 int32_t newFingers_ = 1; 146 double newDistance_ = 0.0; 147 PanDirection newDirection_; 148 }; 149 150 } // namespace OHOS::Ace 151 152 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_PAN_RECOGNIZER_H 153