Index: remoting/client/fling_tracker.cc |
diff --git a/remoting/client/fling_tracker.cc b/remoting/client/fling_tracker.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..909e34a761b3a4a2a0dd0fe9ac7dd6e20397e125 |
--- /dev/null |
+++ b/remoting/client/fling_tracker.cc |
@@ -0,0 +1,100 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/client/fling_tracker.h" |
+ |
+#include <cmath> |
+ |
+namespace remoting { |
+ |
+namespace { |
+ |
+// Stop flinging if the speed drops below this. |
+// 1px per 16ms. i.e. 1px/frame. |
+const float kMinTrackSpeed = 0.0625f; |
+ |
+// The minimum fling duration (ms) needed to trigger the fling animation. This |
+// is to prevent unintentional fling with low velocity. |
+const float kMinFlingTime = 500.f; |
+ |
+// Multiplied with the speed. This is used to prevent abruptness at the |
+// beginning. |
+// TODO(yuweih): This may need to be normalized with the screen DPI. |
+const float kAmplituteFactor = 0.75f; |
+ |
+float GetDisplacement(float initial_speed_rate, |
+ float time_constant, |
+ float time_delta) { |
+ // x = v0 * (1 - e^(-t / T)) |
+ // This comes from a solution to the linear drag equation F=-kv |
+ float exp_deceleration = std::exp(-time_delta / time_constant); |
+ return initial_speed_rate * (1.f - exp_deceleration); |
+} |
+ |
+float GetSpeed(float initial_speed_rate, |
+ float time_constant, |
+ float time_delta) { |
+ // v = (1 / T) * v0 * e^(-t / T). |
+ // Derivative of the displacement. |
+ float exp_deceleration = std::exp(-time_delta / time_constant); |
+ return (1.f / time_constant) * initial_speed_rate * exp_deceleration; |
+} |
+ |
+} // namespace |
+ |
+FlingTracker::FlingTracker(float time_constant) |
+ : time_constant_(time_constant) {} |
+ |
+FlingTracker::~FlingTracker() {} |
+ |
+void FlingTracker::StartFling(float velocity_x, float velocity_y) { |
+ start_time_ = base::TimeTicks::Now(); |
+ |
+ initial_speed_rate_ = |
+ std::sqrt(velocity_x * velocity_x + velocity_y * velocity_y); |
+ |
+ if (GetSpeed(initial_speed_rate_, time_constant_, kMinFlingTime) < |
+ kMinTrackSpeed) { |
+ StopFling(); |
+ return; |
+ } |
+ |
+ velocity_ratio_x_ = velocity_x / initial_speed_rate_; |
+ velocity_ratio_y_ = velocity_y / initial_speed_rate_; |
+ |
+ initial_speed_rate_ *= kAmplituteFactor; |
nicholss
2017/05/10 19:34:05
Why start with attenuation?
Yuwei
2017/05/10 23:28:24
I was basically inspired by this article:
https:/
|
+} |
+ |
+void FlingTracker::StopFling() { |
+ initial_speed_rate_ = 0.f; |
+} |
+ |
+bool FlingTracker::IsFlingInProgress() const { |
+ return initial_speed_rate_ > 0; |
+} |
+ |
+bool FlingTracker::TrackPositions(base::TimeTicks time, float* x, float* y) { |
+ if (!IsFlingInProgress()) { |
+ return false; |
+ } |
+ |
+ float time_delta_ms = (time - start_time_).InMilliseconds(); |
+ |
+ float speed = GetSpeed(initial_speed_rate_, time_constant_, time_delta_ms); |
+ |
+ if (speed < kMinTrackSpeed) { |
+ StopFling(); |
+ return false; |
+ } |
+ |
+ float displacement = |
+ GetDisplacement(initial_speed_rate_, time_constant_, time_delta_ms); |
+ |
+ *x = displacement * velocity_ratio_x_; |
+ *y = displacement * velocity_ratio_y_; |
+ |
+ return true; |
+} |
+ |
+} // namespace remoting |