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