Falkon Develop
Cross-platform Qt-based web browser
adv_recognizer.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the mouse gesture package.
3 * Copyright (C) 2006 Johan Thelin <e8johan@gmail.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the
8 * following conditions are met:
9 *
10 * - Redistributions of source code must retain the above
11 * copyright notice, this list of conditions and the
12 * following disclaimer.
13 * - Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions and
15 * the following disclaimer in the documentation and/or
16 * other materials provided with the distribution.
17 * - The names of its contributors may be used to endorse
18 * or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38#include "adv_recognizer.h"
39#include <algorithm>
40
41using namespace Gesture;
42
43RealTimeMouseGestureRecognizer::RealTimeMouseGestureRecognizer(int minimumMovement, double minimumMatch, bool allowDiagonals)
44 : minimumMatch(minimumMatch), allowDiagonals(allowDiagonals)
45{
46 minimumMovement2 = minimumMovement * minimumMovement;
47
48 directions.resize(64);
49 lastX = 0;
50 lastY = 0;
51 lastDirection = NoMatch;
52}
53
55{
56
57}
58
59
60const Direction dirsD[8] = {
61 Down,
62 Up,
63 Right,
64 Left,
67 UpLeft,
69};
70
72{
73 int dx, dy;
74
75 dx = x - lastX;
76 dy = y - lastY;
77
78 if (dx * dx + dy * dy < minimumMovement2) {
79 return;
80 }
81
82 Direction direction;
83
84 const int _directions[8][2] = {
85 {0, 15}, //down 0
86 {0, -15}, //up 1
87 {15, 0}, //right 2
88 { -15, 0}, //left 3
89 {10, 10}, //down right 4
90 { -10, 10}, //down left 5
91 { -10, -10}, //up left 6
92 {10, -10}//up right 7
93 };
94 int maxValue = 0;
95 int maxIndex = -1;
96
97 for (int i = 0; i < (allowDiagonals ? 8 : 4); i++) {
98 int value = dx * _directions[i][0] + dy * _directions[i][1];
99 if (value > maxValue) {
100 maxValue = value;
101 maxIndex = i;
102 }
103 }
104
105 direction = dirsD[maxIndex];
106
107 if (direction != lastDirection) {
108 directions.push_back(direction);
109 recognizeGesture();
110 }
111
112
113 lastX = x;
114 lastY = y;
115 lastDirection = direction;
116}
117
120 return a.directions.size() > b.directions.size();
121 }
122};
123
125{
126 gestures.push_back(gesture);
127 std::sort(gestures.begin(), gestures.end(), DirectionSort());
128}
129
131{
132 gestures.clear();
133}
134
135void RealTimeMouseGestureRecognizer::recognizeGesture()
136{
137 int first = gestures.size();
138
139 for (GestureList::const_iterator gi = gestures.begin(); gi != gestures.end(); ++gi) {
140 int readIndex = directions.getReadPointer();
141
142 try {
143 bool match = true;
144
145 for (DirectionList::const_iterator di = gi->directions.begin(); di != gi->directions.end() && match; ++di) {
146 Direction d = directions.pop();
147 if (*di != d) {
148 match = false;
149 }
150 }
151
152 if (match) {
153 if (gi->callbackClass) {
154 gi->callbackClass->callback();
155 }
156 return;
157 }
158 else {
159 first--;
160 directions.setReadPointerTo(readIndex);
161 }
162 }
163 catch (const std::exception &) {
164 directions.setReadPointerTo(readIndex);
165 }
166 }
167
168 if (first == 0) {
169 directions.pop();
170 }
171}
const Direction dirsD[8]
void addGestureDefinition(const GestureDefinition &gesture)
void resize(int s)
Definition: ring_buffer.h:117
void push_back(T item)
Definition: ring_buffer.h:71
void setReadPointerTo(int index)
Definition: ring_buffer.h:103
T & pop()
Definition: ring_buffer.h:86
int getReadPointer()
Definition: ring_buffer.h:109
int value(const QColor &c)
Definition: colors.cpp:238
i
Definition: i18n.py:23
bool operator()(const GestureDefinition &a, const GestureDefinition &b)