Basic inertia scrolling

This commit is contained in:
Henrik Rydgard committed 2013-08-20 11:35:54 +02:00
1 parent ba28246dd2
commit 3e65b41753
5 files changed
+62 -6

No files matched your search

+35 -2
View File
@@ -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;
}
/*
+5
View File
@@ -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_;
};
+2 -2
View File
@@ -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) {
+18 -1
View File
@@ -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_);
}
}
+2 -1
View File
@@ -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_;
};