Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: ui/events/gestures/fling_curve.cc

Issue 634373003: Consolidate content fling implementations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix GN build Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/events/gestures/fling_curve.h" 5 #include "ui/events/gestures/fling_curve.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 20 matching lines...) Expand all
31 } 31 }
32 32
33 } // namespace 33 } // namespace
34 34
35 namespace ui { 35 namespace ui {
36 36
37 FlingCurve::FlingCurve(const gfx::Vector2dF& velocity, 37 FlingCurve::FlingCurve(const gfx::Vector2dF& velocity,
38 base::TimeTicks start_timestamp) 38 base::TimeTicks start_timestamp)
39 : curve_duration_(GetTimeAtVelocity(0)), 39 : curve_duration_(GetTimeAtVelocity(0)),
40 start_timestamp_(start_timestamp), 40 start_timestamp_(start_timestamp),
41 previous_timestamp_(start_timestamp_),
41 time_offset_(0), 42 time_offset_(0),
42 position_offset_(0) { 43 position_offset_(0) {
44 DCHECK(!velocity.IsZero());
43 float max_start_velocity = std::max(fabs(velocity.x()), fabs(velocity.y())); 45 float max_start_velocity = std::max(fabs(velocity.x()), fabs(velocity.y()));
44 if (max_start_velocity > GetVelocityAtTime(0)) 46 if (max_start_velocity > GetVelocityAtTime(0))
45 max_start_velocity = GetVelocityAtTime(0); 47 max_start_velocity = GetVelocityAtTime(0);
46 CHECK_GT(max_start_velocity, 0); 48 CHECK_GT(max_start_velocity, 0);
47 49
48 displacement_ratio_ = gfx::Vector2dF(velocity.x() / max_start_velocity, 50 displacement_ratio_ = gfx::Vector2dF(velocity.x() / max_start_velocity,
49 velocity.y() / max_start_velocity); 51 velocity.y() / max_start_velocity);
50 time_offset_ = GetTimeAtVelocity(max_start_velocity); 52 time_offset_ = GetTimeAtVelocity(max_start_velocity);
51 position_offset_ = GetPositionAtTime(time_offset_); 53 position_offset_ = GetPositionAtTime(time_offset_);
52 last_timestamp_ = start_timestamp_ + base::TimeDelta::FromSecondsD(
53 curve_duration_ - time_offset_);
54 } 54 }
55 55
56 FlingCurve::~FlingCurve() { 56 FlingCurve::~FlingCurve() {
57 } 57 }
58 58
59 gfx::Vector2dF FlingCurve::GetScrollAmountAtTime(base::TimeTicks current) { 59 bool FlingCurve::ComputeScrollOffset(base::TimeTicks time,
60 if (current < start_timestamp_) 60 gfx::Vector2dF* offset,
61 return gfx::Vector2dF(); 61 gfx::Vector2dF* velocity) {
62 62 DCHECK(offset);
63 float displacement = 0; 63 DCHECK(velocity);
64 if (current < last_timestamp_) { 64 base::TimeDelta elapsed_time = time - start_timestamp_;
65 float time = time_offset_ + (current - start_timestamp_).InSecondsF(); 65 if (elapsed_time < base::TimeDelta()) {
66 CHECK_LT(time, curve_duration_); 66 *offset = gfx::Vector2dF();
67 displacement = GetPositionAtTime(time) - position_offset_; 67 *velocity = gfx::Vector2dF();
68 } else { 68 return true;
69 displacement = GetPositionAtTime(curve_duration_) - position_offset_;
70 } 69 }
71 70
72 gfx::Vector2dF scroll(displacement * displacement_ratio_.x(), 71 bool still_active = true;
73 displacement * displacement_ratio_.y()); 72 float scalar_offset;
74 gfx::Vector2dF scroll_increment(scroll.x() - cumulative_scroll_.x(), 73 float scalar_velocity;
75 scroll.y() - cumulative_scroll_.y()); 74 double offset_time = elapsed_time.InSecondsF() + time_offset_;
76 cumulative_scroll_ = scroll; 75 if (offset_time < curve_duration_) {
77 return scroll_increment; 76 scalar_offset = GetPositionAtTime(offset_time) - position_offset_;
77 scalar_velocity = GetVelocityAtTime(offset_time);
78 } else {
79 scalar_offset = GetPositionAtTime(curve_duration_) - position_offset_;
80 scalar_velocity = 0;
81 still_active = false;
82 }
83
84 *offset = gfx::ScaleVector2d(displacement_ratio_, scalar_offset);
85 *velocity = gfx::ScaleVector2d(displacement_ratio_, scalar_velocity);
86 return still_active;
87 }
88
89 bool FlingCurve::ComputeScrollDeltaAtTime(base::TimeTicks current,
90 gfx::Vector2dF* delta) {
91 DCHECK(delta);
92 if (current <= previous_timestamp_) {
93 *delta = gfx::Vector2dF();
94 return true;
95 }
96
97 previous_timestamp_ = current;
98
99 gfx::Vector2dF offset, velocity;
100 bool still_active = ComputeScrollOffset(current, &offset, &velocity);
101
102 *delta = offset - cumulative_scroll_;
103 cumulative_scroll_ = offset;
104 return still_active;
78 } 105 }
79 106
80 } // namespace ui 107 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698