-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamepad.min.js
More file actions
1 lines (1 loc) · 9.02 KB
/
gamepad.min.js
File metadata and controls
1 lines (1 loc) · 9.02 KB
1
!function(t){"use strict";var e=function(){},n=function(t){for(var e=navigator.getGamepads(),n=0;n<e.length;n++){var i=e[n];if(i&&i.index===t.index)return i}},i={getType:function(){return"null"},isSupported:function(){return!1},update:e},r=function(t){var n=this,i=window;this.running=!1,this.intervalId=null,this.update=e,this.requestAnimationFrame=t||i.requestAnimationFrame||i.webkitRequestAnimationFrame||i.mozRequestAnimationFrame,this.tickFunction=function(){n.running&&n.update()},this.startTicker=function(){this.intervalId=setInterval(this.tickFunction,16)}};r.prototype.start=function(t){this.running=!0,this.update=t||e,this.startTicker()},r.prototype.pause=function(){this.running=!1,clearInterval(this.intervalId),this.intervalId=null},r.prototype.resume=function(){this.running=!0,this.intervalId||this.startTicker()};var o=function(){};o.prototype.update=e,o.prototype.start=function(t){this.update=t||e};var a=function(t){this.listener=t,setTimeout(function(){window.addEventListener("gamepadconnected",function(e){t._connect(e.gamepad)}),window.addEventListener("gamepaddisconnected",function(e){t._disconnect(e.gamepad)})},500)};a.factory=function(t){var e=i,n=window&&window.navigator;return n&&(void 0!==n.getGamepads?e=new a(t,function(){return n.getGamepads()}):void 0!==n.webkitGamepads?e=new a(t,function(){return n.webkitGamepads()}):void 0!==n.webkitGetGamepads&&(e=new a(t,function(){return n.webkitGetGamepads()}))),e},a.getType=function(){return"WebKit"},a.prototype.getType=function(){return a.getType()},a.isSupported=a.prototype.isSupported=function(){return!0};a.prototype.update=e;var s=function(t){this.isSupported()&&(this.listener=t,window.addEventListener("gamepadconnected",function(e){t._connect(e.gamepad)}),window.addEventListener("gamepaddisconnected",function(e){t._disconnect(e.gamepad)}))};s.factory=function(t){var e=i;return window&&void 0!==window.addEventListener&&(e=new s(t)),e},s.getType=function(){return"Firefox"},s.prototype.getType=function(){return s.getType()},s.isSupported=s.prototype.isSupported=function(){return-1!==(window&&window.navigator).userAgent.indexOf("Firefox")},s.prototype.update=e;var p=function(t){this.updateStrategy=t||new r,this.gamepads=[],this.listeners={},this.platform=i,this.deadzone=.03,this.maximizeThreshold=.97};p.UpdateStrategies={AnimFrameUpdateStrategy:r,ManualUpdateStrategy:o},p.PlatformFactories=[s.factory,a.factory],p.Type={N64:"n64",PLAYSTATION:"playstation",LOGITECH:"logitech",XBOX:"xbox",TWIN:"twin",UNKNOWN:"unknown"},p.Event={CONNECTED:"connected",UNSUPPORTED:"unsupported",DISCONNECTED:"disconnected",TICK:"tick",BUTTON_DOWN:"button-down",BUTTON_UP:"button-up",AXIS_CHANGED:"axis-changed"},p.StandardButtons=["FACE_1","FACE_2","FACE_3","FACE_4","LEFT_TOP_SHOULDER","RIGHT_TOP_SHOULDER","LEFT_BOTTOM_SHOULDER","RIGHT_BOTTOM_SHOULDER","SELECT_BACK","START_FORWARD","LEFT_STICK","RIGHT_STICK","DPAD_UP","DPAD_DOWN","DPAD_LEFT","DPAD_RIGHT","HOME"],p.StandardAxes=["LEFT_STICK_X","LEFT_STICK_Y","RIGHT_STICK_X","RIGHT_STICK_Y","DPAD_X","DPAD_Y","LEFT_BOTTOM_SHOULDER","RIGHT_BOTTOM_SHOULDER"];var u,d=function(t,e,n){return e<t.length?t[e]:n+(e-t.length+1)};p.StandardMapping={env:{},buttons:{byButton:[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},axes:{byAxis:[0,1,2,3]}},p.Mappings=[{env:{platform:s.getType(),type:p.Type.N64},buttons:{byButton:[2,1,3,0,4,5,-1,-1,8,9,-1,-1,12,13,14,15,-1]},axes:{byAxis:[1,2,-1,-1]}},{env:{platform:a.getType(),type:p.Type.N64},buttons:{byButton:[2,1,3,0,4,5,-1,-1,8,9,-1,-1,12,13,14,15,-1]},axes:{byAxis:[0,1,-1,-1]}},{env:{platform:s.getType(),type:p.Type.XBOX},buttons:{byButton:[3,1,0,2,4,5,-1,-1,6,7,9,10,-1,-1,-1,-1,8]},axes:{byAxis:[0,1,3,4,6,7]}},{env:{platform:a.getType(),type:p.Type.XBOX},buttons:{byButton:[3,1,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16]},axes:{byAxis:[0,1,2,3]}},{env:{platform:s.getType(),type:p.Type.PLAYSTATION},buttons:{byButton:[14,13,15,12,10,11,8,9,0,3,1,2,4,6,7,5,16]},axes:{byAxis:[0,1,2,3]}},{env:{platform:a.getType(),type:p.Type.LOGITECH},buttons:{byButton:[1,2,0,3,4,5,6,7,8,9,10,11,11,12,13,14,10]},axes:{byAxis:[0,1,2,3]}},{env:{platform:s.getType(),type:p.Type.LOGITECH},buttons:{byButton:[0,1,2,3,4,5,-1,-1,6,7,8,9,11,12,13,14,10],byAxis:[-1,-1,-1,-1,-1,-1,[2,0,1],[2,0,-1]]},axes:{byAxis:[0,1,3,4]}},{env:{platform:a.getType(),type:p.Type.TWIN},buttons:{byButton:[0,1,2,3,6,7,4,5,8,9,-1,-1,-1,-1,-1,-1,-1]},axes:{byAxis:[0,1,3,2,4,5]}},{env:{platform:s.getType(),type:p.Type.TWIN},buttons:{byButton:[0,1,2,3,6,7,4,5,8,9,-1,-1,-1,-1,-1,-1,-1]},axes:{byAxis:[0,1,3,2,4,5]}}],p.prototype.init=function(){var t=p.resolvePlatform(this),e=this;return this.platform=t,this.updateStrategy.start(function(){e._update()}),t.isSupported()},p.prototype.pause=function(){this.updateStrategy.pause()},p.prototype.resume=function(){this.updateStrategy.resume()},p.prototype.bind=function(t,e){return void 0===this.listeners[t]&&(this.listeners[t]=[]),this.listeners[t].push(e),this},p.prototype.unbind=function(t,e){if(void 0!==t){if(void 0!==e){if(void 0===this.listeners[t])return!1;for(var n=0;n<this.listeners[t].length;n++)if(this.listeners[t][n]===e)return this.listeners[t].splice(n,1),!0;return!1}this.listeners[t]=[]}else this.listeners={}},p.prototype.count=function(){return this.gamepads.length},p.prototype._fire=function(t,e){if(void 0!==this.listeners[t])for(var n=0;n<this.listeners[t].length;n++)this.listeners[t][n].apply(this.listeners[t][n],[e])},p.getNullPlatform=function(){return Object.create(i)},p.resolvePlatform=function(t){var e,n=i;for(e=0;!n.isSupported()&&e<p.PlatformFactories.length;e++)n=p.PlatformFactories[e](t);return n},p.prototype._connect=function(t){var e,n,i=this._resolveMapping(t);for(t.state={},t.lastState={},t.updater=[],e=i.buttons.byButton.length,n=0;n<e;n++)this._addButtonUpdater(t,i,n);for(e=i.axes.byAxis.length,n=0;n<e;n++)this._addAxisUpdater(t,i,n);this.gamepads[t.index]=t,this._fire(p.Event.CONNECTED,t)},p.prototype._addButtonUpdater=function(t,e,n){var i,r=d(p.StandardButtons,n,"EXTRA_BUTTON_"),o=this._createButtonGetter(t,e.buttons,n),a=this,s={gamepad:t,control:r};t.state[r]=0,t.lastState[r]=0,i=function(){var e=o(),n=t.lastState[r],i=e>.5,u=n>.5;t.state[r]=e,i&&!u?a._fire(p.Event.BUTTON_DOWN,Object.create(s)):!i&&u&&a._fire(p.Event.BUTTON_UP,Object.create(s)),0!==e&&1!==e&&e!==n&&a._fireAxisChangedEvent(t,r,e),t.lastState[r]=e},t.updater.push(i)},p.prototype._addAxisUpdater=function(t,e,n){var i,r=d(p.StandardAxes,n,"EXTRA_AXIS_"),o=this._createAxisGetter(t,e.axes,n),a=this;t.state[r]=0,t.lastState[r]=0,i=function(){var e=o(),n=t.lastState[r];t.state[r]=e,e!==n&&a._fireAxisChangedEvent(t,r,e),t.lastState[r]=e},t.updater.push(i)},p.prototype._fireAxisChangedEvent=function(t,e,n){var i={gamepad:t,axis:e,value:n};this._fire(p.Event.AXIS_CHANGED,i)},p.prototype._createButtonGetter=(u=function(){return 0},function(t,e,i){var r,o,a=u,s=this;return-1!==(r=e.byButton[i])?"number"==typeof r&&r<t.buttons.length&&(a=function(){var e=(t=n(t)).buttons[r];return"number"==typeof e?e:"number"==typeof e.value?e.value:0}):e.byAxis&&i<e.byAxis.length&&(r=e.byAxis[i],o=r,"[object Array]"===Object.prototype.toString.call(o)&&3==r.length&&r[0]<t.axes.length&&(a=function(t,e,n){var i=u;return e<n?i=function(){var i=n-e,r=t();return(r=(r-e)/i)<0?0:r}:n<e&&(i=function(){var i=e-n,r=t();return(r=(r-n)/i)>1?0:1-r}),i}(a=function(){var e=(t=n(t)).axes[r[0]];return s._applyDeadzoneMaximize(e)},r[1],r[2]))),a}),p.prototype._createAxisGetter=function(){var t=function(){return 0};return function(e,i,r){var o,a=t,s=this;return-1!==(o=i.byAxis[r])&&"number"==typeof o&&o<e.axes.length&&(a=function(){var t=(e=n(e)).axes[o];return s._applyDeadzoneMaximize(t)}),a}}(),p.prototype._disconnect=function(t){var e,n=[];for(void 0!==this.gamepads[t.index]&&delete this.gamepads[t.index],e=0;e<this.gamepads.length;e++)void 0!==this.gamepads[e]&&(n[e]=this.gamepads[e]);this.gamepads=n,this._fire(p.Event.DISCONNECTED,t)},p.prototype._resolveControllerType=function(t){return-1!==(t=t.toLowerCase().replace(/\s+/g," ").replace(/^\s+|\s+$/g,"")).indexOf("playstation")?p.Type.PLAYSTATION:-1!==t.indexOf("logitech")||-1!==t.indexOf("wireless gamepad")?p.Type.LOGITECH:-1!==t.indexOf("xbox")||-1!==t.indexOf("360")||-1!==t.indexOf("microsoft")?p.Type.XBOX:-1!==t.indexOf("79-6-generic")&&-1!==t.indexOf("joystick")||-1!==t.indexOf("vendor: 0079 product: 0006")&&-1!==t.indexOf("generic usb joystick")?p.Type.N64:-1!==t.indexOf("twin")?p.Type.TWIN:p.Type.UNKNOWN},p.prototype._resolveMapping=function(t){var e,n,i=p.Mappings,r=null,o={platform:this.platform.getType(),type:this._resolveControllerType(t.id)};for(e=0;!r&&e<i.length;e++)n=i[e],p.envMatchesFilter(n.env,o)&&(r=n);return r||p.StandardMapping},p.envMatchesFilter=function(t,e){var n,i=!0;for(n in t)t[n]!==e[n]&&(i=!1);return i},p.prototype._update=function(){this.platform.update(),this.gamepads.forEach(function(t){t&&t.updater.forEach(function(t){t()})}),this.gamepads.length>0&&this._fire(p.Event.TICK,this.gamepads)},p.prototype._applyDeadzoneMaximize=function(t,e,n){return e=void 0!==e?e:this.deadzone,n=void 0!==n?n:this.maximizeThreshold,t>=0?t<e?t=0:t>n&&(t=1):t>-e?t=0:t<-n&&(t=-1),t},t.Gamepad=p}("undefined"!=typeof module&&module.exports||window);