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_PAN_RECOGNIZER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PAN_RECOGNIZER_H 18 19 #include <map> 20 21 #include "core/components_ng/event/drag_event.h" 22 #include "core/components_ng/gestures/pan_gesture.h" 23 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h" 24 25 namespace OHOS::Ace::NG { 26 enum class PanGestureState : int32_t; 27 28 class ACE_FORCE_EXPORT PanRecognizer : public MultiFingersRecognizer { 29 DECLARE_ACE_TYPE(PanRecognizer, MultiFingersRecognizer); 30 31 public: 32 PanRecognizer(int32_t fingers, const PanDirection& direction, double distance, bool isLimitFingerCount = false); 33 PanRecognizer(int32_t fingers, const PanDirection& direction, const PanDistanceMap& distanceMap, 34 bool isLimitFingerCount = false); 35 PanRecognizer(int32_t fingers, const PanDirection& direction, const PanDistanceMapDimension& distanceMap, 36 bool isLimitFingerCount = false); 37 38 explicit PanRecognizer(const RefPtr<PanGestureOption>& panGestureOption); 39 ~PanRecognizer()40 ~PanRecognizer() override 41 { 42 if (panGestureOption_ == nullptr) { 43 return; 44 } 45 panGestureOption_->GetOnPanFingersIds().erase(onChangeFingers_.GetId()); 46 panGestureOption_->GetOnPanDirectionIds().erase(onChangeDirection_.GetId()); 47 panGestureOption_->GetOnPanDistanceIds().erase(onChangeDistance_.GetId()); 48 } 49 50 void OnAccepted() override; 51 void OnRejected() override; 52 53 void OnFlushTouchEventsBegin() override; 54 void OnFlushTouchEventsEnd() override; 55 56 Axis GetAxisDirection() override; 57 58 void SetDirection(const PanDirection& direction); 59 SetIsForDrag(bool isForDrag)60 void SetIsForDrag(bool isForDrag) 61 { 62 isForDrag_ = isForDrag; 63 } 64 65 void SetMouseDistance(double distance); 66 SetIsAllowMouse(bool isAllowMouse)67 void SetIsAllowMouse(bool isAllowMouse) 68 { 69 isAllowMouse_ = isAllowMouse; 70 } 71 72 virtual RefPtr<GestureSnapshot> Dump() const override; 73 RefPtr<Gesture> CreateGestureFromRecognizer() const override; 74 void ForceCleanRecognizer() override; 75 void DumpVelocityInfo(int32_t fingerId); 76 77 double GetDistance() const; 78 double GetDistanceConfigFor(SourceTool sourceTool = SourceTool::UNKNOWN) const; 79 GetDirection()80 PanDirection GetDirection() const 81 { 82 return direction_; 83 } 84 85 void SetDistanceMap(const PanDistanceMap& distanceMap); 86 SetDistanceMap(const PanDistanceMapDimension & distanceMap)87 void SetDistanceMap(const PanDistanceMapDimension& distanceMap) 88 { 89 distanceMap_ = distanceMap; 90 } 91 GetDistanceMap()92 PanDistanceMapDimension GetDistanceMap() const 93 { 94 return distanceMap_; 95 } 96 97 void HandlePanGestureAccept(const GestureEvent& info, PanGestureState panGestureState, GestureCallbackType type); 98 SetPanEndCallback(const GestureEventFunc & panEndCallback)99 void SetPanEndCallback(const GestureEventFunc& panEndCallback) 100 { 101 panEndOnDisableState_ = std::make_unique<GestureEventFunc>(panEndCallback); 102 } 103 GetPanGestureOption()104 const RefPtr<PanGestureOption>& GetPanGestureOption() const 105 { 106 return panGestureOption_; 107 } 108 109 private: 110 class PanVelocity { 111 public: 112 Velocity GetVelocity(); 113 double GetMainAxisVelocity(); 114 void UpdateTouchPoint(int32_t id, const TouchEvent& event, bool end); 115 void Reset(int32_t id); 116 void ResetAll(); 117 void SetDirection(int32_t directionType); GetVelocityMap()118 const std::map<int32_t, VelocityTracker>& GetVelocityMap() const 119 { 120 return trackerMap_; 121 } 122 123 private: 124 int32_t GetFastestTracker(std::function<double(VelocityTracker&)>&& func); 125 std::map<int32_t, VelocityTracker> trackerMap_; 126 Axis axis_ = Axis::FREE; 127 }; 128 129 enum class GestureAcceptResult { 130 ACCEPT, 131 REJECT, 132 DETECTING, 133 }; 134 void HandleTouchDownEvent(const TouchEvent& event) override; 135 void HandleTouchUpEvent(const TouchEvent& event) override; 136 void HandleTouchMoveEvent(const TouchEvent& event) override; 137 void HandleTouchCancelEvent(const TouchEvent& event) override; 138 void HandleTouchDownEvent(const AxisEvent& event) override; 139 void HandleTouchUpEvent(const AxisEvent& event) override; 140 void HandleTouchMoveEvent(const AxisEvent& event) override; 141 void HandleTouchCancelEvent(const AxisEvent& event) override; 142 143 bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override; 144 GestureAcceptResult IsPanGestureAccept() const; 145 GestureAcceptResult IsPanGestureAcceptInAllDirection(double judgeDistance) const; 146 GestureAcceptResult IsPanGestureAcceptInHorizontalDirection(double judgeDistance) const; 147 GestureAcceptResult IsPanGestureAcceptInVerticalDirection(double judgeDistance) const; 148 bool JudgeVerticalDistance() const; 149 bool CalculateTruthFingers(bool isDirectionUp) const; 150 void UpdateTouchPointInVelocityTracker(const TouchEvent& touchEvent); 151 void UpdateAxisPointInVelocityTracker(const AxisEvent& event, bool end = false); 152 void UpdateTouchEventInfo(const TouchEvent& event); 153 Offset GetRawGlobalLocation(int32_t postEventNodeId); 154 155 void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback, GestureCallbackType type); 156 void HandleCallbackReports(const GestureEvent& info, GestureCallbackType type, PanGestureState panGestureState); 157 void HandleReports(const GestureEvent& info, GestureCallbackType type) override; 158 GestureJudgeResult TriggerGestureJudgeCallback(); 159 void UpdateGestureEventInfo(std::shared_ptr<PanGestureEvent>& info); 160 void ChangeFingers(int32_t fingers); 161 void ChangeDirection(const PanDirection& direction); 162 void ChangeDistance(double distance); 163 double GetMainAxisDelta(); 164 RefPtr<DragEventActuator> GetDragEventActuator(); 165 bool HandlePanAccept(); 166 bool HandlePanExtAccept(); 167 void GetGestureEventHalfInfo(GestureEvent* info); 168 GestureEvent GetGestureEventInfo(); 169 void ResetDistanceMap(); 170 171 void OnResetStatus() override; 172 void OnSucceedCancel() override; 173 174 void AddOverTimeTrace(); 175 176 void DispatchPanStartedToPerf(const TouchEvent& event); 177 GetTouchRestrict()178 const TouchRestrict& GetTouchRestrict() const 179 { 180 return touchRestrict_; 181 } 182 183 void UpdateAxisDeltaTransform(const AxisEvent& event); 184 185 PanDirection direction_; 186 double distance_ = 0.0; 187 double mouseDistance_ = 0.0; 188 int32_t newFingers_ = 1; 189 double newDistance_ = 0.0; 190 PanDirection newDirection_; 191 PanDistanceMapDimension distanceMap_; 192 PanDistanceMapDimension newDistanceMap_; 193 194 AxisEvent lastAxisEvent_; 195 Offset averageDistance_; 196 std::map<int32_t, Offset> touchPointsDistance_; 197 Offset delta_; 198 double mainDelta_ = 0.0; 199 PanVelocity panVelocity_; 200 TimeStamp time_; 201 bool isFlushTouchEventsEnd_ = false; 202 bool isForDrag_ = false; 203 bool isAllowMouse_ = true; 204 bool isStartTriggered_ = false; 205 Point globalPoint_; 206 TouchEvent lastTouchEvent_; 207 RefPtr<PanGestureOption> panGestureOption_; 208 209 OnPanFingersFunc onChangeFingers_; 210 OnPanDirectionFunc onChangeDirection_; 211 OnPanDistanceFunc onChangeDistance_; 212 // this callback will be triggered when pan end, but the enable state is false 213 std::unique_ptr<GestureEventFunc> panEndOnDisableState_; 214 int32_t lastAction_ = 0; 215 }; 216 217 } // namespace OHOS::Ace::NG 218 219 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PAN_RECOGNIZER_H 220