summaryrefslogtreecommitdiffstats
path: root/ui/aura/gestures
diff options
context:
space:
mode:
authorsadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-08 18:19:58 +0000
committersadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-08 18:19:58 +0000
commitc24b15748524cace93ed652eedfd7406693eaa65 (patch)
tree024274c72623818539288ecb11e217c267a35ee2 /ui/aura/gestures
parentd5c3e518cb9235ae9cc4c20a8e649ba11a931c49 (diff)
downloadchromium_src-c24b15748524cace93ed652eedfd7406693eaa65.zip
chromium_src-c24b15748524cace93ed652eedfd7406693eaa65.tar.gz
chromium_src-c24b15748524cace93ed652eedfd7406693eaa65.tar.bz2
Revert 120997 and 120996 because they cause compile failures in linux chomiumos.
BUG=none TEST=none Review URL: https://chromiumcodereview.appspot.com/9360022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@120998 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/aura/gestures')
-rw-r--r--ui/aura/gestures/gesture_point.cc32
-rw-r--r--ui/aura/gestures/gesture_point.h15
-rw-r--r--ui/aura/gestures/gesture_sequence.cc4
-rw-r--r--ui/aura/gestures/gesture_sequence.h2
-rw-r--r--ui/aura/gestures/velocity_calculator.cc109
-rw-r--r--ui/aura/gestures/velocity_calculator.h51
-rw-r--r--ui/aura/gestures/velocity_calculator_unittest.cc146
7 files changed, 24 insertions, 335 deletions
diff --git a/ui/aura/gestures/gesture_point.cc b/ui/aura/gestures/gesture_point.cc
index 6619b93..da772b5 100644
--- a/ui/aura/gestures/gesture_point.cc
+++ b/ui/aura/gestures/gesture_point.cc
@@ -6,7 +6,6 @@
#include <cmath>
-#include "base/basictypes.h"
#include "ui/aura/event.h"
#include "ui/base/events.h"
@@ -19,9 +18,8 @@ const double kMinimumTouchDownDurationInSecondsForClick = 0.01;
const double kMaximumSecondsBetweenDoubleClick = 0.7;
const int kMaximumTouchMoveInPixelsForClick = 20;
const float kMinFlickSpeedSquared = 550.f * 550.f;
-const int kBufferedPoints = 10;
-} // namespace
+} // namespace aura
namespace aura {
@@ -29,23 +27,20 @@ GesturePoint::GesturePoint()
: first_touch_time_(0.0),
last_touch_time_(0.0),
last_tap_time_(0.0),
- velocity_calculator_(kBufferedPoints) {
+ x_velocity_(0.0),
+ y_velocity_(0.0) {
}
-GesturePoint::~GesturePoint() {}
-
void GesturePoint::Reset() {
first_touch_time_ = last_touch_time_ = 0.0;
- velocity_calculator_.ClearHistory();
+ x_velocity_ = y_velocity_ = 0.0;
}
void GesturePoint::UpdateValues(const TouchEvent& event) {
- const int64 event_timestamp_microseconds =
- event.time_stamp().InMicroseconds();
if (event.type() == ui::ET_TOUCH_MOVED) {
- velocity_calculator_.PointSeen(event.x(),
- event.y(),
- event_timestamp_microseconds);
+ double interval(event.time_stamp().InSecondsF() - last_touch_time_);
+ x_velocity_ = (event.x() - last_touch_position_.x()) / interval;
+ y_velocity_ = (event.y() - last_touch_position_.y()) / interval;
}
last_touch_time_ = event.time_stamp().InSecondsF();
@@ -54,10 +49,8 @@ void GesturePoint::UpdateValues(const TouchEvent& event) {
if (event.type() == ui::ET_TOUCH_PRESSED) {
first_touch_time_ = last_touch_time_;
first_touch_position_ = event.location();
- velocity_calculator_.ClearHistory();
- velocity_calculator_.PointSeen(event.x(),
- event.y(),
- event_timestamp_microseconds);
+ x_velocity_ = 0.0;
+ y_velocity_ = 0.0;
}
}
@@ -90,7 +83,7 @@ bool GesturePoint::IsInScrollWindow(const TouchEvent& event) const {
!IsInsideManhattanSquare(event);
}
-bool GesturePoint::IsInFlickWindow(const TouchEvent& event) {
+bool GesturePoint::IsInFlickWindow(const TouchEvent& event) const {
return IsOverMinFlickSpeed() && event.type() != ui::ET_TOUCH_CANCELLED;
}
@@ -129,8 +122,9 @@ bool GesturePoint::IsSecondClickInsideManhattanSquare(
return manhattanDistance < kMaximumTouchMoveInPixelsForClick;
}
-bool GesturePoint::IsOverMinFlickSpeed() {
- return velocity_calculator_.VelocitySquared() > kMinFlickSpeedSquared;
+bool GesturePoint::IsOverMinFlickSpeed() const {
+ return (x_velocity_ * x_velocity_ + y_velocity_ * y_velocity_) >
+ kMinFlickSpeedSquared;
}
} // namespace aura
diff --git a/ui/aura/gestures/gesture_point.h b/ui/aura/gestures/gesture_point.h
index 4269865..f4b589d 100644
--- a/ui/aura/gestures/gesture_point.h
+++ b/ui/aura/gestures/gesture_point.h
@@ -7,7 +7,6 @@
#pragma once
#include "base/basictypes.h"
-#include "ui/aura/gestures/velocity_calculator.h"
#include "ui/gfx/point.h"
namespace aura {
@@ -26,7 +25,7 @@ enum GestureState {
class GesturePoint {
public:
GesturePoint();
- ~GesturePoint();
+ virtual ~GesturePoint() {}
// Resets various states.
void Reset();
@@ -46,7 +45,7 @@ class GesturePoint {
bool IsInClickWindow(const TouchEvent& event) const;
bool IsInDoubleClickWindow(const TouchEvent& event) const;
bool IsInScrollWindow(const TouchEvent& event) const;
- bool IsInFlickWindow(const TouchEvent& event);
+ bool IsInFlickWindow(const TouchEvent& event) const;
bool DidScroll(const TouchEvent& event, int distance) const;
const gfx::Point& first_touch_position() const {
@@ -64,8 +63,8 @@ class GesturePoint {
return last_touch_position_.y() - first_touch_position_.y();
}
- float XVelocity() { return velocity_calculator_.XVelocity(); }
- float YVelocity() { return velocity_calculator_.YVelocity(); }
+ float x_velocity() const { return x_velocity_; }
+ float y_velocity() const { return y_velocity_; }
float Distance(const GesturePoint& point) const;
@@ -75,7 +74,7 @@ class GesturePoint {
bool IsInSecondClickTimeWindow() const;
bool IsInsideManhattanSquare(const TouchEvent& event) const;
bool IsSecondClickInsideManhattanSquare(const TouchEvent& event) const;
- bool IsOverMinFlickSpeed();
+ bool IsOverMinFlickSpeed() const;
gfx::Point first_touch_position_;
double first_touch_time_;
@@ -85,7 +84,9 @@ class GesturePoint {
double last_tap_time_;
gfx::Point last_tap_position_;
- VelocityCalculator velocity_calculator_;
+ float x_velocity_;
+ float y_velocity_;
+
DISALLOW_COPY_AND_ASSIGN(GesturePoint);
};
diff --git a/ui/aura/gestures/gesture_sequence.cc b/ui/aura/gestures/gesture_sequence.cc
index 7611495..c14c686 100644
--- a/ui/aura/gestures/gesture_sequence.cc
+++ b/ui/aura/gestures/gesture_sequence.cc
@@ -417,10 +417,10 @@ bool GestureSequence::TouchDown(const TouchEvent& event,
}
bool GestureSequence::ScrollEnd(const TouchEvent& event,
- GesturePoint& point, Gestures* gestures) {
+ const GesturePoint& point, Gestures* gestures) {
if (point.IsInFlickWindow(event)) {
AppendScrollGestureEnd(point, point.last_touch_position(), gestures,
- point.XVelocity(), point.YVelocity());
+ point.x_velocity(), point.y_velocity());
} else {
AppendScrollGestureEnd(point, point.last_touch_position(), gestures,
0.f, 0.f);
diff --git a/ui/aura/gestures/gesture_sequence.h b/ui/aura/gestures/gesture_sequence.h
index 3e254f2..0ef0419 100644
--- a/ui/aura/gestures/gesture_sequence.h
+++ b/ui/aura/gestures/gesture_sequence.h
@@ -88,7 +88,7 @@ class GestureSequence {
const GesturePoint& point,
Gestures* gestures);
bool ScrollEnd(const TouchEvent& event,
- GesturePoint& point,
+ const GesturePoint& point,
Gestures* gestures);
bool PinchStart(const TouchEvent& event,
const GesturePoint& point,
diff --git a/ui/aura/gestures/velocity_calculator.cc b/ui/aura/gestures/velocity_calculator.cc
deleted file mode 100644
index 7b475fd..0000000
--- a/ui/aura/gestures/velocity_calculator.cc
+++ /dev/null
@@ -1,109 +0,0 @@
-// Copyright (c) 2012 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 "ui/aura/gestures/velocity_calculator.h"
-
-namespace aura {
-
-VelocityCalculator::VelocityCalculator(int buffer_size)
- : buffer_(new Point[buffer_size]) ,
- index_(0),
- num_valid_entries_(0),
- buffer_size_(buffer_size),
- x_velocity_(0),
- y_velocity_(0),
- velocities_stale_(false) {
-}
-
-VelocityCalculator::~VelocityCalculator() {}
-
-float VelocityCalculator::XVelocity() {
- if (velocities_stale_)
- UpdateVelocity();
- return x_velocity_;
-}
-
-float VelocityCalculator::YVelocity() {
- if (velocities_stale_)
- UpdateVelocity();
- return y_velocity_;
-}
-
-void VelocityCalculator::PointSeen(int x, int y, int64 time) {
- buffer_[index_].x = x;
- buffer_[index_].y = y;
- buffer_[index_].time = time;
-
- index_ = (index_ + 1) % buffer_size_;
- if (num_valid_entries_ < buffer_size_)
- ++num_valid_entries_;
-
- velocities_stale_ = true;
-}
-
-float VelocityCalculator::VelocitySquared() {
- if (velocities_stale_)
- UpdateVelocity();
- return x_velocity_ * x_velocity_ + y_velocity_ * y_velocity_;
-}
-
-void VelocityCalculator::UpdateVelocity() {
- // We don't have enough data to make a good estimate of the velocity.
- if (num_valid_entries_ < buffer_size_)
- return;
-
- // Where A_i = A[i] - mean(A)
- // x velocity = sum_i(x_i * t_i) / sum_i(t_i * t_i)
- // This is an Ordinary Least Squares Regression.
-
- float mean_x = 0;
- float mean_y = 0;
- int64 mean_time = 0;
-
- for (size_t i = 0; i < buffer_size_; ++i) {
- mean_x += buffer_[i].x;
- mean_y += buffer_[i].y;
- mean_time += buffer_[i].time;
- }
-
- // Minimize number of divides.
- const float buffer_size_i = 1.0f / buffer_size_;
-
- mean_x *= buffer_size_i;
- mean_y *= buffer_size_i;
-
- // The loss in accuracy due to rounding is insignificant compared to
- // the error due to the resolution of the timer.
- // Use integer division to avoid the cast to double, which would cause
- // VelocityCalculatorTest.IsAccurateWithLargeTimes to fail.
- mean_time /= buffer_size_;
-
- float xt = 0; // sum_i(x_i * t_i)
- float yt = 0; // sum_i(y_i * t_i)
- int64 tt = 0; // sum_i(t_i * t_i)
-
- int64 t_i;
-
- for (size_t i = 0; i < buffer_size_; ++i) {
- t_i = (buffer_[i].time - mean_time);
- xt += (buffer_[i].x - mean_x) * t_i;
- yt += (buffer_[i].y - mean_y) * t_i;
- tt += t_i * t_i;
- }
-
- // Convert time from microseconds to seconds.
- x_velocity_ = xt / (tt / 1000000.0f);
- y_velocity_ = yt / (tt / 1000000.0f);
- velocities_stale_ = false;
-}
-
-void VelocityCalculator::ClearHistory() {
- index_ = 0;
- num_valid_entries_ = 0;
- x_velocity_ = 0;
- y_velocity_ = 0;
- velocities_stale_ = false;
-}
-
-} // namespace aura
diff --git a/ui/aura/gestures/velocity_calculator.h b/ui/aura/gestures/velocity_calculator.h
deleted file mode 100644
index 28df0d7..0000000
--- a/ui/aura/gestures/velocity_calculator.h
+++ /dev/null
@@ -1,51 +0,0 @@
-// Copyright (c) 2012 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.
-
-#ifndef UI_AURA_GESTURES_VELOCITY_CALCULATOR_H_
-#define UI_AURA_GESTURES_VELOCITY_CALCULATOR_H_
-#pragma once
-
-#include <vector>
-
-#include "base/basictypes.h"
-#include "base/memory/scoped_ptr.h"
-#include "ui/aura/aura_export.h"
-
-namespace aura {
-
-class AURA_EXPORT VelocityCalculator {
- public:
- explicit VelocityCalculator(int bufferSize);
- ~VelocityCalculator();
- void PointSeen(int x, int y, int64 time);
- float XVelocity();
- float YVelocity();
- float VelocitySquared();
- void ClearHistory();
-
- private:
- struct Point {
- int x;
- int y;
- int64 time;
- };
-
- void UpdateVelocity();
-
- typedef scoped_array<Point> HistoryBuffer;
- HistoryBuffer buffer_;
-
- // index_ points directly after the last point added.
- int index_;
- size_t num_valid_entries_;
- const size_t buffer_size_;
- float x_velocity_;
- float y_velocity_;
- bool velocities_stale_;
- DISALLOW_COPY_AND_ASSIGN(VelocityCalculator);
-};
-
-} // namespace aura
-
-#endif // UI_AURA_GESTURES_VELOCITY_CALCULATOR_H_
diff --git a/ui/aura/gestures/velocity_calculator_unittest.cc b/ui/aura/gestures/velocity_calculator_unittest.cc
deleted file mode 100644
index 2955461..0000000
--- a/ui/aura/gestures/velocity_calculator_unittest.cc
+++ /dev/null
@@ -1,146 +0,0 @@
-// Copyright (c) 2012 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 "base/basictypes.h"
-#include "testing/gtest/include/gtest/gtest.h"
-#include "ui/aura/gestures/velocity_calculator.h"
-#include "ui/aura/test/aura_test_base.h"
-
-namespace aura {
-namespace test {
-
-namespace {
-
-static void AddPoints(VelocityCalculator* velocity_calculator,
- float x_increment,
- float y_increment,
- float time_increment_seconds,
- int num_points) {
- float x = 0;
- float y = 0;
- double time = 0;
-
- for (int i = 0; i < num_points; ++i) {
- velocity_calculator->PointSeen(x, y, time);
- x += x_increment;
- y += y_increment;
- time += time_increment_seconds * 1000000;
- }
-}
-
-} // namespace
-
-typedef AuraTestBase VelocityCalculatorTest;
-
-// Test that the velocity returned is reasonable
-TEST_F(VelocityCalculatorTest, ReturnsReasonableVelocity) {
- VelocityCalculator velocity_calculator(5);
- AddPoints(&velocity_calculator, 10, -10, 1, 7);
-
- EXPECT_GT(velocity_calculator.XVelocity(), 9.9);
- EXPECT_LT(velocity_calculator.XVelocity(), 10.1);
- EXPECT_GT(velocity_calculator.YVelocity(), -10.1);
- EXPECT_LT(velocity_calculator.YVelocity(), -9.9);
-
- velocity_calculator.PointSeen(9, -11, 5500000);
- velocity_calculator.PointSeen(21, -19, 6000000);
- velocity_calculator.PointSeen(30, -32, 6500000);
- velocity_calculator.PointSeen(38, -40, 7000000);
- velocity_calculator.PointSeen(50, -51, 7500000);
-
- EXPECT_GT(velocity_calculator.XVelocity(), 19);
- EXPECT_LT(velocity_calculator.XVelocity(), 21);
- EXPECT_GT(velocity_calculator.YVelocity(), -21);
- EXPECT_LT(velocity_calculator.YVelocity(), -19);
-
- // Significantly larger difference in position
- velocity_calculator.PointSeen(70, -70, 8000000);
-
- EXPECT_GT(velocity_calculator.XVelocity(), 20);
- EXPECT_LT(velocity_calculator.XVelocity(), 25);
- EXPECT_GT(velocity_calculator.YVelocity(), -25);
- EXPECT_LT(velocity_calculator.YVelocity(), -20);
-}
-
-TEST_F(VelocityCalculatorTest, IsAccurateWithLargeTimes) {
- VelocityCalculator velocity_calculator(5);
- int64 start_time = 0;
- velocity_calculator.PointSeen(9, -11, start_time);
- velocity_calculator.PointSeen(21, -19, start_time + 8);
- velocity_calculator.PointSeen(30, -32, start_time + 16);
- velocity_calculator.PointSeen(38, -40, start_time + 24);
- velocity_calculator.PointSeen(50, -51, start_time + 32);
-
- EXPECT_GT(velocity_calculator.XVelocity(), 1230000);
- EXPECT_LT(velocity_calculator.XVelocity(), 1260000);
- EXPECT_GT(velocity_calculator.YVelocity(), -1270000);
- EXPECT_LT(velocity_calculator.YVelocity(), -1240000);
-
- start_time = 1223372036800000000L;
- velocity_calculator.PointSeen(9, -11, start_time);
- velocity_calculator.PointSeen(21, -19, start_time + 8);
- velocity_calculator.PointSeen(30, -32, start_time + 16);
- velocity_calculator.PointSeen(38, -40, start_time + 24);
- velocity_calculator.PointSeen(50, -51, start_time + 32);
-
- EXPECT_GT(velocity_calculator.XVelocity(), 1230000);
- EXPECT_LT(velocity_calculator.XVelocity(), 1260000);
- EXPECT_GT(velocity_calculator.YVelocity(), -1270000);
- EXPECT_LT(velocity_calculator.YVelocity(), -124000);
-}
-
-// Check that the velocity returned is 0 if the velocity calculator
-// doesn't have enough data
-TEST_F(VelocityCalculatorTest, RequiresEnoughData) {
- VelocityCalculator velocity_calculator(5);
- EXPECT_EQ(velocity_calculator.XVelocity(), 0);
- EXPECT_EQ(velocity_calculator.YVelocity(), 0);
-
- AddPoints(&velocity_calculator, 10, 10, 1, 4);
-
- // We've only seen 4 points, the buffer size is 5
- // Since the buffer isn't full, return 0
- EXPECT_EQ(velocity_calculator.XVelocity(), 0);
- EXPECT_EQ(velocity_calculator.YVelocity(), 0);
-
- AddPoints(&velocity_calculator, 10, 10, 1, 1);
-
- EXPECT_GT(velocity_calculator.XVelocity(), 9.9);
- EXPECT_GT(velocity_calculator.YVelocity(), 9.9);
-}
-
-// Ensures ClearHistory behaves correctly
-TEST_F(VelocityCalculatorTest, ClearsHistory) {
- VelocityCalculator velocity_calculator(5);
- AddPoints(&velocity_calculator, 10, -10, 1, 7);
-
- EXPECT_GT(velocity_calculator.XVelocity(), 9.9);
- EXPECT_LT(velocity_calculator.XVelocity(), 10.1);
- EXPECT_GT(velocity_calculator.YVelocity(), -10.1);
- EXPECT_LT(velocity_calculator.YVelocity(), -9.9);
-
- velocity_calculator.ClearHistory();
-
- EXPECT_EQ(velocity_calculator.XVelocity(), 0);
- EXPECT_EQ(velocity_calculator.YVelocity(), 0);
-}
-
-// Ensure data older than the buffer size is ignored
-TEST_F(VelocityCalculatorTest, IgnoresOldData) {
- VelocityCalculator velocity_calculator(5);
- AddPoints(&velocity_calculator, 10, -10, 1, 7);
-
- EXPECT_GT(velocity_calculator.XVelocity(), 9.9);
- EXPECT_LT(velocity_calculator.XVelocity(), 10.1);
- EXPECT_GT(velocity_calculator.YVelocity(), -10.1);
- EXPECT_LT(velocity_calculator.YVelocity(), -9.9);
-
- AddPoints(&velocity_calculator, 0, 0, 1, 5);
-
- EXPECT_EQ(velocity_calculator.XVelocity(), 0);
- EXPECT_EQ(velocity_calculator.YVelocity(), 0);
-}
-
-} // namespace test
-} // namespace aura