• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
29     VelocityTracker() = default;
VelocityTracker(Axis mainAxis)30     explicit VelocityTracker(Axis mainAxis) : mainAxis_(mainAxis) {}
31     ~VelocityTracker() = default;
32 
Reset()33     void Reset()
34     {
35         lastPosition_.Reset();
36         velocity_.Reset();
37         delta_.Reset();
38         isFirstPoint_ = true;
39         xAxis_.Reset();
40         yAxis_.Reset();
41     }
42 
43     void UpdateTouchPoint(const TouchEvent& event, bool end = false);
44 
45     void UpdateTrackerPoint(double x, double y, const TimeStamp& time, bool end = false);
46 
GetFirstTrackPoint()47     const TouchEvent& GetFirstTrackPoint() const
48     {
49         return firstTrackPoint_;
50     }
51 
GetCurrentTrackPoint()52     const TouchEvent& GetCurrentTrackPoint() const
53     {
54         return currentTrackPoint_;
55     }
56 
GetPosition()57     const Offset& GetPosition() const
58     {
59         return lastPosition_;
60     }
61 
GetDelta()62     const Offset& GetDelta() const
63     {
64         return delta_;
65     }
66 
GetVelocity()67     const Velocity& GetVelocity()
68     {
69         UpdateVelocity();
70         return velocity_;
71     }
72 
SetMainAxis(Axis axis)73     void SetMainAxis(Axis axis)
74     {
75         mainAxis_ = axis;
76     }
77 
GetMainAxisPos()78     double GetMainAxisPos() const
79     {
80         switch (mainAxis_) {
81             case Axis::FREE:
82                 return lastPosition_.GetDistance();
83             case Axis::HORIZONTAL:
84                 return lastPosition_.GetX();
85             case Axis::VERTICAL:
86                 return lastPosition_.GetY();
87             default:
88                 return 0.0;
89         }
90     }
91 
GetMainAxisDeltaPos()92     double GetMainAxisDeltaPos() const
93     {
94         switch (mainAxis_) {
95             case Axis::FREE:
96                 return delta_.GetDistance();
97             case Axis::HORIZONTAL:
98                 return delta_.GetX();
99             case Axis::VERTICAL:
100                 return delta_.GetY();
101             default:
102                 return 0.0;
103         }
104     }
105 
GetMainAxisVelocity()106     double GetMainAxisVelocity()
107     {
108         UpdateVelocity();
109         switch (mainAxis_) {
110             case Axis::FREE:
111                 return velocity_.GetVelocityValue();
112             case Axis::HORIZONTAL:
113                 return velocity_.GetVelocityX();
114             case Axis::VERTICAL:
115                 return velocity_.GetVelocityY();
116             default:
117                 return 0.0;
118         }
119     }
120 
121 private:
122     void UpdateVelocity();
123 
124     Axis mainAxis_ { Axis::FREE };
125     TouchEvent firstTrackPoint_;
126     TouchEvent currentTrackPoint_;
127     Offset lastPosition_;
128     Velocity velocity_;
129     Offset delta_;
130     Offset offset_;
131     bool isFirstPoint_ = true;
132     TimeStamp lastTimePoint_;
133     TimeStamp firstPointTime_;
134     LeastSquareImpl xAxis_ { 3, 5 };
135     LeastSquareImpl yAxis_ { 3, 5 };
136     bool isVelocityDone_ = false;
137 };
138 
139 } // namespace OHOS::Ace
140 
141 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_VELOCITY_TRACKER_H
142