~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to ext/native/input/gesture_detector.cpp

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Unfinished.
 
2
// TODO:
 
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
 
4
 
 
5
#include "base/logging.h"
 
6
#include "base/timeutil.h"
 
7
#include "input/gesture_detector.h"
 
8
 
 
9
 
 
10
const float estimatedInertiaDamping = 0.75f;
 
11
 
 
12
GestureDetector::GestureDetector()
 
13
        : active_(0),
 
14
                estimatedInertiaX_(0.0f),
 
15
                estimatedInertiaY_(0.0f) {
 
16
        memset(pointers, 0, sizeof(pointers));
 
17
}
 
18
 
 
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)) {
 
23
                p.down = true;
 
24
                p.downTime = time_now_d();
 
25
                p.downX = touch.x;
 
26
                p.downY = touch.y;
 
27
                p.lastX = touch.x;
 
28
                p.lastY = touch.y;
 
29
                p.distanceX = 0.0f;
 
30
                p.distanceY = 0.0f;
 
31
                estimatedInertiaX_ = 0.0f;
 
32
                estimatedInertiaY_ = 0.0f;
 
33
        } else if (touch.flags & TOUCH_UP) {
 
34
                p.down = false;
 
35
        } else {
 
36
                p.distanceX += fabsf(touch.x - p.lastX);
 
37
                p.distanceY += fabsf(touch.y - p.lastY);
 
38
 
 
39
                estimatedInertiaX_ += touch.x - p.lastX;
 
40
                estimatedInertiaY_ += touch.y - p.lastY;
 
41
                estimatedInertiaX_ *= estimatedInertiaDamping;
 
42
                estimatedInertiaY_ *= estimatedInertiaDamping;
 
43
 
 
44
                p.lastX = touch.x;
 
45
                p.lastY = touch.y;
 
46
        }
 
47
 
 
48
        if (touch.id == 0 && p.distanceY > p.distanceX) {
 
49
                if (p.down) {
 
50
                        double timeDown = time_now_d() - p.downTime;
 
51
                        if (!active_ && p.distanceY * timeDown > 3) {
 
52
                                active_ |= GESTURE_DRAG_VERTICAL;
 
53
                                // Kill the drag
 
54
                                TouchInput inp2 = touch;
 
55
                                inp2.flags = TOUCH_UP | TOUCH_CANCEL;
 
56
                                return inp2;
 
57
                        }
 
58
                } else {
 
59
                        active_ = 0;
 
60
                }
 
61
        }
 
62
 
 
63
        if (touch.id == 0 && p.distanceX > p.distanceY) {
 
64
                if (p.down) {
 
65
                        double timeDown = time_now_d() - p.downTime;
 
66
                        if (!active_ && p.distanceX * timeDown > 3) {
 
67
                                active_ |= GESTURE_DRAG_HORIZONTAL;
 
68
                                // Kill the drag
 
69
                                TouchInput inp2 = touch;
 
70
                                inp2.flags = TOUCH_UP | TOUCH_CANCEL;
 
71
                                return inp2;
 
72
                        }
 
73
                } else {
 
74
                        active_ = 0;
 
75
                }
 
76
        }
 
77
 
 
78
        return touch;
 
79
}
 
80
 
 
81
void GestureDetector::UpdateFrame() {
 
82
        estimatedInertiaX_ *= estimatedInertiaDamping;
 
83
        estimatedInertiaY_ *= estimatedInertiaDamping;
 
84
}
 
85
 
 
86
bool GestureDetector::IsGestureActive(Gesture gesture) const {
 
87
        return (active_ & gesture) != 0;
 
88
}
 
89
 
 
90
bool GestureDetector::GetGestureInfo(Gesture gesture, float info[4]) const {
 
91
        if (!(active_ & gesture)) {
 
92
                memset(info, 0, sizeof(float) * 4);
 
93
                return false;
 
94
        }
 
95
 
 
96
        memset(info, 0, sizeof(float) * 4);
 
97
 
 
98
        switch (gesture) {
 
99
        case GESTURE_DRAG_HORIZONTAL:
 
100
                info[0] = pointers[0].lastX - pointers[0].downX;
 
101
                info[1] = estimatedInertiaX_;
 
102
                return true;
 
103
        case GESTURE_DRAG_VERTICAL:
 
104
                info[0] = pointers[0].lastY - pointers[0].downY;
 
105
                info[1] = estimatedInertiaY_;
 
106
                return true;
 
107
        default:
 
108
                return false;
 
109
        }
 
110
}