diff --git a/Libraries/gamepad.js b/Libraries/gamepad.js index c7f7876e6..237a15493 100644 --- a/Libraries/gamepad.js +++ b/Libraries/gamepad.js @@ -1,36 +1,357 @@ -/* - gamepad.js, See README for copyright and usage instructions. +/** @license gamepad.js, See README for copyright and usage instructions. */ -(function(){var m=function(){return navigator.webkitGamepads||navigator.mozGamepads||navigator.gamepads},n=function(){this.dpadRight=this.dpadLeft=this.dpadDown=this.dpadUp=this.rightStickButton=this.leftStickButton=this.start=this.select=this.rightShoulder1=this.leftShoulder1=this.rightShoulder0=this.leftShoulder0=this.faceButton3=this.faceButton2=this.faceButton1=this.faceButton0=this.rightStickY=this.rightStickX=this.leftStickY=this.leftStickX=0;this.deadZoneRightStick=this.deadZoneLeftStick=0.25; -this.deadZoneShoulder1=this.deadZoneShoulder0=0;this.images=f.ImageDataUrls_Unknown;this.name="Unknown"},i=navigator.userAgent,o=-1!=i.indexOf("Windows NT"),p=-1!=i.indexOf("Macintosh"),q=-1!=i.indexOf("Chrome/");if(i=-1!=i.indexOf("Firefox/"))navigator.mozGamepads=[],window.addEventListener("MozGamepadConnected",function(b){navigator.mozGamepads[b.gamepad.index]=b.gamepad}),window.addEventListener("MozGamepadDisconnected",function(b){navigator.mozGamepads[b.gamepad.index]=void 0});var r=function(b, -a){var e=b[a];if(e){void 0===j[a]&&(k[a]=new n,j[a]=new n);var c=k[a],d=j[a];c.leftStickX=d.leftStickX;c.leftStickY=d.leftStickY;c.rightStickX=d.rightStickX;c.rightStickY=d.rightStickY;c.faceButton0=d.faceButton0;c.faceButton1=d.faceButton1;c.faceButton2=d.faceButton2;c.faceButton3=d.faceButton3;c.leftShoulder0=d.leftShoulder0;c.rightShoulder0=d.rightShoulder0;c.leftShoulder1=d.leftShoulder1;c.rightShoulder1=d.rightShoulder1;c.select=d.select;c.start=d.start;c.leftStickButton=d.leftStickButton;c.rightStickButton= -d.rightStickButton;c.dpadUp=d.dpadUp;c.dpadDown=d.dpadDown;c.dpadLeft=d.dpadLeft;c.dpadRight=d.dpadRight;a:{for(var c=j[a],d=g.length,h=0;hb.axes[2]?-b.axes[2]:0;a.select=b.buttons[6];a.start=b.buttons[7];a.leftStickButton=b.buttons[8];a.rightStickButton=b.buttons[9];a.dpadUp=-0.5>b.axes[6]?1:0;a.dpadDown=0.5b.axes[5]?1:0;a.dpadRight=0.5 0 ? raw.axes[2] : 0; + into.rightShoulder1 = raw.axes[2] < 0 ? -raw.axes[2] : 0; + into.select = raw.buttons[6]; + into.start = raw.buttons[7]; + into.leftStickButton = raw.buttons[8]; + into.rightStickButton = raw.buttons[9]; + into.dpadUp = raw.axes[6] < -0.5 ? 1 : 0; + into.dpadDown = raw.axes[6] > 0.5 ? 1 : 0; + into.dpadLeft = raw.axes[5] < -0.5 ? 1 : 0; + into.dpadRight = raw.axes[5] > 0.5 ? 1 : 0; + // From http://msdn.microsoft.com/en-us/library/windows/desktop/ee417001(v=vs.85).aspx + into.deadZoneLeftStick = 7849.0/32767.0; + into.deadZoneRightStick = 8689/32767.0; + into.deadZoneShoulder0 = 0.5; + into.deadZoneShoulder1 = 30.0/255.0; + } + + var CommonMacXbox360Controller = function(raw, into, index) { + // NOTE: Partial, doesn't set all values. + into.leftStickX = raw.axes[0]; + into.leftStickY = raw.axes[1]; + into.faceButton0 = raw.buttons[0]; + into.faceButton1 = raw.buttons[1]; + into.faceButton2 = raw.buttons[2]; + into.faceButton3 = raw.buttons[3]; + into.leftShoulder0 = raw.buttons[4]; + into.rightShoulder0 = raw.buttons[5]; + into.select = raw.buttons[9]; + into.start = raw.buttons[8]; + into.leftStickButton = raw.buttons[6]; + into.rightStickButton = raw.buttons[7]; + into.dpadUp = raw.buttons[11]; + into.dpadDown = raw.buttons[12]; + into.dpadLeft = raw.buttons[13]; + into.dpadRight = raw.buttons[14]; + // From http://msdn.microsoft.com/en-us/library/windows/desktop/ee417001(v=vs.85).aspx + into.deadZoneLeftStick = 7849.0/32767.0; + into.deadZoneRightStick = 8689/32767.0; + into.deadZoneShoulder0 = 0.5; + into.deadZoneShoulder1 = 30.0/255.0; + } + + var ChromeMacXbox360Controller = function(raw, into, index) { + CommonMacXbox360Controller(raw, into, index); + into.rightStickX = raw.axes[3]; + into.rightStickY = raw.axes[4]; + into.leftShoulder1 = axisToButton(raw.axes[2]); + into.rightShoulder1 = axisToButton(raw.axes[5]); + }; + + var ChromeMacLogitechF310Controller = function(raw, into, index) { + into.leftStickX = raw.axes[0]; + into.leftStickY = raw.axes[1]; + into.faceButton0 = raw.buttons[1]; + into.faceButton1 = raw.buttons[2]; + into.faceButton2 = raw.buttons[0]; + into.faceButton3 = raw.buttons[3]; + into.leftShoulder0 = raw.buttons[4]; + into.rightShoulder0 = raw.buttons[5]; + into.select = raw.buttons[8]; + into.start = raw.buttons[9]; + into.leftStickButton = raw.buttons[10]; + into.rightStickButton = raw.buttons[11]; + + // There is a switch to toggle the left joystick and dpad + // only one is enabled at a time and the output always goes + // through the left joystick + into.dpadUp = 0; + into.dpadDown = 0; + into.dpadLeft = 0; + into.dpadRight = 0; + + into.deadZoneLeftStick = 7849.0/32767.0; + into.deadZoneRightStick = 8689/32767.0; + into.deadZoneShoulder0 = 0.5; + into.deadZoneShoulder1 = 30.0/255.0; + console.log(raw.axes); + into.rightStickX = raw.axes[2]; + into.rightStickY = raw.axes[5]; + into.leftShoulder1 = raw.buttons[6]; + into.rightShoulder1 = raw.buttons[7]; + }; + + var FirefoxMacXbox360Controller = function(raw, into, index) { + CommonMacXbox360Controller(raw, into, index); + into.rightStickX = raw.axes[2]; + into.rightStickY = raw.axes[3]; + into.leftShoulder1 = axisToButton(raw.axes[4]); + into.rightShoulder1 = axisToButton(raw.axes[5]); + }; + + var CommonMacPS3Controller = function(Raw, into, index) { + // NOTE: Partial, doesn't set all values. + into.leftStickX = raw.axes[0]; + into.leftStickY = raw.axes[1]; + into.rightStickX = raw.axes[2]; + into.faceButton0 = raw.buttons[14]; + into.faceButton1 = raw.buttons[13]; + into.faceButton2 = raw.buttons[15]; + into.faceButton3 = raw.buttons[12]; + into.leftShoulder0 = raw.buttons[10]; + into.rightShoulder0 = raw.buttons[11]; + into.leftShoulder1 = raw.buttons[8]; + into.rightShoulder1 = raw.buttons[9]; + into.select = raw.buttons[0]; + into.start = raw.buttons[3]; + into.leftStickButton = raw.buttons[1]; + into.rightStickButton = raw.buttons[2]; + into.dpadUp = raw.buttons[4]; + into.dpadDown = raw.buttons[6]; + into.dpadLeft = raw.buttons[7]; + into.dpadRight = raw.buttons[5]; + }; + + var FirefoxMacPS3Controller = function(raw, into, index) { + CommonMacPS3Controller(raw, into, index); + into.rightStickY = raw.axes[3]; + }; + + var ChromeMacPS3Controller = function(raw, into, index) { + into.rightStickY = raw.axes[5]; + }; + + var Gamepad_ImageDataUrls_PS3 = {}; + var Gamepad_ImageDataUrls_Unknown = {}; + var Gamepad_ImageDataUrls_Xbox360 = {}; + + var active = []; + // todo; possible we need to add different deadzones based on controller + // manufacturer, but perhaps they're fairly close anyway. + if (isChrome && isWindows) { + active.push([ 'XInput ', 'GAMEPAD', ChromeWindowsXinputGamepad, "Xbox 360", Gamepad_ImageDataUrls_Xbox360 ]); + } else if (isChrome && isMac) { + active.push([ 'Vendor: 045e', 'Product: 028e', ChromeMacXbox360Controller, "Xbox 360", Gamepad_ImageDataUrls_Xbox360 ]); + active.push([ 'Vendor: 045e', 'Product: 02a1', ChromeMacXbox360Controller, "Xbox 360", Gamepad_ImageDataUrls_Xbox360 ]); + active.push([ 'Vendor: 054c', 'Product: 0268', ChromeMacPS3Controller, "Playstation 3", Gamepad_ImageDataUrls_PS3 ]); + active.push([ 'Vendor: 046d', 'Product: c216', ChromeMacLogitechF310Controller, "Logitech F310", Gamepad_ImageDataUrls_Xbox360 ]); + } else if (isFirefox && isWindows) { + active.push([ '45e-', '28e-', FirefoxWindowsXbox360Controller, "Xbox 360", Gamepad_ImageDataUrls_Xbox360 ]); + active.push([ '45e-', '2a1-', FirefoxWindowsXbox360Controller, "Xbox 360", Gamepad_ImageDataUrls_Xbox360 ]); + active.push([ '46d-', 'c21d-', FirefoxWindowsXbox360Controller, "Logitech F310", Gamepad_ImageDataUrls_Xbox360 ]); + active.push([ '46d-', 'c21e-', FirefoxWindowsXbox360Controller, "Logitech F510", Gamepad_ImageDataUrls_Xbox360 ]); + } else if (isFirefox && isMac) { + active.push([ '45e-', '28e-', FirefoxMacXbox360Controller, "Xbox 360", Gamepad_ImageDataUrls_Xbox360 ]); + active.push([ '54c-', '268-', FirefoxMacPS3Controller, "Playstation 3", Gamepad_ImageDataUrls_PS3 ]); + } +})(); diff --git a/Upstream/Gamepad.js b/Upstream/Gamepad.js index dccf1e364..7779600f7 160000 --- a/Upstream/Gamepad.js +++ b/Upstream/Gamepad.js @@ -1 +1 @@ -Subproject commit dccf1e364e63e4b03cfbfb1745339052a0d03497 +Subproject commit 7779600f7e0ef5410291738d29bb4b3ca58d4fd5 diff --git a/jsil.org/demos/Platformer/Platformer.html b/jsil.org/demos/Platformer/Platformer.html index 5b56b15c8..1184ecd40 100644 --- a/jsil.org/demos/Platformer/Platformer.html +++ b/jsil.org/demos/Platformer/Platformer.html @@ -14,6 +14,8 @@ printStackTrace: false, webgl2d: true, xna: 3, + gamepad: true, + manifests: [ "PlatformerStarterKit.exe", "Content/HighResolutionContent.contentproj", diff --git a/jsil.org/demos/RPG/RPG.html b/jsil.org/demos/RPG/RPG.html index babb586fa..d0eb66baa 100644 --- a/jsil.org/demos/RPG/RPG.html +++ b/jsil.org/demos/RPG/RPG.html @@ -15,6 +15,8 @@ webgl2d: true, xna: 4, localStorage: true, + gamepad: true, + manifests: [ "RolePlayingGame.exe", "Content/RolePlayingGameContentWindows.contentproj"