forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputDevice.I
More file actions
410 lines (370 loc) · 10 KB
/
inputDevice.I
File metadata and controls
410 lines (370 loc) · 10 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* 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 inputDevice.I
* @author rdb
* @date 2015-12-11
*/
#include "config_device.h"
/**
*
*/
INLINE InputDevice::
InputDevice() {
_button_events = new ButtonEventList;
_pointer_events = new PointerEventList;
}
/**
* Returns a human-readable name for the device. Not necessarily unique.
*/
INLINE std::string InputDevice::
get_name() const {
LightMutexHolder holder(_lock);
return _name;
}
/**
* Returns a string containing the manufacturer of the device, if this
* information is known.
*/
INLINE std::string InputDevice::
get_manufacturer() const {
LightMutexHolder holder(_lock);
return _manufacturer;
}
/**
* Returns a string containing the serial number of the device, if this
* information is known.
*/
INLINE std::string InputDevice::
get_serial_number() const {
LightMutexHolder holder(_lock);
return _serial_number;
}
/**
* Returns a string containing the USB vendor ID of the device, if this
* information is known.
*/
INLINE unsigned short InputDevice::
get_vendor_id() const {
LightMutexHolder holder(_lock);
return _vendor_id;
}
/**
* Returns a string containing the USB product ID of the device, if this
* information is known.
*/
INLINE unsigned short InputDevice::
get_product_id() const {
LightMutexHolder holder(_lock);
return _product_id;
}
/**
* Returns true if the device is still connected and able to receive data,
* false otherwise. May return false positives.
*/
INLINE bool InputDevice::
is_connected() const {
LightMutexHolder holder(_lock);
return _is_connected;
}
/**
* Returns an identification of the general type of device. If this could not
* be determined, returns DeviceClass.unknown.
*/
INLINE InputDevice::DeviceClass InputDevice::
get_device_class() const {
LightMutexHolder holder(_lock);
return _device_class;
}
/**
* Returns true if the device supports the indicated feature.
*/
INLINE bool InputDevice::
has_feature(Feature feature) const {
LightMutexHolder holder(_lock);
return (_features & (1 << (unsigned int)feature)) != 0;
}
/**
* Returns true if this is a pointing device.
*/
INLINE bool InputDevice::
has_pointer() const {
return has_feature(Feature::pointer);
}
/**
* Returns true if the device has a physical keyboard designed for text entry.
*/
INLINE bool InputDevice::
has_keyboard() const {
return has_feature(Feature::keyboard);
}
/**
* Returns true if the device features a tracker that can track position and/or
* orientation in 3D space.
*/
INLINE bool InputDevice::
has_tracker() const {
return has_feature(Feature::tracker);
}
/**
* Returns true if the device has vibration motors that can be controlled by
* calling set_vibration().
*/
INLINE bool InputDevice::
has_vibration() const {
return has_feature(Feature::vibration);
}
/**
* Returns true if the device may be able to provide information about its
* battery life.
*/
INLINE bool InputDevice::
has_battery() const {
return has_feature(Feature::battery);
}
/**
* Returns the TrackerData associated with the input device's tracker. This
* only makes sense if has_tracker() also returns true.
*/
INLINE TrackerData InputDevice::
get_tracker() const {
LightMutexHolder holder(_lock);
return _tracker_data;
}
/**
* Returns a rough indication of the battery level, ranging from 0 (completely
* empty battery) to the indicated max_level value.
*/
INLINE InputDevice::BatteryData InputDevice::
get_battery() const {
LightMutexHolder holder(_lock);
return _battery_data;
}
/**
* Returns the number of buttons known to the device. This includes those
* buttons whose state has been seen, as well as buttons that have been
* associated with a ButtonHandle even if their state is unknown. This number
* may change as more buttons are discovered.
*/
INLINE size_t InputDevice::
get_num_buttons() const {
LightMutexHolder holder(_lock);
return _buttons.size();
}
/**
* Associates the indicated ButtonHandle with the button of the indicated index
* number. When the given button index changes state, a corresponding
* ButtonEvent will be generated with the given ButtonHandle. Pass
* ButtonHandle::none() to turn off any association.
*
* It is not necessary to call this if you simply want to query the state of
* the various buttons by index number; this is only necessary in order to
* generate ButtonEvents when the buttons change state.
*/
INLINE void InputDevice::
map_button(size_t index, ButtonHandle button) {
LightMutexHolder holder(_lock);
if (index >= _buttons.size()) {
_buttons.resize(index + 1, ButtonState());
}
_buttons[index].handle = button;
}
/**
* Returns the ButtonHandle that was previously associated with the given index
* number by a call to map_button(), or ButtonHandle::none() if no button
* was associated.
*/
INLINE ButtonHandle InputDevice::
get_button_map(size_t index) const {
if (index < _buttons.size()) {
return _buttons[index].handle;
} else {
return ButtonHandle::none();
}
}
/**
* Returns true if the indicated button (identified by its index number) is
* currently known to be down, or false if it is up or unknown.
*/
INLINE bool InputDevice::
is_button_pressed(size_t index) const {
if (index < _buttons.size()) {
return (_buttons[index]._state == S_down);
} else {
return false;
}
}
/**
* Returns true if the state of the indicated button is known, or false if we
* have never heard anything about this particular button.
*/
INLINE bool InputDevice::
is_button_known(size_t index) const {
if (index < _buttons.size()) {
return _buttons[index]._state != S_unknown;
} else {
return false;
}
}
/**
* Returns the ButtonState that is set at the given index, or throw an assert
* if the index was not found in the list.
*/
INLINE InputDevice::ButtonState InputDevice::
get_button(size_t index) const {
nassertr_always(index < _buttons.size(), ButtonState());
return _buttons[index];
}
/**
* Returns the first ButtonState found with the given axis, or throw an assert
* if the button handle was not found in the list.
*/
INLINE InputDevice::ButtonState InputDevice::
find_button(ButtonHandle handle) const {
for (size_t i = 0; i < _buttons.size(); ++i) {
if (_buttons[i].handle == handle) {
return _buttons[i];
}
}
return ButtonState();
}
/**
* Returns the number of analog axes known to the InputDevice. This number
* may change as more axes are discovered.
*/
INLINE size_t InputDevice::
get_num_axes() const {
return _axes.size();
}
/**
* Associates the indicated Axis with the axis of the indicated index
* number. Pass Axis::none to turn off any association.
*
* It is not necessary to call this if you simply want to query the state of
* the various axes by index number.
*/
INLINE void InputDevice::
map_axis(size_t index, InputDevice::Axis axis) {
LightMutexHolder holder(_lock);
if (index >= _axes.size()) {
_axes.resize(index + 1, AxisState());
}
_axes[index].axis = axis;
}
/**
* Returns the current position of indicated analog axis (identified by its
* index number), or 0.0 if the axis is unknown. The normal range of a
* single axis is -1.0 to 1.0.
*/
INLINE double InputDevice::
get_axis_value(size_t index) const {
if (index < _axes.size()) {
return _axes[index].value;
} else {
return 0.0;
}
}
/**
* Returns the axis state that is set at the given index, or throw an assert
* if the index was not found in the list.
*/
INLINE InputDevice::AxisState InputDevice::
get_axis(size_t index) const {
nassertr_always(index < _axes.size(), AxisState());
return _axes[index];
}
/**
* Returns the first AnalogAxis found with the given axis, or throw an assert
* if the axis was not found in the list.
*/
INLINE InputDevice::AxisState InputDevice::
find_axis(InputDevice::Axis axis) const {
for (size_t i = 0; i < _axes.size(); ++i) {
if (_axes[i].axis == axis) {
return _axes[i];
}
}
return AxisState();
}
/**
* Returns true if the state of the indicated analog axis is known, or false
* if we have never heard anything about this particular axis.
*/
INLINE bool InputDevice::
is_axis_known(size_t index) const {
if (index < _axes.size()) {
return _axes[index].known;
} else {
return false;
}
}
/**
* Sets the strength of the vibration effect, if supported. The values are
* clamped to 0-1 range. The first value axes the low-frequency rumble
* motor, whereas the second axes the high-frequency motor, if present.
*/
INLINE void InputDevice::
set_vibration(double strong, double weak) {
LightMutexHolder holder(_lock);
do_set_vibration(std::max(std::min(strong, 1.0), 0.0), std::max(std::min(weak, 1.0), 0.0));
}
/**
* Enables the generation of mouse-movement events.
*/
INLINE void InputDevice::
enable_pointer_events() {
LightMutexHolder holder(_lock);
_enable_pointer_events = true;
}
/**
* Disables the generation of mouse-movement events.
*/
INLINE void InputDevice::
disable_pointer_events() {
LightMutexHolder holder(_lock);
_enable_pointer_events = false;
_pointer_events.clear();
}
/**
* Called to indicate that the device supports the given feature.
* Assumes the lock is held.
*/
INLINE void InputDevice::
enable_feature(Feature feature) {
_features |= (1 << (unsigned int)feature);
}
/**
* Called to indicate that the device has been disconnected or connected from
* its host.
*/
INLINE void InputDevice::
set_connected(bool connected) {
LightMutexHolder holder(_lock);
_is_connected = connected;
}
/**
*
*/
INLINE InputDevice::ButtonState::
ButtonState(ButtonHandle handle) :
handle(handle) {
}
/**
* True if the button state is currently known.
*/
ALWAYS_INLINE bool InputDevice::ButtonState::
is_known() const {
return (_state != S_unknown);
}
/**
* True if the button is currently known to be pressed.
*/
ALWAYS_INLINE bool InputDevice::ButtonState::
is_pressed() const {
return (_state == S_down);
}