forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointerEventList.cxx
More file actions
295 lines (276 loc) · 7.76 KB
/
pointerEventList.cxx
File metadata and controls
295 lines (276 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file pointerEventList.cxx
* @author jyelon
* @date 2007-09-20
*/
#include "pointerEventList.h"
#include "indent.h"
#include "config_event.h"
#include "clockObject.h"
#include "deg_2_rad.h"
#include "cmath.h"
TypeHandle PointerEventList::_type_handle;
/**
* Compute the difference between two angles. Returns a value in the range
* -180 to 180.
*/
INLINE double delta_angle(double angle1, double angle2) {
double deltang = angle2 - angle1;
while (deltang < -180.0) deltang += 360.0;
while (deltang > 180.0) deltang -= 360.0;
return deltang;
}
/**
* Compute the difference between two angles. Returns a value in the range
* -180 to 180.
*/
INLINE double normalize_angle(double angle) {
while (angle < 0.0) angle += 360.0;
while (angle > 360.0) angle -= 360.0;
return angle;
}
/**
*
*/
void PointerEventList::
output(std::ostream &out) const {
if (_events.empty()) {
out << "(no pointers)";
} else {
Events::const_iterator ei;
ei = _events.begin();
out << "(" << (*ei);
++ei;
while (ei != _events.end()) {
out << " " << (*ei);
++ei;
}
out << ")";
}
}
/**
*
*/
void PointerEventList::
write(std::ostream &out, int indent_level) const {
indent(out, indent_level) << _events.size() << " events:\n";
Events::const_iterator ei;
for (ei = _events.begin(); ei != _events.end(); ++ei) {
indent(out, indent_level + 2) << (*ei) << "\n";
}
}
/**
* Adds a new event from the given PointerData object.
*/
void PointerEventList::
add_event(const PointerData &data, int seq, double time) {
PointerEvent pe;
pe._in_window = data._in_window;
pe._type = data._type;
pe._id = data._id;
pe._xpos = data._xpos;
pe._ypos = data._ypos;
pe._pressure = data._pressure;
pe._sequence = seq;
pe._time = time;
if (_events.size() > 0) {
pe._dx = data._xpos - _events.back()._xpos;
pe._dy = data._ypos - _events.back()._ypos;
double ddx = pe._dx;
double ddy = pe._dy;
pe._length = csqrt(ddx*ddx + ddy*ddy);
if (pe._length > 0.0) {
pe._direction = normalize_angle(rad_2_deg(catan2(-ddy,ddx)));
} else {
pe._direction = _events.back()._direction;
}
pe._rotation = delta_angle(_events.back()._direction, pe._direction);
} else {
pe._dx = 0;
pe._dy = 0;
pe._length = 0.0;
pe._direction = 0.0;
pe._rotation = 0.0;
}
_events.push_back(pe);
}
/**
* Adds a new event to the end of the list. Automatically calculates the dx,
* dy, length, direction, and rotation for all but the first event.
*/
void PointerEventList::
add_event(bool in_win, int xpos, int ypos, int seq, double time) {
PointerEvent pe;
pe._in_window = in_win;
pe._xpos = xpos;
pe._ypos = ypos;
pe._sequence = seq;
pe._time = time;
if (_events.size() > 0) {
pe._dx = xpos - _events.back()._xpos;
pe._dy = ypos - _events.back()._ypos;
double ddx = pe._dx;
double ddy = pe._dy;
pe._length = csqrt(ddx*ddx + ddy*ddy);
if (pe._length > 0.0) {
pe._direction = normalize_angle(rad_2_deg(catan2(-ddy,ddx)));
} else {
pe._direction = _events.back()._direction;
}
pe._rotation = delta_angle(_events.back()._direction, pe._direction);
} else {
pe._dx = 0;
pe._dy = 0;
pe._length = 0.0;
pe._direction = 0.0;
pe._rotation = 0.0;
}
_events.push_back(pe);
}
/**
* Adds a new event to the end of the list based on the given mouse movement.
*/
void PointerEventList::
add_event(bool in_win, int xpos, int ypos, double xdelta, double ydelta, int seq, double time) {
PointerEvent pe;
pe._in_window = in_win;
pe._xpos = xpos;
pe._ypos = ypos;
pe._dx = xdelta;
pe._dy = ydelta;
pe._sequence = seq;
pe._time = time;
pe._length = csqrt(xdelta*xdelta + ydelta*ydelta);
if (pe._length > 0.0) {
pe._direction = normalize_angle(rad_2_deg(catan2(-ydelta,xdelta)));
} else if (!_events.empty()) {
pe._direction = _events.back()._direction;
} else {
pe._direction = 0.0;
}
if (!_events.empty()) {
pe._rotation = delta_angle(_events.back()._direction, pe._direction);
} else {
pe._rotation = 0.0;
}
_events.push_back(pe);
}
/**
* Returns true if the trail loops around the specified point.
*/
bool PointerEventList::
encircles(int x, int y) const {
int tot_events = _events.size();
if (tot_events < 3) {
return false;
}
int last = tot_events-1;
double dx = _events[last]._xpos - x;
double dy = _events[last]._ypos - y;
double lastang = rad_2_deg(catan2(dy, dx));
double total = 0.0;
for (int i=last; (i>=0) && (total < 360.0) && (total > -360.0); i--) {
dx = _events[i]._xpos - x;
dy = _events[i]._ypos - y;
if ((dx==0.0)&&(dy==0.0)) {
continue;
}
double angle = rad_2_deg(catan2(dy,dx));
double deltang = delta_angle(lastang, angle);
if (deltang * total < 0.0) {
total = 0.0;
}
total += deltang;
lastang = angle;
}
return (total > 360.0) || (total < -360.0);
}
/**
* returns the total angular deviation that the trail has made in the
* specified time period. A small number means that the trail is moving in a
* relatively straight line, a large number means that the trail is zig-
* zagging or spinning. The result is in degrees.
*/
double PointerEventList::
total_turns(double sec) const {
double old = ClockObject::get_global_clock()->get_frame_time() - sec;
int pos = _events.size()-1;
double tot = 0.0;
while ((pos >= 0)&&(_events[pos]._time >= old)) {
double rot = _events[pos]._rotation;
if (rot < 0.0) rot = -rot;
tot += rot;
}
return tot;
}
/**
* This function is not implemented yet. It is a work in progress. The
* intent is as follows:
*
* Returns a nonzero value if the mouse movements match the specified pattern.
* The higher the value, the better the match. The pattern is a sequence of
* compass directions (ie, "E", "NE", etc) separated by spaces. If rot is
* nonzero, then the pattern is rotated counterclockwise by the specified
* amount before testing. Seglen is the minimum length a mouse movement needs
* to be in order to be considered significant.
*/
double PointerEventList::
match_pattern(const std::string &ascpat, double rot, double seglen) {
// Convert the pattern from ascii to a more usable form.
vector_double pattern;
parse_pattern(ascpat, pattern);
// Apply the rotation to the pattern.
for (size_t i=0; i<pattern.size(); i++) {
pattern[i] = normalize_angle(pattern[i] + rot);
}
return 0.0;
}
/**
* Parses a pattern as used by match_pattern.
*/
void PointerEventList::
parse_pattern(const std::string &ascpat, vector_double &pattern) {
int chars = 0;
double dir = 180.0;
for (size_t i=0; i<ascpat.size(); i++) {
char c = ascpat[i];
double ang = -1.0;
if ((c=='E')||(c=='e')) ang=0.0;
else if ((c=='N')||(c=='n')) ang=90.0;
else if ((c=='W')||(c=='w')) ang=180.0;
else if ((c=='S')||(c=='s')) ang=270.0;
if (ang >= 0.0) {
double offset = delta_angle(dir, ang);
double newang = dir + offset;
dir = normalize_angle((dir * chars + newang) / (chars + 1));
chars += 1;
} else {
if ((c != ' ')&&(c != '\t')) {
event_cat.warning() <<
"Invalid pattern in PointerEventList::match_pattern\n";
pattern.clear();
return;
}
if (chars > 0) {
pattern.push_back(dir);
}
chars = 0;
dir = 180.0;
}
}
if (chars > 0) {
pattern.push_back(dir);
}
std::cerr << "Pattern: ";
for (int i=0; i<(int)pattern.size(); i++) {
std::cerr << pattern[i] << " ";
}
std::cerr << "\n";
}