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 27 class PanRecognizer : public MultiFingersRecognizer { 28 DECLARE_ACE_TYPE(PanRecognizer, MultiFingersRecognizer); 29 30 public: 31 PanRecognizer(int32_t fingers, const PanDirection& direction, double distance, bool isLimitFingerCount = false); 32 33 explicit PanRecognizer(const RefPtr<PanGestureOption>& panGestureOption); 34 ~PanRecognizer()35 ~PanRecognizer() override 36 { 37 if (panGestureOption_ == nullptr) { 38 return; 39 } 40 panGestureOption_->GetOnPanFingersIds().erase(onChangeFingers_.GetId()); 41 panGestureOption_->GetOnPanDirectionIds().erase(onChangeDirection_.GetId()); 42 panGestureOption_->GetOnPanDistanceIds().erase(onChangeDistance_.GetId()); 43 } 44 45 void OnAccepted() override; 46 void OnRejected() override; 47 48 void OnFlushTouchEventsBegin() override; 49 void OnFlushTouchEventsEnd() override; 50 51 Axis GetAxisDirection() override; 52 53 void SetDirection(const PanDirection& direction); 54 SetIsForDrag(bool isForDrag)55 void SetIsForDrag(bool isForDrag) 56 { 57 isForDrag_ = isForDrag; 58 } 59 SetMouseDistance(double distance)60 void SetMouseDistance(double distance) 61 { 62 mouseDistance_ = distance; 63 } 64 SetIsAllowMouse(bool isAllowMouse)65 void SetIsAllowMouse(bool isAllowMouse) 66 { 67 isAllowMouse_ = isAllowMouse; 68 } 69 70 virtual RefPtr<GestureSnapshot> Dump() const override; 71 RefPtr<Gesture> CreateGestureFromRecognizer() const override; 72 void ForceCleanRecognizer() override; 73 74 void DumpVelocityInfo(int32_t fingerId); 75 GetDistance()76 double GetDistance() const 77 { 78 return distance_; 79 } 80 GetDirection()81 PanDirection GetDirection() const 82 { 83 return direction_; 84 } 85 86 private: 87 class PanVelocity { 88 public: 89 Velocity GetVelocity(); 90 double GetMainAxisVelocity(); 91 void UpdateTouchPoint(int32_t id, const TouchEvent& event, bool end); 92 void Reset(int32_t id); 93 void ResetAll(); 94 void SetDirection(int32_t directionType); GetVelocityMap()95 const std::map<int32_t, VelocityTracker>& GetVelocityMap() const 96 { 97 return trackerMap_; 98 } 99 100 private: 101 int32_t GetFastestTracker(std::function<double(VelocityTracker&)>&& func); 102 std::map<int32_t, VelocityTracker> trackerMap_; 103 Axis axis_ = Axis::FREE; 104 }; 105 106 enum class GestureAcceptResult { 107 ACCEPT, 108 REJECT, 109 DETECTING, 110 }; 111 void HandleTouchDownEvent(const TouchEvent& event) override; 112 void HandleTouchUpEvent(const TouchEvent& event) override; 113 void HandleTouchMoveEvent(const TouchEvent& event) override; 114 void HandleTouchCancelEvent(const TouchEvent& event) override; 115 void HandleTouchDownEvent(const AxisEvent& event) override; 116 void HandleTouchUpEvent(const AxisEvent& event) override; 117 void HandleTouchMoveEvent(const AxisEvent& event) override; 118 void HandleTouchCancelEvent(const AxisEvent& event) override; 119 void UpdateTouchEventInfo(const TouchEvent& event); 120 121 bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override; 122 GestureAcceptResult IsPanGestureAccept() const; 123 bool CalculateTruthFingers(bool isDirectionUp) const; 124 void UpdateTouchPointInVelocityTracker(const TouchEvent& touchEvent); 125 void UpdateAxisPointInVelocityTracker(const AxisEvent& event, bool end = false); 126 127 Offset GetRawGlobalLocation(int32_t postEventNodeId); 128 129 void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback); 130 GestureJudgeResult TriggerGestureJudgeCallback(); 131 void ChangeFingers(int32_t fingers); 132 void ChangeDirection(const PanDirection& direction); 133 void ChangeDistance(double distance); 134 double GetMainAxisDelta(); 135 RefPtr<DragEventActuator> GetDragEventActuator(); 136 bool HandlePanAccept(); 137 GestureEvent GetGestureEventInfo(); 138 139 void OnResetStatus() override; 140 void OnSucceedCancel() override; 141 142 void AddOverTimeTrace(); 143 144 void DispatchPanStartedToPerf(const TouchEvent& event); 145 GetTouchRestrict()146 const TouchRestrict& GetTouchRestrict() const 147 { 148 return touchRestrict_; 149 } 150 151 PanDirection direction_; 152 double distance_ = 0.0; 153 double mouseDistance_ = 0.0; 154 AxisEvent lastAxisEvent_; 155 Offset averageDistance_; 156 std::map<int32_t, Offset> touchPointsDistance_; 157 Offset delta_; 158 double mainDelta_ = 0.0; 159 PanVelocity panVelocity_; 160 TimeStamp time_; 161 162 Point globalPoint_; 163 TouchEvent lastTouchEvent_; 164 RefPtr<PanGestureOption> panGestureOption_; 165 OnPanFingersFunc onChangeFingers_; 166 OnPanDirectionFunc onChangeDirection_; 167 OnPanDistanceFunc onChangeDistance_; 168 169 int32_t newFingers_ = 1; 170 double newDistance_ = 0.0; 171 PanDirection newDirection_; 172 bool isFlushTouchEventsEnd_ = false; 173 bool isForDrag_ = false; 174 bool isAllowMouse_ = true; 175 bool isStartTriggered_ = false; 176 }; 177 178 } // namespace OHOS::Ace::NG 179 180 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PAN_RECOGNIZER_H 181