define(["./../common/inputmanager.js"],function(_inputmanager){var isLooping,ButtonInfos={};function setPressed(buttonInfo,newPressedState){var fire,key=buttonInfo.key,keyCode=buttonInfo.keyCode;!0===newPressedState?(fire=!1===buttonInfo.pressed||!1!==buttonInfo.enableRepeatKeyDown?!0:fire)&&keyCode&&raiseEvent("keydown",key,keyCode,!0===buttonInfo.pressed):!1===newPressedState&&!0===buttonInfo.pressed&&(keyCode&&raiseEvent("keyup",key,keyCode),buttonInfo.clickOnKeyUp)&&allowInput()&&_inputmanager.default.trigger("select"),buttonInfo.pressed=newPressedState}function allowInput(){var doc=document;return"hidden"!==doc.visibilityState&&!!doc.hasFocus()}function raiseEvent(name,key,keyCode,repeat){var event;allowInput()&&((event=document.createEvent("Event")).initEvent(name,!0,!0),event.key=key,event.keyCode=keyCode,null!=repeat&&(event.repeat=repeat),(document.activeElement||document.body).dispatchEvent(event))}function runInputLoop(){for(var hasGamepad=!1,gamepads=function(){try{return navigator.getGamepads()||[]}catch(err){return console.log("Error getting gamepads: "+err),[]}}(),i=0,length=gamepads.length;i<length;i++){var gamepad=gamepads[i];if(gamepad)for(var hasGamepad=!0,axes=gamepad.axes,leftStickX=axes[0],axes=axes[1],buttons=(.75<leftStickX?setPressed(ButtonInfos.GamepadLeftThumbStickRight,!0):leftStickX<-.75?setPressed(ButtonInfos.GamepadLeftThumbStickLeft,!0):axes<-.75?setPressed(ButtonInfos.GamepadLeftThumbStickUp,!0):.75<axes?setPressed(ButtonInfos.GamepadLeftThumbStickDown,!0):(setPressed(ButtonInfos.GamepadLeftThumbStickLeft,!1),setPressed(ButtonInfos.GamepadLeftThumbStickRight,!1),setPressed(ButtonInfos.GamepadLeftThumbStickUp,!1),setPressed(ButtonInfos.GamepadLeftThumbStickDown,!1)),gamepad.buttons),j=0,len=buttons.length;j<len;j++)if(buttons[j].pressed)switch(j){case 12:case 13:case 14:case 15:case 0:case 1:setPressed(ButtonInfos[j],!0)}else switch(j){case 12:case 13:case 14:case 15:case 0:case 1:setPressed(ButtonInfos[j],!1)}}hasGamepad?requestAnimationFrame(runInputLoop):(console.log("exiting gamepad input loop"),isLooping=!1)}ButtonInfos[0]={key:"GamepadA",keyCode:0,enableRepeatKeyDown:!1,clickOnKeyUp:!0},ButtonInfos[1]={key:"GamepadB",keyCode:27},ButtonInfos[12]={key:"GamepadDPadUp",keyCode:38},ButtonInfos[13]={key:"GamepadDPadDown",keyCode:40},ButtonInfos[14]={key:"GamepadDPadLeft",keyCode:37},ButtonInfos[15]={key:"GamepadDPadRight",keyCode:39},ButtonInfos.GamepadLeftThumbStickUp={key:"GamepadLeftThumbStickUp",keyCode:38},ButtonInfos.GamepadLeftThumbStickDown={key:"GamepadLeftThumbStickDown",keyCode:40},ButtonInfos.GamepadLeftThumbStickLeft={key:"GamepadLeftThumbStickLeft",keyCode:37},ButtonInfos.GamepadLeftThumbStickRight={key:"GamepadLeftThumbStickRight",keyCode:39},navigator.getGamepads||(navigator.webkitGetGamepads?navigator.getGamepads=navigator.webkitGetGamepads:navigator.getGamepads=function(){return null}),window.addEventListener("gamepadconnected",function(e){e=e.gamepad.index;console.log("gamepadconnected: "+e),isLooping||(isLooping=!0,runInputLoop())}),window.addEventListener("gamepaddisconnected",function(e){e=e.gamepad.index;console.log("gamepaddisconnected: "+e)}),window.navigator&&"string"==typeof window.navigator.gamepadInputEmulation&&(window.navigator.gamepadInputEmulation="gamepad")});