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 #include "core/components_ng/event/drag_event.h" 21 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h" 22 #include "core/components_ng/gestures/pan_gesture.h" 23 24 namespace OHOS::Ace::NG { 25 26 class PanRecognizer : public MultiFingersRecognizer { 27 DECLARE_ACE_TYPE(PanRecognizer, MultiFingersRecognizer); 28 29 public: 30 PanRecognizer(int32_t fingers, const PanDirection& direction, double distance); 31 32 explicit PanRecognizer(const RefPtr<PanGestureOption>& panGestureOption); 33 ~PanRecognizer()34 ~PanRecognizer() override 35 { 36 if (panGestureOption_ == nullptr) { 37 return; 38 } 39 panGestureOption_->GetOnPanFingersIds().erase(onChangeFingers_.GetId()); 40 panGestureOption_->GetOnPanDirectionIds().erase(onChangeDirection_.GetId()); 41 panGestureOption_->GetOnPanDistanceIds().erase(onChangeDistance_.GetId()); 42 } 43 44 void OnAccepted() override; 45 void OnRejected() override; 46 47 void OnFlushTouchEventsBegin() override; 48 void OnFlushTouchEventsEnd() override; 49 GetAxisDirection()50 Axis GetAxisDirection() override 51 { 52 if (direction_.type == PanDirection::ALL) { 53 return Axis::FREE; 54 } 55 if ((direction_.type & PanDirection::VERTICAL) == 0) { 56 return Axis::HORIZONTAL; 57 } 58 if ((direction_.type & PanDirection::HORIZONTAL) == 0) { 59 return Axis::VERTICAL; 60 } 61 return Axis::NONE; 62 } 63 64 void SetDirection(const PanDirection& direction); 65 SetIsForDrag(bool isForDrag)66 void SetIsForDrag(bool isForDrag) 67 { 68 isForDrag_ = isForDrag; 69 } 70 SetMouseDistance(double distance)71 void SetMouseDistance(double distance) 72 { 73 mouseDistance_ = distance; 74 } 75 SetIsAllowMouse(bool isAllowMouse)76 void SetIsAllowMouse(bool isAllowMouse) 77 { 78 isAllowMouse_ = isAllowMouse; 79 } 80 81 virtual RefPtr<GestureSnapshot> Dump() const override; 82 RefPtr<Gesture> CreateGestureFromRecognizer() const override; 83 void ForceCleanRecognizer() override; 84 AboutToAddCurrentFingers(int32_t touchId)85 bool AboutToAddCurrentFingers(int32_t touchId) override 86 { 87 if (fingersId_.find(touchId) != fingersId_.end()) { 88 return false; 89 } 90 currentFingers_++; 91 return true; 92 } 93 94 private: 95 enum class GestureAcceptResult { 96 ACCEPT, 97 REJECT, 98 DETECTING, 99 }; 100 void HandleTouchDownEvent(const TouchEvent& event) override; 101 void HandleTouchUpEvent(const TouchEvent& event) override; 102 void HandleTouchMoveEvent(const TouchEvent& event) override; 103 void HandleTouchCancelEvent(const TouchEvent& event) override; 104 void HandleTouchDownEvent(const AxisEvent& event) override; 105 void HandleTouchUpEvent(const AxisEvent& event) override; 106 void HandleTouchMoveEvent(const AxisEvent& event) override; 107 void HandleTouchCancelEvent(const AxisEvent& event) override; 108 109 bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override; 110 GestureAcceptResult IsPanGestureAccept() const; 111 bool CalculateTruthFingers(bool isDirectionUp) const; 112 void UpdateTouchPointInVelocityTracker(const TouchEvent& event, bool end = false); 113 114 void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback); 115 GestureJudgeResult TriggerGestureJudgeCallback(); 116 void ChangeFingers(int32_t fingers); 117 void ChangeDirection(const PanDirection& direction); 118 void ChangeDistance(double distance); 119 double GetMainAxisDelta(); 120 RefPtr<DragEventActuator> GetDragEventActuator(); 121 bool HandlePanAccept(); 122 123 void OnResetStatus() override; 124 void OnSucceedCancel() override; 125 GetTouchRestrict()126 const TouchRestrict& GetTouchRestrict() const 127 { 128 return touchRestrict_; 129 } 130 131 PanDirection direction_; 132 double distance_ = 0.0; 133 double mouseDistance_ = 0.0; 134 AxisEvent lastAxisEvent_ = { 135 .action = AxisAction::NONE, 136 }; 137 Offset averageDistance_; 138 std::map<int32_t, Offset> touchPointsDistance_; 139 Offset delta_; 140 double mainDelta_ = 0.0; 141 VelocityTracker velocityTracker_; 142 TimeStamp time_; 143 144 Point globalPoint_; 145 TouchEvent lastTouchEvent_; 146 RefPtr<PanGestureOption> panGestureOption_; 147 OnPanFingersFunc onChangeFingers_; 148 OnPanDirectionFunc onChangeDirection_; 149 OnPanDistanceFunc onChangeDistance_; 150 151 int32_t newFingers_ = 1; 152 double newDistance_ = 0.0; 153 PanDirection newDirection_; 154 bool isFlushTouchEventsEnd_ = false; 155 InputEventType inputEventType_ = InputEventType::TOUCH_SCREEN; 156 bool isForDrag_ = false; 157 bool isAllowMouse_ = true; 158 }; 159 160 } // namespace OHOS::Ace::NG 161 162 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PAN_RECOGNIZER_H 163