@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
36 lines (26 loc) • 845 B
JavaScript
/**
*
* @param {TouchEvent|MouseEvent} event
* @returns {number} index of touch or mouse button that corresponded to this event, multiplexed with the device ID in MSB area
*/
export function eventToSourceIdentifier(event) {
let device_id = 0;
let source_id = 0;
if (event instanceof MouseEvent) {
// mouse
device_id = 1;
source_id = event.button;
} else if (event instanceof TouchEvent) {
// touch
device_id = 2;
const touches = event.changedTouches;
const num_changes = touches.length;
if (num_changes > 0) {
const touch = touches.item(0);
source_id = touch.identifier;
}
}
return ((device_id & 0b11) << 29)
| (source_id & 0b11111111111111111111111111111)
;
}