From 3e65b41753bb3fdb6413e46c93a277c3793aa4fa Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Tue, 20 Aug 2013 11:35:54 +0200 Subject: [PATCH] Basic inertia scrolling --- input/gesture_detector.cpp | 37 +++++++++++++++++++++++++++++++++++-- input/gesture_detector.h | 5 +++++ ui/view.cpp | 4 ++-- ui/viewgroup.cpp | 19 ++++++++++++++++++- ui/viewgroup.h | 3 ++- 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/input/gesture_detector.cpp b/input/gesture_detector.cpp index 585e077adb..2815555bbb 100644 --- a/input/gesture_detector.cpp +++ b/input/gesture_detector.cpp @@ -2,11 +2,17 @@ // TODO: // Zoom gesture a la http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom-gesture/1847 +#include "base/logging.h" #include "base/timeutil.h" #include "input/gesture_detector.h" -GestureDetector::GestureDetector() : active_(0) { +const float estimatedInertiaDamping = 0.75f; + +GestureDetector::GestureDetector() + : active_(0), + estimatedInertiaX_(0.0f), + estimatedInertiaY_(0.0f) { } TouchInput GestureDetector::Update(const TouchInput &touch, const Bounds &bounds) { @@ -21,11 +27,21 @@ TouchInput GestureDetector::Update(const TouchInput &touch, const Bounds &bounds p.lastY = touch.y; p.distanceX = 0.0f; p.distanceY = 0.0f; + estimatedInertiaX_ = 0.0f; + estimatedInertiaY_ = 0.0f; } else if (touch.flags & TOUCH_UP) { p.down = false; } else { p.distanceX += fabsf(touch.x - p.lastX); p.distanceY += fabsf(touch.y - p.lastY); + + estimatedInertiaX_ += touch.x - p.lastX; + estimatedInertiaY_ += touch.y - p.lastY; + estimatedInertiaX_ *= estimatedInertiaDamping; + estimatedInertiaY_ *= estimatedInertiaDamping; + + ILOG("%f %f", estimatedInertiaX_, estimatedInertiaY_); + p.lastX = touch.x; p.lastY = touch.y; } @@ -47,13 +63,30 @@ TouchInput GestureDetector::Update(const TouchInput &touch, const Bounds &bounds return touch; } +void GestureDetector::UpdateFrame() { + estimatedInertiaX_ *= estimatedInertiaDamping; + estimatedInertiaY_ *= estimatedInertiaDamping; +} + bool GestureDetector::IsGestureActive(Gesture gesture) const { return (active_ & gesture) != 0; } void GestureDetector::GetGestureInfo(Gesture gesture, float info[4]) { - if (gesture == GESTURE_DRAG_VERTICAL) { + if (!(active_ & gesture)) { + memset(info, 0, sizeof(info)); + return; + } + + switch (gesture) { + case GESTURE_DRAG_HORIZONTAL: + info[0] = pointers[0].lastX - pointers[0].downX; + info[1] = estimatedInertiaX_; + break; + case GESTURE_DRAG_VERTICAL: info[0] = pointers[0].lastY - pointers[0].downY; + info[1] = estimatedInertiaY_; + break; } /* diff --git a/input/gesture_detector.h b/input/gesture_detector.h index 4c8178fd41..bbc1ad7cd4 100644 --- a/input/gesture_detector.h +++ b/input/gesture_detector.h @@ -18,6 +18,7 @@ class GestureDetector { public: GestureDetector(); TouchInput Update(const TouchInput &touch, const Bounds &bounds); + void UpdateFrame(); bool IsGestureActive(Gesture gesture) const; void GetGestureInfo(Gesture gesture, float info[4]); @@ -45,4 +46,8 @@ private: Pointer pointers[MAX_PTRS]; uint32_t active_; + + // For inertia estimation + float estimatedInertiaX_; + float estimatedInertiaY_; }; diff --git a/ui/view.cpp b/ui/view.cpp index d5161c0c08..79112e09a5 100644 --- a/ui/view.cpp +++ b/ui/view.cpp @@ -187,11 +187,11 @@ void Clickable::Touch(const TouchInput &input) { // TODO: O/X confirm preference for xperia play? bool IsAcceptKeyCode(int keyCode) { - return keyCode == NKCODE_SPACE || keyCode == NKCODE_ENTER || keyCode == NKCODE_Z || keyCode == NKCODE_BUTTON_A || keyCode == NKCODE_BUTTON_CROSS; + return keyCode == NKCODE_SPACE || keyCode == NKCODE_ENTER || keyCode == NKCODE_Z || keyCode == NKCODE_BUTTON_A || keyCode == NKCODE_BUTTON_CROSS || keyCode == NKCODE_BUTTON_1; } bool IsEscapeKeyCode(int keyCode) { - return keyCode == NKCODE_ESCAPE || keyCode == NKCODE_BACK || keyCode == NKCODE_BUTTON_CIRCLE || keyCode == NKCODE_BUTTON_B; + return keyCode == NKCODE_ESCAPE || keyCode == NKCODE_BACK || keyCode == NKCODE_BUTTON_CIRCLE || keyCode == NKCODE_BUTTON_B || keyCode == NKCODE_BUTTON_2; } void Clickable::Key(const KeyInput &key) { diff --git a/ui/viewgroup.cpp b/ui/viewgroup.cpp index a32a6c9ed8..85631cd787 100644 --- a/ui/viewgroup.cpp +++ b/ui/viewgroup.cpp @@ -533,11 +533,20 @@ void ScrollView::Key(const KeyInput &input) { ViewGroup::Key(input); } +const float friction = 0.92f; +const float stop_threshold = 0.1f; + void ScrollView::Touch(const TouchInput &input) { if ((input.flags & TOUCH_DOWN) && input.id == 0) { scrollStart_ = scrollPos_; + inertia_ = 0.0f; } - + if (input.flags & TOUCH_UP) { + float info[4]; + gesture_.GetGestureInfo(GESTURE_DRAG_VERTICAL, info); + inertia_ = info[1]; + } + TouchInput input2; if (CanScroll()) { input2 = gesture_.Update(input, bounds_); @@ -648,13 +657,21 @@ bool ScrollView::CanScroll() const { void ScrollView::Update(const InputState &input_state) { ViewGroup::Update(input_state); + gesture_.UpdateFrame(); if (scrollToTarget_) { + inertia_ = 0.0f; if (fabsf(scrollTarget_ - scrollPos_) < 0.5f) { scrollPos_ = scrollTarget_; scrollToTarget_ = false; } else { scrollPos_ += (scrollTarget_ - scrollPos_) * 0.3f; } + } else if (inertia_ != 0.0f && !gesture_.IsGestureActive(GESTURE_DRAG_VERTICAL)) { + scrollPos_ -= inertia_; + inertia_ *= friction; + if (fabsf(inertia_) < stop_threshold) + inertia_ = 0.0f; + ClampScrollPos(scrollPos_); } } diff --git a/ui/viewgroup.h b/ui/viewgroup.h index 1ca23863ff..2caca0a935 100644 --- a/ui/viewgroup.h +++ b/ui/viewgroup.h @@ -188,6 +188,7 @@ public: scrollMax_(0), scrollTarget_(0), scrollToTarget_(false), + inertia_(0), lastViewSize_(0.0f), scrollToTopOnSizeChange_(true) {} @@ -219,7 +220,7 @@ private: float scrollMax_; float scrollTarget_; bool scrollToTarget_; - + float inertia_; float lastViewSize_; bool scrollToTopOnSizeChange_; };