UNPKG

jsgamepad

Version:

W3C Gamepad API Simplified & Implementation fixes

1 lines 11.2 kB
!function(a){function b(a){Object.defineProperty(this,a,{enumerable:!0,get:function(){return this[o][a]}})}function c(a){var b;if(a&&a.__esModule){b={};for(var c in a)Object.hasOwnProperty.call(a,c)&&(b[c]=a[c]);b.default=a}else{if("[object Module]"===Object.prototype.toString.call(a)||"undefined"!=typeof System&&System.isModule&&System.isModule(a))return a;b={default:a,__useDefault:!0}}return new d(b)}function d(a){Object.defineProperty(this,o,{value:a}),Object.keys(a).forEach(b,this)}function e(a){return"@node/"===a.substr(0,6)?m(a,c(p(a.substr(6))),{}):n[a]}function f(a){var b=e(a);if(!b)throw new Error('Module "'+a+'" expected, but not contained in build.');if(b.module)return b.module;var c=b.linkRecord;return g(b,c),l(b,c,[]),b.module}function g(a,b){if(!b.depLoads){b.declare&&h(a,b),b.depLoads=[];for(var c=0;c<b.deps.length;c++){var d=e(b.deps[c]);b.depLoads.push(d),d.linkRecord&&g(d,d.linkRecord);var f=b.setters&&b.setters[c];f&&(f(d.module||d.linkRecord.moduleObj),d.importerSetters.push(f))}return a}}function h(b,c){var d=c.moduleObj,e=b.importerSetters,f=!1,g=c.declare.call(a,function(a,b){if(!f){if("object"==typeof a)for(var c in a)"__useDefault"!==c&&(d[c]=a[c]);else d[a]=b;f=!0;for(var g=0;g<e.length;g++)e[g](d);return f=!1,b}},{id:b.key});"function"!=typeof g?(c.setters=g.setters,c.execute=g.execute):(c.setters=[],c.execute=g)}function i(a,b,c){return n[a]={key:a,module:void 0,importerSetters:[],linkRecord:{deps:b,depLoads:void 0,declare:c,setters:void 0,execute:void 0,moduleObj:{}}}}function j(a,b,c,d){return n[a]={key:a,module:void 0,importerSetters:[],linkRecord:{deps:b,depLoads:void 0,declare:void 0,execute:d,executingRequire:c,moduleObj:{default:{},__useDefault:!0},setters:void 0}}}function k(a,b,c){return function(d){for(var e=0;e<a.length;e++)if(a[e]===d){var f,g=b[e];return f=-1===c.indexOf(g)?l(g,g.linkRecord,c):g.linkRecord.moduleObj,f.__useDefault?f.default:f}}}function l(b,c,e){if(e.push(b),b.module)return b.module;var f;if(c.setters){for(var g=0;g<c.deps.length;g++){var h=c.depLoads[g],i=h.linkRecord;i&&-1===e.indexOf(h)&&(f=l(h,i,i.setters?e:[]))}c.execute.call(q)}else{var j={id:b.key},m=c.moduleObj;Object.defineProperty(j,"exports",{configurable:!0,set:function(a){m.default=a},get:function(){return m.default}});var n=k(c.deps,c.depLoads,e);if(!c.executingRequire)for(var g=0;g<c.deps.length;g++)n(c.deps[g]);var o=c.execute.call(a,n,m.default,j);if(void 0!==o?m.default=o:j.exports!==m.default&&(m.default=j.exports),m.default&&m.default.__esModule)for(var p in m.default)Object.hasOwnProperty.call(m.default,p)&&"default"!==p&&(m[p]=m.default[p])}var j=b.module=new d(c.moduleObj);if(!c.setters)for(var g=0;g<b.importerSetters.length;g++)b.importerSetters[g](j);return j}function m(a,b){return n[a]={key:a,module:b,importerSetters:[],linkRecord:void 0}}var n={},o="undefined"!=typeof Symbol?Symbol():"@@baseObject";d.prototype=Object.create(null),"undefined"!=typeof Symbol&&Symbol.toStringTag&&(d.prototype[Symbol.toStringTag]="Module");var p="undefined"!=typeof System&&System._nodeRequire||"undefined"!=typeof require&&"undefined"!=typeof require.resolve&&"undefined"!=typeof process&&process.platform&&require,q={};return Object.freeze&&Object.freeze(q),function(a,b,e,g){return function(h){h(function(h){var k={_nodeRequire:p,register:i,registerDynamic:j,registry:{get:function(a){return n[a].module},set:m},newModule:function(a){return new d(a)}};m("@empty",new d({}));for(var l=0;l<b.length;l++)m(b[l],c(arguments[l],{}));g(k);var o=f(a[0]);if(a.length>1)for(var l=1;l<a.length;l++)f(a[l]);return e?o.default:(o instanceof d&&Object.defineProperty(o,"__esModule",{value:!0}),o)})}}}("undefined"!=typeof self?self:global)(["a"],["c"],!1,function(a){this.require,this.exports,this.module;a.registerDynamic("b",[],!0,function(a,b,c){function d(){this._events=this._events||{},this._maxListeners=this._maxListeners||void 0}function e(a){return"function"==typeof a}function f(a){return"number"==typeof a}function g(a){return"object"==typeof a&&null!==a}function h(a){return void 0===a}this||self;c.exports=d,d.EventEmitter=d,d.prototype._events=void 0,d.prototype._maxListeners=void 0,d.defaultMaxListeners=10,d.prototype.setMaxListeners=function(a){if(!f(a)||a<0||isNaN(a))throw TypeError("n must be a positive number");return this._maxListeners=a,this},d.prototype.emit=function(a){var b,c,d,f,i,j;if(this._events||(this._events={}),"error"===a&&(!this._events.error||g(this._events.error)&&!this._events.error.length)){if(b=arguments[1],b instanceof Error)throw b;throw TypeError('Uncaught, unspecified "error" event.')}if(c=this._events[a],h(c))return!1;if(e(c))switch(arguments.length){case 1:c.call(this);break;case 2:c.call(this,arguments[1]);break;case 3:c.call(this,arguments[1],arguments[2]);break;default:f=Array.prototype.slice.call(arguments,1),c.apply(this,f)}else if(g(c))for(f=Array.prototype.slice.call(arguments,1),j=c.slice(),d=j.length,i=0;i<d;i++)j[i].apply(this,f);return!0},d.prototype.addListener=function(a,b){var c;if(!e(b))throw TypeError("listener must be a function");return this._events||(this._events={}),this._events.newListener&&this.emit("newListener",a,e(b.listener)?b.listener:b),this._events[a]?g(this._events[a])?this._events[a].push(b):this._events[a]=[this._events[a],b]:this._events[a]=b,g(this._events[a])&&!this._events[a].warned&&(c=h(this._maxListeners)?d.defaultMaxListeners:this._maxListeners,c&&c>0&&this._events[a].length>c&&(this._events[a].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[a].length),"function"==typeof console.trace&&console.trace())),this},d.prototype.on=d.prototype.addListener,d.prototype.once=function(a,b){function c(){this.removeListener(a,c),d||(d=!0,b.apply(this,arguments))}if(!e(b))throw TypeError("listener must be a function");var d=!1;return c.listener=b,this.on(a,c),this},d.prototype.removeListener=function(a,b){var c,d,f,h;if(!e(b))throw TypeError("listener must be a function");if(!this._events||!this._events[a])return this;if(c=this._events[a],f=c.length,d=-1,c===b||e(c.listener)&&c.listener===b)delete this._events[a],this._events.removeListener&&this.emit("removeListener",a,b);else if(g(c)){for(h=f;h-- >0;)if(c[h]===b||c[h].listener&&c[h].listener===b){d=h;break}if(d<0)return this;1===c.length?(c.length=0,delete this._events[a]):c.splice(d,1),this._events.removeListener&&this.emit("removeListener",a,b)}return this},d.prototype.removeAllListeners=function(a){var b,c;if(!this._events)return this;if(!this._events.removeListener)return 0===arguments.length?this._events={}:this._events[a]&&delete this._events[a],this;if(0===arguments.length){for(b in this._events)"removeListener"!==b&&this.removeAllListeners(b);return this.removeAllListeners("removeListener"),this._events={},this}if(c=this._events[a],e(c))this.removeListener(a,c);else if(c)for(;c.length;)this.removeListener(a,c[c.length-1]);return delete this._events[a],this},d.prototype.listeners=function(a){var b;return b=this._events&&this._events[a]?e(this._events[a])?[this._events[a]]:this._events[a].slice():[]},d.prototype.listenerCount=function(a){if(this._events){var b=this._events[a];if(e(b))return 1;if(b)return b.length}return 0},d.listenerCount=function(a,b){return a.listenerCount(b)}}),a.register("a",["b","c"],function(a,b){"use strict";function c(){return window.requestAnimationFrame?window.requestAnimationFrame:window.webkitRequestAnimationFrame?window.webkitRequestAnimationFrame:window.mozRequestAnimationFrame?window.mozRequestAnimationFrame:window.msRequestAnimationFrame?window.msRequestAnimationFrame:window.oRequestAnimationFrame?window.oRequestAnimationFrame:function(a){setTimeout(a,0)}}var d,e,f,g,h,i,j,k,l;return{setters:[function(a){d=a.default},function(a){e=a.default}],execute:function(){f=function(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")},g=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),h=function(a,b){if(!a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!b||"object"!=typeof b&&"function"!=typeof b?a:b},i=function(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function, not "+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)},j=c(),k=function(a){function b(){f(this,b);var a=h(this,(b.__proto__||Object.getPrototypeOf(b)).call(this));return a.gamepads=a.emptyGamepadList,a.activeButtons={},a.activeAxes={},e(window).on("gamepadconnected webkitgamepadconnected mozgamepadconnected",function(){a.scanGamepads()}).on("gamepaddisconnected webkitgamepaddisconnected mozgamepaddisconnected",function(){a.scanGamepads()}),a}return i(b,a),g(b,[{key:"watch",value:function(){this.isWatching||(this.isWatching=!0,this.watchLoop())}},{key:"watchLoop",value:function(){var a=this;this.scanGamepads(),this.isWatching&&j(function(){return a.watchLoop()})}},{key:"unwatch",value:function(){this.isWatching&&(this.isWatching=!1)}},{key:"scanGamepads",value:function(){var a=this,b=this.getGamepads();Object.keys(b).forEach(function(c){var d=b[c];if(d)a.gamepads[c]?(a.scanButtons(d),a.scanAxes(d)):(a.gamepads=a.snapshotGamepadList(b),a.emit("connected",d));else if(a.gamepads[c]){var e=a.gamepads[c];a.gamepads=a.snapshotGamepadList(b),a.emit("disconnected",e)}})}},{key:"scanButtons",value:function(a){var b=this,c=this.activeButtons[a.index]=this.activeButtons[a.index]||{};a.buttons.forEach(function(d,e){d=b.snapshotButton(b.mapButton(d));var f=c[e],g={button:d,buttonIndex:e,gamepad:a};!d.pressed||f&&f.pressed||b.emit("buttonPressed",g),f&&d.value!==f.value&&b.emit("buttonChanged",g),!d.pressed&&f&&f.pressed&&b.emit("buttonReleased",g),c[e]=d})}},{key:"scanAxes",value:function(a){var b=this,c=this.activeAxes[a.index]=this.activeAxes[a.index]||{};a.axes.forEach(function(d,e){var f=c[e],g={axis:d,axisIndex:e,gamepad:a};f&&d!==f&&b.emit("axisChanged",g),c[e]=d})}},{key:"getGamepads",value:function(){return navigator.getGamepads?navigator.getGamepads():navigator.webkitGetGamepads?navigator.webkitGetGamepads():this.emptyGamepadList}},{key:"snapshotGamepadList",value:function(a){var b={};return Object.keys(a).forEach(function(c){b[c]=a[c]}),b.length=a.length,b}},{key:"snapshotButton",value:function(a){return{pressed:!!a.pressed,touched:!!a.touched,value:a.value}}},{key:"mapButton",value:function(a){return"object"==typeof a?a:{pressed:1===a,touched:0!==a,value:a}}},{key:"emptyGamepadList",get:function(){return{0:null,1:null,2:null,3:null,length:4}}}]),b}(d),a("gamepad",l=new k),a("gamepad",l),a("default",l)}}})})(function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof module&&module.exports&&"function"==typeof require?module.exports=a(require("jquery")):jsGamepad=a($)});