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_GESTURES_VELOCITY_TRACKER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_VELOCITY_TRACKER_H 18 19 #include "base/geometry/axis.h" 20 #include "base/geometry/least_square_impl.h" 21 #include "base/geometry/offset.h" 22 #include "core/event/touch_event.h" 23 #include "core/gestures/velocity.h" 24 25 namespace OHOS::Ace { 26 27 class VelocityTracker final { 28 public: VelocityTracker()29 VelocityTracker() 30 { 31 static int32_t pointNum = SystemProperties::GetVelocityTrackerPointNumber(); 32 xAxis_.SetCountNum(pointNum); 33 yAxis_.SetCountNum(pointNum); 34 POINT_NUMBER = pointNum; 35 } VelocityTracker(Axis mainAxis)36 explicit VelocityTracker(Axis mainAxis) : mainAxis_(mainAxis) {} 37 ~VelocityTracker() = default; 38 39 static constexpr int32_t LEAST_SQUARE_PARAM_NUM = 3; 40 static constexpr float TOUCH_STILL_THRESHOLD = 0.5; 41 static constexpr float DURATION_LONGEST_THRESHOLD = 0.1; 42 static int32_t POINT_NUMBER; 43 Reset()44 void Reset() 45 { 46 lastPosition_.Reset(); 47 velocity_.Reset(); 48 delta_.Reset(); 49 isFirstPoint_ = true; 50 xAxis_.Reset(); 51 yAxis_.Reset(); 52 } 53 54 void UpdateTouchPoint(const TouchEvent& event, bool end = false, float range = TOUCH_STILL_THRESHOLD); 55 56 void UpdateTrackerPoint(double x, double y, const TimeStamp& time, bool end = false); 57 GetFirstTrackPoint()58 const TouchEvent& GetFirstTrackPoint() const 59 { 60 return firstTrackPoint_; 61 } 62 GetCurrentTrackPoint()63 const TouchEvent& GetCurrentTrackPoint() const 64 { 65 return currentTrackPoint_; 66 } 67 GetPosition()68 const Offset& GetPosition() const 69 { 70 return lastPosition_; 71 } 72 GetDelta()73 const Offset& GetDelta() const 74 { 75 return delta_; 76 } 77 GetVelocity()78 const Velocity& GetVelocity() 79 { 80 UpdateVelocity(); 81 return velocity_; 82 } 83 SetMainAxis(Axis axis)84 void SetMainAxis(Axis axis) 85 { 86 mainAxis_ = axis; 87 } 88 GetMainAxisPos()89 double GetMainAxisPos() const 90 { 91 switch (mainAxis_) { 92 case Axis::FREE: 93 return lastPosition_.GetDistance(); 94 case Axis::HORIZONTAL: 95 return lastPosition_.GetX(); 96 case Axis::VERTICAL: 97 return lastPosition_.GetY(); 98 default: 99 return 0.0; 100 } 101 } 102 GetMainAxisDeltaPos()103 double GetMainAxisDeltaPos() const 104 { 105 switch (mainAxis_) { 106 case Axis::FREE: 107 return delta_.GetDistance(); 108 case Axis::HORIZONTAL: 109 return delta_.GetX(); 110 case Axis::VERTICAL: 111 return delta_.GetY(); 112 default: 113 return 0.0; 114 } 115 } 116 GetMainAxisVelocity()117 double GetMainAxisVelocity() 118 { 119 UpdateVelocity(); 120 switch (mainAxis_) { 121 case Axis::FREE: 122 return velocity_.GetVelocityValue(); 123 case Axis::HORIZONTAL: 124 return velocity_.GetVelocityX(); 125 case Axis::VERTICAL: 126 return velocity_.GetVelocityY(); 127 default: 128 return 0.0; 129 } 130 } 131 132 void DumpVelocityPoints() const; 133 134 private: 135 double UpdateAxisVelocity(LeastSquareImpl& axis); 136 137 void UpdateVelocity(); 138 139 Axis mainAxis_ { Axis::FREE }; 140 TouchEvent firstTrackPoint_; 141 TouchEvent currentTrackPoint_; 142 Offset lastPosition_; 143 Velocity velocity_; 144 Offset delta_; 145 Offset offset_; 146 bool isFirstPoint_ = true; 147 TimeStamp lastTimePoint_; 148 TimeStamp firstPointTime_; 149 LeastSquareImpl xAxis_ { LEAST_SQUARE_PARAM_NUM }; 150 LeastSquareImpl yAxis_ { LEAST_SQUARE_PARAM_NUM }; 151 bool isVelocityDone_ = false; 152 }; 153 154 } // namespace OHOS::Ace 155 156 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_VELOCITY_TRACKER_H 157