3
// 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
5
#include "base/logging.h"
6
#include "base/timeutil.h"
7
#include "input/gesture_detector.h"
10
const float estimatedInertiaDamping = 0.75f;
12
GestureDetector::GestureDetector()
14
estimatedInertiaX_(0.0f),
15
estimatedInertiaY_(0.0f) {
16
memset(pointers, 0, sizeof(pointers));
19
TouchInput GestureDetector::Update(const TouchInput &touch, const Bounds &bounds) {
20
// Mouse / 1-finger-touch control.
21
Pointer &p = pointers[touch.id];
22
if ((touch.flags & TOUCH_DOWN) && bounds.Contains(touch.x, touch.y)) {
24
p.downTime = time_now_d();
31
estimatedInertiaX_ = 0.0f;
32
estimatedInertiaY_ = 0.0f;
33
} else if (touch.flags & TOUCH_UP) {
36
p.distanceX += fabsf(touch.x - p.lastX);
37
p.distanceY += fabsf(touch.y - p.lastY);
39
estimatedInertiaX_ += touch.x - p.lastX;
40
estimatedInertiaY_ += touch.y - p.lastY;
41
estimatedInertiaX_ *= estimatedInertiaDamping;
42
estimatedInertiaY_ *= estimatedInertiaDamping;
48
if (touch.id == 0 && p.distanceY > p.distanceX) {
50
double timeDown = time_now_d() - p.downTime;
51
if (!active_ && p.distanceY * timeDown > 3) {
52
active_ |= GESTURE_DRAG_VERTICAL;
54
TouchInput inp2 = touch;
55
inp2.flags = TOUCH_UP | TOUCH_CANCEL;
63
if (touch.id == 0 && p.distanceX > p.distanceY) {
65
double timeDown = time_now_d() - p.downTime;
66
if (!active_ && p.distanceX * timeDown > 3) {
67
active_ |= GESTURE_DRAG_HORIZONTAL;
69
TouchInput inp2 = touch;
70
inp2.flags = TOUCH_UP | TOUCH_CANCEL;
81
void GestureDetector::UpdateFrame() {
82
estimatedInertiaX_ *= estimatedInertiaDamping;
83
estimatedInertiaY_ *= estimatedInertiaDamping;
86
bool GestureDetector::IsGestureActive(Gesture gesture) const {
87
return (active_ & gesture) != 0;
90
bool GestureDetector::GetGestureInfo(Gesture gesture, float info[4]) const {
91
if (!(active_ & gesture)) {
92
memset(info, 0, sizeof(float) * 4);
96
memset(info, 0, sizeof(float) * 4);
99
case GESTURE_DRAG_HORIZONTAL:
100
info[0] = pointers[0].lastX - pointers[0].downX;
101
info[1] = estimatedInertiaX_;
103
case GESTURE_DRAG_VERTICAL:
104
info[0] = pointers[0].lastY - pointers[0].downY;
105
info[1] = estimatedInertiaY_;