-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystick.js
15 lines (15 loc) · 6.29 KB
/
joystick.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* Joystick version 0.0.1 */
(function(){var i,h,j,k,e;i=0;j=["ms","moz","webkit","o"];k=0;for(e=j.length;k<e;k++)h=j[k],window.requestAnimationFrame||(window.requestAnimationFrame=window[h+"RequestAnimationFrame"],window.cancelAnimationFrame=window[h+"CancelAnimationFrame"]||window[h+"CancelRequestAnimationFrame"]);window.requestAnimationFrame||(window.requestAnimationFrame=function(a){var b,c,e;b=+new Date;e=Math.max(0,16-b-i);c=window.setTimeout(function(){return a(b+e)},e);i=b+e;return c});window.cancelAnimationFrame||(window.cancelAnimationFrame=
function(a){return clearTimeout(a)})}).call(this);
(function(){var i,h,j,k;h="face_bottom face_right face_left face_top bumper_left bumper_right trigger_left trigger_right select start thumbstick_left_click thumbstick_right_click dpad_up dpad_down dpad_left dpad_right home".split(" ");i=["left_hori","left_vert","right_hori","right_vert"];j={trigger_threshold:0.5,axis_threshold:0.5,analog_rounding:1};var e=function(a){var b,c;this.settings={};for(b in j)c=j[b],this.settings[b]=(null!=a?a[b]:void 0)||c;this.connected_joysticks={};this.previous_states=
{}};e.prototype.add_listeners=function(){var a,b,c,e,d,f,g=this;this.on_joystick_connect=function(a){g.add_joystick(a.gamepad);if(!g.is_polling)return g.polling_start()};this.on_joystick_disconnect=function(a){return g.remove_joystick(a.gamepad)};this.on_button_down=function(a){return g.button_down(a.gamepad,a.button)};this.on_button_up=function(a){return g.button_up(a.gamepad,a.button)};this.on_axis_move=function(a){return g.axis_move(a.gamepad,a.axis,a.value)};d=this.event_types=[{event_names:["MozGamepadConnected"],
listener:this.on_joystick_connect},{event_names:["MozGamepadDisconnected"],listener:this.on_joystick_disconnect},{event_names:["MozGamepadButtonDown"],listener:this.on_button_down},{event_names:["MozGamepadButtonUp"],listener:this.on_button_up},{event_names:["MozGamepadAxisMove"],listener:this.on_axis_move}];f=[];c=0;for(e=d.length;c<e;c++)b=d[c],f.push(function(){var c,d,g,f;g=b.event_names;f=[];c=0;for(d=g.length;c<d;c++)a=g[c],f.push(window.addEventListener(a,b.listener));return f}());return f};
e.prototype.remove_listeners=function(){var a,b,c,e,d;e=this.event_types;d=[];b=0;for(c=e.length;b<c;b++)a=e[b],d.push(function(){var b,c,d;c=a.event_names;d=[];b=0;for(c=c.length;b<c;b++)d.push(window.removeEventListener(event_Name,a.listener));return d}());return d};e.prototype.polling_start=function(){this.is_polling=!0;return this.poll()};e.prototype.polling_stop=function(){return this.is_polling=!1};e.prototype.poll=function(){var a=this;if(this.is_polling)return this.poll_for_new_joysticks(),
this.update_joysticks(),window.requestAnimationFrame(function(){return a.poll()})};e.prototype.add_joystick=function(a){return this.connected_joysticks[a.index]=a};e.prototype.remove_joystick=function(a){return delete this.connected_joysticks[a.index]};e.prototype.poll_for_new_joysticks=function(){var a,b=this;a=function(a){var e,d,f,g;g=[];d=0;for(f=a.length;d<f;d++)(e=a[d])&&null==b.connected_joysticks[e.index]?g.push(b.add_joystick(e)):g.push(void 0);return g};if(null!=navigator.webkitGamepads)return a(navigator.webkitGamepads);
if(null!=navigator.webkitGetGamepads)return a(navigator.webkitGetGamepads())};e.prototype.clone_joystick_object=function(a,b){var c,e,d,f,g,h;null==b&&(b=!1);d=[];c=[];h=a.buttons;f=0;for(g=h.length;f<g;f++)e=h[f],d.push(b?0:e);h=a.axes;f=0;for(g=h.length;f<g;f++)e=h[f],c.push(b?0:e);return{id:a.id,index:a.index,timestamp:a.timestamp,buttons:d,axes:c}};e.prototype.round_analog=function(a){var b;if(0>this.settings.analog_rounding)return a;b=Math.pow(10,this.settings.analog_rounding);return Math.round(a*
b)/b};e.prototype.update_joysticks=function(){var a,b,c,e,d,f,g,i,j,k,m,l;i=this.connected_joysticks;l=[];for(e in i)if(d=i[e],!(null!=d.timestamp&&d.timestamp===(null!=(j=this.previous_states[d.index])?j.timestamp:void 0))){d=this.clone_joystick_object(d);this.previous_states[d.index]||(this.previous_states[d.index]=this.clone_joystick_object(d,!0));c=f=0;for(g=d.buttons.length;0<=g?f<g:f>g;c=0<=g?++f:--f)a=d.buttons[c],b=this.previous_states[d.index].buttons[c],"trigger_right"===(k=h[c])||"trigger_left"===
k?a>this.settings.trigger_threshold&&b<=this.settings.trigger_threshold?(this.button_down(d,c),this.trigger_engaged(d,c)):a<=this.settings.trigger_threshold&&b>this.settings.trigger_threshold&&(this.button_up(d,c),this.trigger_released(d,c)):a!==b&&this.round_analog(a)!==this.round_analog(this.previous_states[d.index].buttons[c])&&(b=this.round_analog(a)-this.round_analog(this.previous_states[d.index].buttons[c]),1===b?this.button_down(d,c):-1===b?this.button_up(d,c):("trigger_right"===(m=h[c])||
"trigger_left"===m)&&this.trigger_move(d,c,a));c=g=0;for(b=d.axes.length;0<=b?g<b:g>b;c=0<=b?++g:--g)if(a=d.axes[c],f=this.previous_states[d.index].axes[c],this.round_analog(a)!==this.round_analog(this.previous_states[d.index].axes[c])&&this.axis_move(d,c,a),a>this.settings.axis_threshold&&f<=this.settings.axis_threshold||a<-this.settings.axis_threshold&&f>=-this.settings.axis_threshold)this.axis_engaged(d,c,0<f?1:-1);else if(a<=this.settings.axis_threshold&&f>this.settings.axis_threshold||a>=-this.settings.axis_threshold&&
f<-this.settings.axis_threshold)this.axis_released(d,c,0<f?1:-1);l.push(this.previous_states[d.index]=d)}return l};e.prototype.button_down=function(a,b){return console.log("Button down",a.index,h[b])};e.prototype.button_up=function(a,b){return console.log("Button up",a.index,h[b])};e.prototype.trigger_engaged=function(a,b){return console.log("Trigger engaged",a.index,h[b])};e.prototype.trigger_released=function(a,b){return console.log("Trigger released",a.index,h[b])};e.prototype.trigger_move=function(a,
b,c){return console.log("Trigger move",a.index,h[b],this.round_analog(c))};e.prototype.axis_engaged=function(a,b,c){return console.log("Axis engaged",a.index,i[b],c)};e.prototype.axis_released=function(a,b,c){return console.log("Axis released",a.index,i[b],c)};e.prototype.axis_move=function(a,b,c){return console.log("Axis move",a.index,i[b],this.round_analog(c))};k=function(){return!0};window.joystick={init:function(a){if(!window.joystick.supported())return console.log("Joystick not supported in your browser :(");
a=new e(a);k()?a.polling_start():a.add_listeners();return a},supported:function(){return null!=navigator.webkitGamepads||null!=navigator.webkitGetGamepads}}}).call(this);