diff --git a/examples/gamepad-tester/gamepad-tester.json b/examples/gamepad-tester/gamepad-tester.json index a3c3303eb..957fdb2b1 100644 --- a/examples/gamepad-tester/gamepad-tester.json +++ b/examples/gamepad-tester/gamepad-tester.json @@ -1,7 +1,7 @@ { "firstLayout": "", "gdVersion": { - "build": 240, + "build": 241, "major": 5, "minor": 5, "revision": 0 @@ -566,6 +566,32 @@ ] }, "events": [ + { + "type": "BuiltinCommonInstructions::Standard", + "conditions": [ + { + "type": { + "value": "KeyFromTextReleased" + }, + "parameters": [ + "", + "\"r\"" + ] + } + ], + "actions": [ + { + "type": { + "value": "Scene" + }, + "parameters": [ + "", + "\"GamepadTester\"", + "" + ] + } + ] + }, { "type": "BuiltinCommonInstructions::Standard", "conditions": [], @@ -884,6 +910,34 @@ "conditions": [], "actions": [], "events": [ + { + "type": "BuiltinCommonInstructions::Standard", + "conditions": [ + { + "type": { + "value": "Gamepads::IsButtonJustPressed" + }, + "parameters": [ + "", + "PlayerId", + "ButtonName", + "" + ] + } + ], + "actions": [ + { + "type": { + "value": "SetStringVariable" + }, + "parameters": [ + "Log", + "+", + "ButtonName + \" was just pressed\" + NewLine()" + ] + } + ] + }, { "type": "BuiltinCommonInstructions::Standard", "conditions": [ @@ -8367,7 +8421,7 @@ "name": "Gamepads", "previewIconUrl": "https://resources.gdevelop-app.com/assets/Icons/gamepad-variant-outline.svg", "shortDescription": "Add support for gamepads (or other controllers) to your game, giving access to information such as button presses, axis positions, trigger pressure, etc...", - "version": "0.8.1", + "version": "0.9.0", "description": [ "Add support for gamepads (or other physical controllers).", "", @@ -8399,7 +8453,8 @@ "authorIds": [ "2OwwM8ToR9dx9RJ2sAKTcrLmCB92", "taRwmWxwAFYFL9yyBwB3cwBw0BO2", - "mnImQKdn8nQxwzkS5D6a1JB27V23" + "mnImQKdn8nQxwzkS5D6a1JB27V23", + "IWykYNRvhCZBN3vEgKEbBPOR3Oc2" ], "dependencies": [], "globalVariables": [], @@ -8458,83 +8513,323 @@ "name": "onFirstSceneLoaded", "sentence": "", "events": [ - { - "type": "BuiltinCommonInstructions::Standard", - "conditions": [], - "actions": [] - }, { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Define an new private object javascript for the gamepad extension\r", - "gdjs._extensionController = {\r", - " players: {\r", - " 0: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - " 1: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - " 2: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - " 3: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", + "if (gdjs._extensionController) {\r", + " return;\r", + "}\r", + "\r", + "/**\r", + " * Associate controller button ids to button names\r", + " */\r", + "const controllerButtonNames = {\r", + " \"XBOX\": {\r", + " 0: \"A\",\r", + " 1: \"B\",\r", + " 2: \"X\",\r", + " 3: \"Y\",\r", + " 4: \"LB\",\r", + " 5: \"RB\",\r", + " 6: \"LT\",\r", + " 7: \"RT\",\r", + " 8: \"BACK\",\r", + " 9: \"START\",\r", + " 10: \"CLICK_STICK_LEFT\",\r", + " 11: \"CLICK_STICK_RIGHT\",\r", + " 12: \"UP\",\r", + " 13: \"DOWN\",\r", + " 14: \"LEFT\",\r", + " 15: \"RIGHT\",\r", + " 16: \"NONE\",\r", + " 17: \"NONE\"\r", " },\r", - " lastActiveController: -1, // Last active controller\r", - " controllerButtonNames: { //Map associating controller button ids to button names\r", - " \"XBOX\": {\r", - " 0: \"A\",\r", - " 1: \"B\",\r", - " 2: \"X\",\r", - " 3: \"Y\",\r", - " 4: \"LB\",\r", - " 5: \"RB\",\r", - " 6: \"LT\",\r", - " 7: \"RT\",\r", - " 8: \"BACK\",\r", - " 9: \"START\",\r", - " 10: \"CLICK_STICK_LEFT\",\r", - " 11: \"CLICK_STICK_RIGHT\",\r", - " 12: \"UP\",\r", - " 13: \"DOWN\",\r", - " 14: \"LEFT\",\r", - " 15: \"RIGHT\",\r", - " 16: \"NONE\",\r", - " 17: \"NONE\"\r", - " },\r", - " \"PS4\": {\r", - " 0: \"CROSS\",\r", - " 1: \"CIRCLE\",\r", - " 2: \"SQUARE\",\r", - " 3: \"TRIANGLE\",\r", - " 4: \"L1\",\r", - " 5: \"R1\",\r", - " 6: \"L2\",\r", - " 7: \"R2\",\r", - " 8: \"SHARE\",\r", - " 9: \"OPTIONS\",\r", - " 10: \"CLICK_STICK_LEFT\",\r", - " 11: \"CLICK_STICK_RIGHT\",\r", - " 12: \"UP\",\r", - " 13: \"DOWN\",\r", - " 14: \"LEFT\",\r", - " 15: \"RIGHT\",\r", - " 16: \"PS_BUTTON\",\r", - " 17: \"CLICK_TOUCHPAD\"\r", - " }\r", + " \"PS4\": {\r", + " 0: \"CROSS\",\r", + " 1: \"CIRCLE\",\r", + " 2: \"SQUARE\",\r", + " 3: \"TRIANGLE\",\r", + " 4: \"L1\",\r", + " 5: \"R1\",\r", + " 6: \"L2\",\r", + " 7: \"R2\",\r", + " 8: \"SHARE\",\r", + " 9: \"OPTIONS\",\r", + " 10: \"CLICK_STICK_LEFT\",\r", + " 11: \"CLICK_STICK_RIGHT\",\r", + " 12: \"UP\",\r", + " 13: \"DOWN\",\r", + " 14: \"LEFT\",\r", + " 15: \"RIGHT\",\r", + " 16: \"PS_BUTTON\",\r", + " 17: \"CLICK_TOUCHPAD\"\r", " }\r", "};\r", "\r", - "gdjs._extensionController.getInputString = function (type, buttonId) {\r", - " const controllerButtonNames = gdjs._extensionController.controllerButtonNames;\r", - " if (controllerButtonNames[type] !== undefined) {\r", - " return controllerButtonNames[type][buttonId];\r", + "/**\r", + " * @param {number} playerId\r", + " */\r", + "function getGamepad(playerId) {\r", + " /** @type {Gamepad[]} */\r", + " const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", + " return gamepads[playerId];\r", + "}\r", + "\r", + "/** @type {{[playerId: number]: Player}} */\r", + "const players = {};\r", + "\r", + "/**\r", + " * @param {number} playerId\r", + " */\r", + "function getPlayer(playerId) {\r", + " let player = players[playerId];\r", + " if (!player) {\r", + " player = new Player(playerId);\r", + " players[playerId] = player;\r", + " }\r", + " return player;\r", + "}\r", + "\r", + "class Player {\r", + " /** @type {number} */\r", + " playerId;\r", + " mapping = 'DEFAULT';\r", + " lastButtonUsed = -1;\r", + " deadzone = 0.2;\r", + " /** @type {{[buttonId: number]: ButtonState}} */\r", + " buttonStates = {};\r", + " rumble = { elapsedTime: 0, duration: 0, weakMagnitude: 0, strongMagnitude: 0 };\r", + "\r", + " /**\r", + " * @param {number} playerId\r", + " */\r", + " constructor(playerId) {\r", + " this.playerId = playerId;\r", + " }\r", + "\r", + " /**\r", + " * @param {number} buttonId\r", + " */\r", + " getButtonState(buttonId) {\r", + " let buttonState = this.buttonStates[buttonId];\r", + " if (!buttonState) {\r", + " buttonState = new ButtonState();\r", + " this.buttonStates[buttonId] = buttonState;\r", + " }\r", + " return buttonState;\r", + " }\r", + "\r", + " /**\r", + " * @param {number} buttonId\r", + " */\r", + " isButtonPressed(buttonId) {\r", + " return this.getButtonState(buttonId).isPressed;\r", + " }\r", + "\r", + " /**\r", + " * @param {number} buttonId\r", + " */\r", + " isButtonJustPressed(buttonId) {\r", + " return this.getButtonState(buttonId).isJustPressed();\r", + " }\r", + "\r", + " /**\r", + " * @param {number} buttonId\r", + " */\r", + " isButtonReleased(buttonId) {\r", + " return this.getButtonState(buttonId).isReleased();\r", + " }\r", + "\r", + " isAnyButtonReleased() {\r", + " for (const buttonId in this.buttonStates) {\r", + " const buttonState = this.buttonStates[buttonId];\r", + " if (buttonState.isReleased()) {\r", + " return true;\r", + " }\r", + " }\r", + " return false;\r", + " }\r", + "\r", + " isAnyButtonPressed() {\r", + " for (const buttonId in this.buttonStates) {\r", + " const buttonState = this.buttonStates[buttonId];\r", + " if (buttonState.isPressed) {\r", + " return true;\r", + " }\r", + " }\r", + " return false;\r", + " }\r", + "}\r", + "\r", + "class ButtonState {\r", + " wasPressed = false;\r", + " isPressed = false;\r", + "\r", + " isReleased() {\r", + " return this.wasPressed && !this.isPressed;\r", + " }\r", + "\r", + " isJustPressed() {\r", + " return !this.wasPressed && this.isPressed;\r", + " }\r", + "}\r", + "\r", + "// Async tasks are run before everything.\r", + "// This is a hack to make sure that button states are updated\r", + "// before mapping behavior events.\r", + "const frameBeginningTask = new class extends gdjs.AsyncTask {\r", + " update() {\r", + " /** @type {Gamepad[]} */\r", + " const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", + " for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", + " const gamepad = gamepads[playerId];\r", + " if (gamepad == null) {\r", + " // The gamepad is not connected.\r", + " continue;\r", + " }\r", + " const player = getPlayer(playerId);\r", + "\r", + " for (let buttonId = 0; buttonId < Object.keys(gamepad.buttons).length; buttonId++) {\r", + " const buttonState = player.getButtonState(buttonId);\r", + " buttonState.wasPressed = buttonState.isPressed;\r", + " buttonState.isPressed = gamepad.buttons[buttonId].pressed;\r", + " if (buttonState.isJustPressed()) {\r", + " player.lastButtonUsed = buttonId;\r", + " }\r", + " }\r", + " }\r", + " return false;\r", + " }\r", + "}();\r", + "\r", + "function onScenePostEvents() {\r", + " /** @type {Gamepad[]} */\r", + " const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", + " for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", + " let gamepad = gamepads[playerId];\r", + " if (gamepad == null) {\r", + " // The gamepad is not connected.\r", + " continue;\r", + " }\r", + " const player = getPlayer(playerId);\r", + " const rumble = player.rumble;\r", + " rumble.elapsedTime += runtimeScene.getElapsedTime(runtimeScene) / 1000;\r", + " if (rumble.duration - rumble.elapsedTime <= 0 &&\r", + " (rumble.weakMagnitude || rumble.strongMagnitude)\r", + " ) {\r", + " rumble.weakMagnitude = 0;\r", + " rumble.strongMagnitude = 0;\r", + " }\r", + " }\r", + "}\r", + "\r", + "/**\r", + " * @param {string} type\r", + " * @param {number} buttonId\r", + " */\r", + "function getInputString(type, buttonId) {\r", + " if (!controllerButtonNames[type]) {\r", + " return \"UNKNOWN_BUTTON\";\r", + " }\r", + " return controllerButtonNames[type][buttonId];\r", + "}\r", + "\r", + "function getButtonId(buttonName) {\r", + " switch (buttonName) {\r", + " case 'A':\r", + " case 'CROSS':\r", + " return 0;\r", + " case 'B':\r", + " case 'CIRCLE':\r", + " return 1;\r", + " case 'X':\r", + " case 'SQUARE':\r", + " return 2;\r", + " case 'Y':\r", + " case 'TRIANGLE':\r", + " return 3;\r", + " case 'LB':\r", + " case 'L1':\r", + " return 4;\r", + " case 'RB':\r", + " case 'R1':\r", + " return 5;\r", + " case 'LT':\r", + " case 'L2':\r", + " return 6;\r", + " case 'RT':\r", + " case 'R2':\r", + " return 7;\r", + " case 'UP':\r", + " return 12;\r", + " case 'DOWN':\r", + " return 13;\r", + " case 'LEFT':\r", + " return 14;\r", + " case 'RIGHT':\r", + " return 15;\r", + " case 'BACK':\r", + " case 'SHARE':\r", + " return 8;\r", + " case 'START':\r", + " case 'OPTIONS':\r", + " return 9;\r", + " case 'CLICK_STICK_LEFT':\r", + " return 10;\r", + " case 'CLICK_STICK_RIGHT':\r", + " return 11;\r", + " //PS4\r", + " case 'PS_BUTTON':\r", + " return 16;\r", + " case 'CLICK_TOUCHPAD':\r", + " return 17;\r", + " default:\r", + " console.error('The gamepad button: ' + buttonName + ' is not valid.');\r", + " return null;\r", " }\r", + "}\r", "\r", - " return \"UNKNOWN_BUTTON\";\r", + "/**\r", + " * @param {number} playerId\r", + " * @param {string} directionName\r", + " * @param {number} axisValueX\r", + " * @param {number} axisValueY\r", + " */\r", + "function isAxisPushed(playerId, directionName, axisValueX, axisValueY) {\r", + " switch (directionName) {\r", + " case 'LEFT':\r", + " return getNormalizedAxisValue(axisValueX, playerId) < 0;\r", + " case 'RIGHT':\r", + " return getNormalizedAxisValue(axisValueX, playerId) > 0;\r", + " case 'UP':\r", + " return getNormalizedAxisValue(axisValueY, playerId) < 0;\r", + " case 'DOWN':\r", + " return getNormalizedAxisValue(axisValueY, playerId) > 0;\r", + " case 'ANY':\r", + " return getNormalizedAxisValue(axisValueX, playerId) < 0\r", + " || getNormalizedAxisValue(axisValueX, playerId) > 0\r", + " || getNormalizedAxisValue(axisValueY, playerId) < 0\r", + " || getNormalizedAxisValue(axisValueY, playerId) > 0\r", + " default:\r", + " console.error('The value stick direction is not valid.');\r", + " return false;\r", + " }\r", "}\r", "\r", - "gdjs._extensionController.axisToAngle = function (deltaX, deltaY) {\r", + "/**\r", + " * @param {number} deltaX\r", + " * @param {number} deltaY\r", + " */\r", + "function axisToAngle(deltaX, deltaY) {\r", " const rad = Math.atan2(deltaY, deltaX);\r", " const deg = rad * (180 / Math.PI);\r", " return deg;\r", "}\r", "\r", - "gdjs._extensionController.isXbox = function (gamepad) {\r", + "/**\r", + " * @param {{id: string}} gamepad\r", + " */\r", + "function isXbox(gamepad) {\r", " return (gamepad ? (\r", " gamepad.id.toUpperCase().indexOf(\"XBOX\") !== -1\r", " // \"XINPUT\" cannot be used to check if it is a xbox controller is just a generic\r", @@ -8543,44 +8838,52 @@ " ) : false);\r", "}\r", "\r", - "//Returns the new value taking into account the dead zone for the player_ID given\r", - "gdjs._extensionController.getNormalizedAxisValue = function (v, player_ID) {\r", + "/**\r", + " * Returns the new value taking into account the dead zone for the player_ID given\r", + " * @param {number} value\r", + " * @param {number} playerID\r", + " */\r", + "function getNormalizedAxisValue(value, playerID) {\r", " // gdjs._extensionController = gdjs._extensionController || { deadzone: 0.2 };\r", "\r", " // Anything smaller than this is assumed to be 0,0\r", - " const DEADZONE = gdjs._extensionController.players[player_ID].deadzone;\r", - "\r", - " if (Math.abs(v) < DEADZONE) {\r", - " // In the dead zone, set to 0\r", - " v = 0;\r", - "\r", - " if (v == null) {\r", - " return 0;\r", - " } else {\r", - " return v;\r", - " }\r", + " const deadzone = getPlayer(playerID).deadzone;\r", "\r", + " if (Math.abs(value) < deadzone) {\r", + " return 0;\r", " } else {\r", " // We're outside the dead zone, but we'd like to smooth\r", " // this value out so it still runs nicely between 0..1.\r", " // That is, we don't want it to jump suddenly from 0 to\r", - " // DEADZONE.\r", + " // deadzone.\r", "\r", - " // Remap v from\r", - " // DEADZONE..1 to 0..(1-DEADZONE)\r", + " // Remap value from\r", + " // deadzone..1 to 0..(1-deadzone)\r", " // or from\r", - " // -1..-DEADZONE to -(1-DEADZONE)..0\r", + " // -1..-deadzone to -(1-deadzone)..0\r", + " value = value - Math.sign(value) * deadzone;\r", "\r", - " v = v - Math.sign(v) * DEADZONE;\r", - "\r", - " // Remap v from\r", - " // 0..(1-DEADZONE) to 0..1\r", + " // Remap value from\r", + " // 0..(1-deadzone) to 0..1\r", " // or from\r", - " // -(1-DEADZONE)..0 to -1..0\r", - "\r", - " return v / (1 - DEADZONE);\r", + " // -(1-deadzone)..0 to -1..0\r", + " return value / (1 - deadzone);\r", " }\r", - "};" + "}\r", + "\r", + "gdjs._extensionController = {\r", + " getPlayer,\r", + " controllerButtonNames,\r", + " getInputString,\r", + " getButtonId,\r", + " axisToAngle,\r", + " isXbox,\r", + " getNormalizedAxisValue,\r", + " isAxisPushed,\r", + " getGamepad,\r", + " onScenePostEvents,\r", + " frameBeginningTask,\r", + "}" ], "parameterObjects": "", "useStrict": true, @@ -8593,59 +8896,35 @@ { "fullName": "", "functionType": "Action", - "name": "onScenePostEvents", + "name": "onSceneLoaded", "sentence": "", "events": [ { - "type": "BuiltinCommonInstructions::Standard", - "conditions": [], - "actions": [] - }, + "type": "BuiltinCommonInstructions::JsCode", + "inlineCode": [ + "// Async tasks are run before everything.\r", + "// This is a hack to make sure that button states are updated\r", + "// before mapping behavior events.\r", + "runtimeScene.getAsyncTasksManager().addTask(gdjs._extensionController.frameBeginningTask);" + ], + "parameterObjects": "", + "useStrict": true, + "eventsSheetExpanded": false + } + ], + "parameters": [], + "objectGroups": [] + }, + { + "fullName": "", + "functionType": "Action", + "name": "onScenePostEvents", + "sentence": "", + "events": [ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Each time a player press a button i save the last button pressed for the next frame", - "/** @type {Gamepad[]} */", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", - "//Get function parameter", - "let countPlayers = Object.keys(gdjs._extensionController.players).length;", - "", - "//Repeat for each players", - "for (let i = 0; i < countPlayers; i++) {", - " let gamepad = gamepads[i]; // Get the gamepad of the player", - "", - " //We have to keep this condition because if the user hasn't plugged in his controller yet, we can't get the controller in the gamepad variable.", - " if (gamepad == null) {", - " continue;", - " }", - "", - " for (let b = 0; b < Object.keys(gamepad.buttons).length; b++) { //For each buttons", - " if (gamepad.buttons[b].pressed) { //One of them is pressed", - " gdjs._extensionController.players[i].lastButtonUsed = b; //Save the button pressed", - "", - " //Save the state of the button for the next frame.", - " gdjs._extensionController.players[i].previousFrameStateButtons[b] = { pressed: true };", - "", - " // Update Last Active Controller", - " gdjs._extensionController.lastActiveController = i;", - " } else {", - " gdjs._extensionController.players[i].previousFrameStateButtons[b] = { pressed: false };", - " }", - " }", - "", - "", - " gdjs._extensionController.players[i].rumble.elapsedTime += runtimeScene.getElapsedTime(runtimeScene) / 1000;", - " if (", - " gdjs._extensionController.players[i].rumble.duration - gdjs._extensionController.players[i].rumble.elapsedTime <= 0 &&", - " (gdjs._extensionController.players[i].rumble.weakMagnitude || gdjs._extensionController.players[i].rumble.strongMagnitude)", - " ) {", - " gdjs._extensionController.players[i].rumble.weakMagnitude = 0;", - " gdjs._extensionController.players[i].rumble.strongMagnitude = 0;", - " }", - "", - "", - "}", + "gdjs._extensionController.onScenePostEvents();", "" ], "parameterObjects": "", @@ -9083,27 +9362,20 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "//Get function parameters\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "const trigger = eventsFunctionContext.getArgument(\"trigger\").toUpperCase();\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const trigger = eventsFunctionContext.getArgument(\"Trigger\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in expression: \"Pressure on a gamepad trigger\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", "if (trigger != \"LT\" && trigger != \"RT\" && trigger != \"L2\" && trigger != \"R2\") {\r", " console.error('Parameter trigger is not valid in expression: \"Pressure on a gamepad trigger\"');\r", " return;\r", "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "switch (trigger) {\r", " case 'LT':\r", " case 'L2':\r", @@ -9131,12 +9403,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Trigger button", - "name": "trigger", + "name": "Trigger", "supplementaryInformation": "[\"LT\",\"RT\",\"L2\",\"R2\"]", "type": "stringWithSelector" } @@ -9153,43 +9425,28 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", - "//Get function parameters\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", - "\r", + "const { getNormalizedAxisValue } = gdjs._extensionController;\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a stick force\"');\r", - " return;\r", - "}\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const stick = eventsFunctionContext.getArgument(\"Stick\").toUpperCase();\r", "\r", "if (stick !== \"LEFT\" && stick !== \"RIGHT\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a stick force\"');\r", " return;\r", "}\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", + "const axisValueX = stick === 'RIGHT' ? gamepad.axes[2] : gamepad.axes[0];\r", + "const axisValueY = stick === 'RIGHT' ? gamepad.axes[3] : gamepad.axes[1];\r", "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", - "\r", - "switch (stick) {\r", - " case 'LEFT':\r", - " eventsFunctionContext.returnValue = gdjs.evtTools.common.clamp(Math.abs(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[0], playerId)) + Math.abs(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[1], playerId)), 0, 1);\r", - " break;\r", - "\r", - " case 'RIGHT':\r", - " eventsFunctionContext.returnValue = gdjs.evtTools.common.clamp(Math.abs(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[2], playerId)) + Math.abs(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[3], playerId)), 0, 1);\r", - " break;\r", - "\r", - " default:\r", - " eventsFunctionContext.returnValue = -1;\r", - " break;\r", - "}" + "eventsFunctionContext.returnValue = gdjs.evtTools.common.clamp(\r", + " Math.abs(getNormalizedAxisValue(axisValueX, playerId)) +\r", + " Math.abs(getNormalizedAxisValue(axisValueY, playerId)), 0, 1);\r", + "" ], "parameterObjects": "", "useStrict": true, @@ -9202,12 +9459,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Stick: \"Left\" or \"Right\"", - "name": "stick", + "name": "Stick", "supplementaryInformation": "[\"Left\",\"Right\"]", "type": "stringWithSelector" } @@ -9231,7 +9488,7 @@ "value": "SetReturnNumber" }, "parameters": [ - "Gamepads::StickAngle(player_ID, stick)" + "Gamepads::StickAngle(PlayerId, Stick)" ] } ] @@ -9243,12 +9500,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Stick: \"Left\" or \"Right\"", - "name": "stick", + "name": "Stick", "supplementaryInformation": "[\"Left\",\"Right\"]", "type": "stringWithSelector" } @@ -9265,40 +9522,27 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", - "//Get function parameters\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", + "const { getNormalizedAxisValue } = gdjs._extensionController;\r", "\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const stick = eventsFunctionContext.getArgument(\"Stick\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a stick rotation\"');\r", - " return;\r", - "}\r", "if (stick !== \"LEFT\" && stick !== \"RIGHT\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a stick rotation\"');\r", " return;\r", "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", - "switch (stick) {\r", - " case 'LEFT':\r", - " eventsFunctionContext.returnValue = gdjs._extensionController.axisToAngle(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[0], playerId), gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[1], playerId));\r", - " break;\r", - "\r", - " case 'RIGHT':\r", - " eventsFunctionContext.returnValue = gdjs._extensionController.axisToAngle(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[2], playerId), gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[3], playerId));\r", - " break;\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", + "const axisValueX = stick === 'RIGHT' ? gamepad.axes[2] : gamepad.axes[0];\r", + "const axisValueY = stick === 'RIGHT' ? gamepad.axes[3] : gamepad.axes[1];\r", "\r", - " default:\r", - " eventsFunctionContext.returnValue = -1;\r", - " break;\r", - "}" + "eventsFunctionContext.returnValue = gdjs._extensionController.axisToAngle(\r", + " getNormalizedAxisValue(axisValueX, playerId),\r", + " getNormalizedAxisValue(axisValueY, playerId));" ], "parameterObjects": "", "useStrict": true, @@ -9311,12 +9555,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Stick: \"Left\" or \"Right\"", - "name": "stick", + "name": "Stick", "supplementaryInformation": "[\"Left\",\"Right\"]", "type": "stringWithSelector" } @@ -9334,18 +9578,11 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "//Get function parameters\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "const direction = eventsFunctionContext.getArgument(\"direction\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a gamepad axis\"');\r", - " return;\r", - "}\r", "if (stick != \"LEFT\" && stick != \"RIGHT\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", @@ -9354,11 +9591,12 @@ " console.error('Parameter direction is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "let parameterError = false;\r", "switch (stick) {\r", " case 'LEFT':\r", @@ -9455,7 +9693,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -9483,26 +9721,20 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"Gamepad\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"Stick\").toLowerCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a gamepad axis\"');\r", - " return;\r", - "}\r", "if (stick != \"left\" && stick != \"right\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "const axisIndex = stick === 'right' ? 2 : 0;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[axisIndex], playerId);\r", "" @@ -9540,26 +9772,20 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"Gamepad\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"Stick\").toLowerCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a gamepad axis\"');\r", - " return;\r", - "}\r", "if (stick != \"left\" && stick != \"right\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "const axisIndex = stick === 'right' ? 3 : 1;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[axisIndex], playerId);\r", "" @@ -9597,136 +9823,57 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "//Get function parameters\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const button = eventsFunctionContext.getArgument(\"Button\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in condition: \"Gamepad button released\", is not valid number, must be between 0 and 4.');\r", + "let buttonId = gdjs._extensionController.getButtonId(button);\r", + "if (buttonId === null) {\r", " return;\r", "}\r", - "if (button === \"\") {\r", - " console.error('Parameter button is not valid in condition: \"Gamepad button released\"');\r", - " return;\r", - "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", + "const player = gdjs._extensionController.getPlayer(playerId)\r", + "eventsFunctionContext.returnValue = player.isButtonReleased(buttonId);" + ], + "parameterObjects": "", + "useStrict": true, + "eventsSheetExpanded": true + } + ], + "parameters": [ + { + "description": "The gamepad identifier: 1, 2, 3 or 4", + "name": "PlayerId", + "type": "expression" + }, + { + "description": "Name of the button", + "name": "Button", + "supplementaryInformation": "[\"A\",\"Cross\",\"B\",\"Circle\",\"X\",\"Square\",\"Y\",\"Triangle\",\"LB\",\"L1\",\"RB\",\"R1\",\"LT\",\"L2\",\"RT\",\"R2\",\"Up\",\"Down\",\"Left\",\"Right\",\"Back\",\"Share\",\"Start\",\"Options\",\"Click_Stick_Left\",\"Click_Stick_Right\",\"PS_Button\",\"Click_Touchpad\"]", + "type": "stringWithSelector" + } + ], + "objectGroups": [] + }, + { + "description": "Check if a button was just pressed on a gamepad. Buttons can be:\n* Xbox: \"A\", \"B\", \"X\", \"Y\", \"LB\", \"RB\", \"LT\", \"RT\", \"BACK\", \"START\",\n* PS4: \"CROSS\", \"SQUARE\", \"CIRCLE\", \"TRIANGLE\", \"L1\", \"L2\", \"R1\", \"R2\", \"SHARE\", \"OPTIONS\", \"PS_BUTTON\", \"CLICK_TOUCHPAD\",\n* Other: \"UP\", \"DOWN\", \"LEFT\", \"RIGHT\", \"CLICK_STICK_LEFT\", \"CLICK_STICK_RIGHT\".", + "fullName": "Gamepad button just pressed", + "functionType": "Condition", + "name": "IsButtonJustPressed", + "sentence": "Button _PARAM2_ of gamepad _PARAM1_ was just pressed", + "events": [ + { + "type": "BuiltinCommonInstructions::JsCode", + "inlineCode": [ "\r", - "let buttonId;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const button = eventsFunctionContext.getArgument(\"Button\").toUpperCase();\r", "\r", - "switch (button) {\r", - " case 'A':\r", - " case 'CROSS':\r", - " buttonId = 0;\r", - " break;\r", - " case 'B':\r", - " case 'CIRCLE':\r", - " buttonId = 1;\r", - " break;\r", - " case 'X':\r", - " case 'SQUARE':\r", - " buttonId = 2;\r", - " break;\r", - " case 'Y':\r", - " case 'TRIANGLE':\r", - " buttonId = 3;\r", - " break;\r", - " case 'LB':\r", - " case 'L1':\r", - " buttonId = 4;\r", - " break;\r", - " case 'RB':\r", - " case 'R1':\r", - " buttonId = 5;\r", - " break;\r", - " case 'LT':\r", - " case 'L2':\r", - " buttonId = 6;\r", - " break;\r", - " case 'RT':\r", - " case 'R2':\r", - " buttonId = 7;\r", - " break;\r", - "\r", - " case 'UP':\r", - " buttonId = 12;\r", - " break;\r", - " case 'DOWN':\r", - " buttonId = 13;\r", - " break;\r", - " case 'LEFT':\r", - " buttonId = 14;\r", - " break;\r", - " case 'RIGHT':\r", - " buttonId = 15;\r", - " break;\r", - "\r", - " case 'BACK':\r", - " case 'SHARE':\r", - " buttonId = 8;\r", - " break;\r", - " case 'START':\r", - " case 'OPTIONS':\r", - " buttonId = 9;\r", - " break;\r", - "\r", - " case 'CLICK_STICK_LEFT':\r", - " buttonId = 10;\r", - " break;\r", - " case 'CLICK_STICK_RIGHT':\r", - " buttonId = 11;\r", - " break;\r", - "\r", - " //PS4\r", - " case 'PS_BUTTON':\r", - " buttonId = 16;\r", - " break;\r", - " case 'CLICK_TOUCHPAD':\r", - " buttonId = 17;\r", - " break;\r", - "\r", - " default:\r", - " console.error('The button: ' + button + ' in condition: \"Gamepad button released\" is not valid.');\r", - " break;\r", - "}\r", - "\r", - "if (buttonId === undefined) {\r", - " console.error('There is no buttons valid in condition: \"Gamepad button released\"');\r", - " eventsFunctionContext.returnValue = false;\r", + "let buttonId = gdjs._extensionController.getButtonId(button);\r", + "if (buttonId === null) {\r", " return;\r", "}\r", - "\r", - "if (gamepad.buttons == null || gamepad.buttons[buttonId] == null) {\r", - " console.error('Buttons on the gamepad are not accessible in condition: \"Gamepad button released\"');\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "\r", - "//Define default value on pressed button or use previous value\r", - "gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] || { pressed: false };\r", - "\r", - "//Get state of button at previous frame\r", - "const previousStateButton = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed;\r", - "\r", - "//When previousStateButton is true and actual button state is not pressed\r", - "//Player have release the button\r", - "if (previousStateButton === true && gamepad.buttons[buttonId].pressed === false) {\r", - " // Save the last button used for the player \r", - " gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", - " gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed = true;\r", - " eventsFunctionContext.returnValue = true;\r", - "\r", - "} else {\r", - " gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed = false;\r", - " eventsFunctionContext.returnValue = false;\r", - "}\r", - "" + "const player = gdjs._extensionController.getPlayer(playerId)\r", + "eventsFunctionContext.returnValue = player.isButtonJustPressed(buttonId);" ], "parameterObjects": "", "useStrict": true, @@ -9736,12 +9883,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Name of the button", - "name": "button", + "name": "Button", "supplementaryInformation": "[\"A\",\"Cross\",\"B\",\"Circle\",\"X\",\"Square\",\"Y\",\"Triangle\",\"LB\",\"L1\",\"RB\",\"R1\",\"LT\",\"L2\",\"RT\",\"R2\",\"Up\",\"Down\",\"Left\",\"Right\",\"Back\",\"Share\",\"Start\",\"Options\",\"Click_Stick_Left\",\"Click_Stick_Right\",\"PS_Button\",\"Click_Touchpad\"]", "type": "stringWithSelector" } @@ -9758,17 +9905,9 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameter\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "\r", - "//Player id is not valid\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in expression: \"Last pressed button (id)\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", - "\r", - "//Return the last button used by the player\r", - "eventsFunctionContext.returnValue = gdjs._extensionController.players[playerId].lastButtonUsed;" + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).lastButtonUsed;" ], "parameterObjects": "", "useStrict": true, @@ -9781,7 +9920,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -9797,46 +9936,10 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", - "//Get function parameter\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in condition: \"Any gamepad button pressed\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", - "let buttonId;\r", - "for (let i = 0; i < gamepad.buttons.length; i++) { //For each buttons\r", - " if (gamepad.buttons[i].pressed) { //One of them is pressed\r", - " buttonId = i; //Save the button pressed\r", - " break;\r", - " }\r", - "}\r", - "\r", - "if (buttonId === undefined) {\r", - " // No buttons are pressed.\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "\r", - "if (gamepad.buttons == null || gamepad.buttons[buttonId] == null) {\r", - " console.error('Buttons on the gamepad are not accessible in condition: \"Any gamepad button pressed\"');\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "\r", - "//When a button is pressed, save the button in lastButtonUsed for each players\r", - "if (gamepad.buttons[buttonId].pressed) gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", - "eventsFunctionContext.returnValue = gamepad.buttons[buttonId].pressed;\r", - "\r", "\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const player = gdjs._extensionController.getPlayer(playerId)\r", + "eventsFunctionContext.returnValue = player.isAnyButtonPressed();\r", "" ], "parameterObjects": "", @@ -9847,7 +9950,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -9863,35 +9966,18 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "//Get function parameters\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();\r", - "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in string expression: \"Last pressed button (LastButtonString)\", is not valid number, must be between 0 and 4.');\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", " return;\r", "}\r", - "if (controllerType === \"\") {\r", - " console.error('Parameter controller type is not valid in string expression: \"Last pressed button (LastButtonString)\"');\r", - " return;\r", - "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "if (gamepad !== null) { //Gamepad exist\r", - " //Get last btn id\r", - " const lastButtonUsedID = gdjs._extensionController.players[playerId].lastButtonUsed;\r", - "\r", - " //Return last button as string \r", - " eventsFunctionContext.returnValue = gdjs._extensionController.getInputString(controllerType, lastButtonUsedID);\r", - "\r", - "} else { //Gamepad dosen't exist\r", - " console.error('Your controller is not supported or the gamepad wasn\\'t detected in string expression: \"Last pressed button (LastButtonString)\"');\r", - " eventsFunctionContext.returnValue = \"Gamepad not connected\";\r", - "}" + "const lastButtonUsedID = gdjs._extensionController.getPlayer(playerId).lastButtonUsed;\r", + "eventsFunctionContext.returnValue = gdjs._extensionController.getInputString(controllerType, lastButtonUsedID);\r", + "" ], "parameterObjects": "", "useStrict": true, @@ -9904,7 +9990,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -9926,20 +10012,16 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", - "//Get the last activated controller\r", - "const controllerId = gdjs._extensionController.lastActiveController;\r", "\r", - "// Check if controller is active\r", - "const gamepad = gamepads[controllerId];\r", - "if (gamepad == null) {\r", - " eventsFunctionContext.returnValue = 0;\r", - "} else {\r", - " // Return active controller id\r", - " eventsFunctionContext.returnValue = controllerId + 1;\r", + "const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", + "let lastGamepadIndex = -1;\r", + "for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", + " // Gamepads can be disconnected and become null\r", + " if (gamepads[playerId]) {\r", + " lastGamepadIndex = playerId\r", + " }\r", "}\r", - "" + "eventsFunctionContext.returnValue = lastGamepadIndex + 1;" ], "parameterObjects": "", "useStrict": true, @@ -9962,127 +10044,16 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "//Get function parameters\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in condition: \"Gamepad button pressed\", is not valid number, must be between 0 and 4.');\r", + "const buttonId = gdjs._extensionController.getButtonId(button);\r", + "if (buttonId === null) {\r", " return;\r", "}\r", - "if (button === \"\") {\r", - " console.error('Parameter button is not valid in condition: \"Gamepad button pressed\"');\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", - "let buttonId;\r", - "\r", - "switch (button) {\r", - " case 'A':\r", - " case 'CROSS':\r", - " buttonId = 0;\r", - " break;\r", - " case 'B':\r", - " case 'CIRCLE':\r", - " buttonId = 1;\r", - " break;\r", - " case 'X':\r", - " case 'SQUARE':\r", - " buttonId = 2;\r", - " break;\r", - " case 'Y':\r", - " case 'TRIANGLE':\r", - " buttonId = 3;\r", - " break;\r", - " case 'LB':\r", - " case 'L1':\r", - " buttonId = 4;\r", - " break;\r", - " case 'RB':\r", - " case 'R1':\r", - " buttonId = 5;\r", - " break;\r", - " case 'LT':\r", - " case 'L2':\r", - " buttonId = 6;\r", - " break;\r", - " case 'RT':\r", - " case 'R2':\r", - " buttonId = 7;\r", - " break;\r", - "\r", - " case 'UP':\r", - " buttonId = 12;\r", - " break;\r", - " case 'DOWN':\r", - " buttonId = 13;\r", - " break;\r", - " case 'LEFT':\r", - " buttonId = 14;\r", - " break;\r", - " case 'RIGHT':\r", - " buttonId = 15;\r", - " break;\r", - "\r", - " case 'BACK':\r", - " case 'SHARE':\r", - " buttonId = 8;\r", - " break;\r", - " case 'START':\r", - " case 'OPTIONS':\r", - " buttonId = 9;\r", - " break;\r", - "\r", - " case 'CLICK_STICK_LEFT':\r", - " buttonId = 10;\r", - " break;\r", - " case 'CLICK_STICK_RIGHT':\r", - " buttonId = 11;\r", - " break;\r", - "\r", - " //PS4\r", - " case 'PS_BUTTON':\r", - " buttonId = 16;\r", - " break;\r", - " case 'CLICK_TOUCHPAD':\r", - " buttonId = 17;\r", - " break;\r", - "\r", - " default:\r", - " console.error('The button: ' + button + ' in condition: \"Gamepad button pressed\" is not valid.');\r", - " eventsFunctionContext.returnValue = false;\r", - " break;\r", - "}\r", - "\r", - "\r", - "\r", - "if (buttonId === undefined) {\r", - " console.error('There is no buttons valid in condition: \"Gamepad button pressed\"');\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "\r", - "if (gamepad.buttons == null || gamepad.buttons[buttonId] == null) {\r", - " console.error('Buttons on the gamepad are not accessible in condition: \"Gamepad button pressed\"');\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "\r", - "//When a button is pressed, save the button in lastButtonUsed for each players\r", - "if (gamepad.buttons[buttonId].pressed) gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", - "eventsFunctionContext.returnValue = gamepad.buttons[buttonId].pressed;\r", - "\r", - "\r", - "\r", + "const player = gdjs._extensionController.getPlayer(playerId)\r", + "eventsFunctionContext.returnValue = player.isButtonPressed(buttonId);\r", "" ], "parameterObjects": "", @@ -10093,7 +10064,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -10120,15 +10091,9 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameter\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in expression: \"Gamepad deadzone for sticks\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", - "///Return the deadzone value for a given player\r", - "eventsFunctionContext.returnValue = gdjs._extensionController.players[playerId].deadzone;" + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).deadzone;" ], "parameterObjects": "", "useStrict": true, @@ -10141,7 +10106,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -10157,18 +10122,12 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameter\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "const newDeadzone = eventsFunctionContext.getArgument(\"deadzone\");\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in action: \"Set gamepad deadzone for sticks\", is not valid, must be between 0 and 4.');\r", - " return;\r", - "}\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const newDeadzone = eventsFunctionContext.getArgument(\"deadzone\");\r", "\r", - "// clamp the newDeadzone in range [0, 1].\r", "// https://github.com/4ian/GDevelop-extensions/pull/33#issuecomment-618224857\r", - "gdjs._extensionController.players[playerId].deadzone = gdjs.evtTools.common.clamp(newDeadzone, 0, 1);\r", + "gdjs._extensionController.getPlayer(playerId).deadzone = gdjs.evtTools.common.clamp(newDeadzone, 0, 1);\r", "" ], "parameterObjects": "", @@ -10179,7 +10138,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -10200,141 +10159,28 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "//Get function parameters\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "const direction = eventsFunctionContext.getArgument(\"direction\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in condition: \"Gamepad stick pushed (axis)\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", "if (stick != \"LEFT\" && stick != \"RIGHT\") {\r", " console.error('Parameter stick in condition: \"Gamepad stick pushed (axis)\", is not valid, must be LEFT or RIGHT');\r", " return;\r", "}\r", "if (direction != \"UP\" && direction != \"DOWN\" && direction != \"LEFT\" && direction != \"RIGHT\" && direction != \"ANY\") {\r", - " console.error('Parameter deadzone in condition: \"Gamepad stick pushed (axis)\", is not valid, must be UP, DOWN, LEFT or RIGHT');\r", + " console.error('Parameter direction in condition: \"Gamepad stick pushed (axis)\", is not valid, must be UP, DOWN, LEFT or RIGHT');\r", " return;\r", "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) {\r", - " eventsFunctionContext.returnValue = false;\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", " return;\r", "}\r", - "\r", - "\r", - "//Define in onFirstSceneLoaded function\r", - "const getNormalizedAxisValue = gdjs._extensionController.getNormalizedAxisValue;\r", - "\r", - "switch (stick) {\r", - " case 'LEFT':\r", - " switch (direction) {\r", - " case 'LEFT':\r", - " if (getNormalizedAxisValue(gamepad.axes[0], playerId) < 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'RIGHT':\r", - " if (getNormalizedAxisValue(gamepad.axes[0], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'UP':\r", - " if (getNormalizedAxisValue(gamepad.axes[1], playerId) < 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'DOWN':\r", - " if (getNormalizedAxisValue(gamepad.axes[1], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'ANY':\r", - " if ( getNormalizedAxisValue(gamepad.axes[0], playerId) < 0\r", - " || getNormalizedAxisValue(gamepad.axes[0], playerId) > 0\r", - " || getNormalizedAxisValue(gamepad.axes[1], playerId) < 0 \r", - " || getNormalizedAxisValue(gamepad.axes[1], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " default:\r", - " console.error('The value Direction on stick Left on the condition: \"Gamepad stick pushed (axis)\" is not valid.');\r", - " eventsFunctionContext.returnValue = false;\r", - " break;\r", - " }\r", - " break;\r", - "\r", - " case 'RIGHT':\r", - " switch (direction) {\r", - " case 'LEFT':\r", - " if (getNormalizedAxisValue(gamepad.axes[2], playerId) < 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'RIGHT':\r", - " if (getNormalizedAxisValue(gamepad.axes[2], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'UP':\r", - " if (getNormalizedAxisValue(gamepad.axes[3], playerId) < 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'DOWN':\r", - " if (getNormalizedAxisValue(gamepad.axes[3], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'ANY':\r", - " if ( getNormalizedAxisValue(gamepad.axes[2], playerId) < 0\r", - " || getNormalizedAxisValue(gamepad.axes[2], playerId) > 0\r", - " || getNormalizedAxisValue(gamepad.axes[3], playerId) < 0 \r", - " || getNormalizedAxisValue(gamepad.axes[3], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " default:\r", - " console.error('The value Direction on stick Right on the condition: \"Gamepad stick pushed (axis)\" is not valid.');\r", - " eventsFunctionContext.returnValue = false;\r", - " break;\r", - " }\r", - " break;\r", - "\r", - " default:\r", - " console.error('The value Stick on the condition: \"Gamepad stick pushed (axis)\" is not valid.');\r", - " eventsFunctionContext.returnValue = false;\r", - " break;\r", - "}\r", - "\r", - "eventsFunctionContext.returnValue = false;\r", + "const axisValueX = stick === 'RIGHT' ? gamepad.axes[2] : gamepad.axes[0];\r", + "const axisValueY = stick === 'RIGHT' ? gamepad.axes[3] : gamepad.axes[1];\r", + "eventsFunctionContext.returnValue = gdjs._extensionController.isAxisPushed(playerId, direction, axisValueX, axisValueY);\r", "" ], "parameterObjects": "", @@ -10345,7 +10191,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -10365,24 +10211,24 @@ }, { "description": "Return the number of connected gamepads.", - "fullName": "Connected gamepads number", + "fullName": "Connected gamepads count", "functionType": "Expression", "name": "ConnectedGamepadsCount", "sentence": "", "events": [ - { - "type": "BuiltinCommonInstructions::Standard", - "conditions": [], - "actions": [] - }, { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "// Gamepads can be disconnected and become null, so we have to filter them.\r", - "eventsFunctionContext.returnValue = Object.keys(gamepads).filter(key => !!gamepads[key]).length;\r", + "const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", + "let connectedGamepadCount = 0;\r", + "for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", + " // Gamepads can be disconnected and become null\r", + " if (gamepads[playerId]) {\r", + " connectedGamepadCount++;\r", + " }\r", + "}\r", + "eventsFunctionContext.returnValue = connectedGamepadCount;\r", "" ], "parameterObjects": "", @@ -10406,22 +10252,13 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", - "//Get function parameter", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", - "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in string expression: \"Gamepad type\", is not valid number, must be between 0 and 4');", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", "eventsFunctionContext.returnValue = (gamepad && gamepad.id) ? gamepad.id : \"No information for player \" + (playerId + 1)", "" ], @@ -10436,7 +10273,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -10452,28 +10289,14 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", - "//Get function parameters", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();", - "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in condition: \"Gamepad type\", is not valid number, must be between 0 and 4.');", - " return;", - "}", - "if (controllerType === \"\") {", - " console.error('Parameter type in condition: \"Gamepad type\", is not a string.');", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", - "", "if (controllerType == \"XBOX\") {", " eventsFunctionContext.returnValue = gdjs._extensionController.isXbox(gamepad);", "} else {", @@ -10488,7 +10311,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -10509,17 +10332,10 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", - "//Get function parameter", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", - "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in condition: \"Gamepad connected\", is not valid number, must be between 0 and 4.');", - " return;", - "}", "", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", + "/** @type {Gamepad[]} */", + "const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];", "// If gamepad was disconnected it will be null (so this will return false)", "// If gamepad was never connected it will be undefined (so this will return false)", "eventsFunctionContext.returnValue = !!gamepads[playerId];" @@ -10532,7 +10348,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -10548,25 +10364,16 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "//Vibration work only on game in browser.", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", - "//Get function parameters", - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", "const duration = eventsFunctionContext.getArgument(\"Duration\") || 1;", - "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in action: \"Gamepad connected\", is not valid number, must be between 0 and 4.');", - " return;", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", + " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", - "if (gamepad && gamepad.vibrationActuator) {", + "//Vibration work only on game in browser.", + "if (gamepad.vibrationActuator) {", " gamepad.vibrationActuator.playEffect(\"dual-rumble\", {", " startDelay: 0,", " duration: duration * 1000,", @@ -10583,7 +10390,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -10604,35 +10411,19 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "//Vibration work only on game in browser.", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", - "//Get function parameters", - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", + "const { clamp } = gdjs.evtTools.common;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", "const duration = eventsFunctionContext.getArgument(\"Duration\") || 1;", - "const strongRumbleMagnitude = eventsFunctionContext.getArgument(\"StrongMagnitude\");", - "const weakRumbleMagnitude = eventsFunctionContext.getArgument(\"WeakMagnitude\");", - "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in action: \"Advanced gamepad vibration\", is not valid number, must be between 0 and 4.');", - " return;", - "}", - "if (weakRumbleMagnitude < 0 || weakRumbleMagnitude > 1) {", - " console.error('Parameter weakRumble identifier in action: \"Advanced gamepad vibration\", is not valid number, must be between 0 and 1.');", - " return;", - "}", - "if (strongRumbleMagnitude < 0 || strongRumbleMagnitude > 1) {", - " console.error('Parameter strongRumble identifier in action: \"Advanced gamepad vibration\", is not valid number, must be between 0 and 1.');", - " return;", + "const strongRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"StrongMagnitude\"), 0, 1);", + "const weakRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"WeakMagnitude\"), 0, 1);", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", + " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug the controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", - "if (gamepad && gamepad.vibrationActuator) {", + "//Vibration work only on game in browser.", + "if (gamepad.vibrationActuator) {", " gamepad.vibrationActuator.playEffect(\"dual-rumble\", {", " startDelay: 0,", " duration: duration * 1000,", @@ -10640,11 +10431,11 @@ " strongMagnitude: strongRumbleMagnitude", " });", "}", - "", - "gdjs._extensionController.players[playerId].rumble.duration = duration;", - "gdjs._extensionController.players[playerId].rumble.elapsedTime = 0;", - "gdjs._extensionController.players[playerId].rumble.weakMagnitude = weakRumbleMagnitude;", - "gdjs._extensionController.players[playerId].rumble.strongMagnitude = strongRumbleMagnitude;" + "const player = gdjs._extensionController.getPlayer(playerId)", + "player.rumble.duration = duration;", + "player.rumble.elapsedTime = 0;", + "player.rumble.weakMagnitude = weakRumbleMagnitude;", + "player.rumble.strongMagnitude = strongRumbleMagnitude;" ], "parameterObjects": "", "useStrict": true, @@ -10654,7 +10445,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -10685,39 +10476,22 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "//Vibration work only on game in browser.", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", - "//Get function parameters", - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", - "const elapsedTime = gdjs._extensionController.players[playerId].rumble.elapsedTime || 0;", - "const originalDuration = gdjs._extensionController.players[playerId].rumble.duration || 1;", - "const strongRumbleMagnitude = eventsFunctionContext.getArgument(\"StrongMagnitude\");", - "const weakRumbleMagnitude = eventsFunctionContext.getArgument(\"WeakMagnitude\");", - "", - "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in action: \"Change gamepad active vibration\", is not valid number, must be between 0 and 4.');", - " return;", - "}", - "if (weakRumbleMagnitude < 0 || weakRumbleMagnitude > 1) {", - " console.error('Parameter weakRumble identifier in action: \"Change gamepad active vibration\", is not valid number, must be between 0 and 1.');", - " return;", - "}", - "if (strongRumbleMagnitude < 0 || strongRumbleMagnitude > 1) {", - " console.error('Parameter strongRumble identifier in action: \"Change gamepad active vibration\", is not valid number, must be between 0 and 1.');", - " return;", + "const { clamp } = gdjs.evtTools.common;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", + "const player = gdjs._extensionController.getPlayer(playerId);", + "const elapsedTime = player.rumble.elapsedTime || 0;", + "const originalDuration = player.rumble.duration || 1;", + "const strongRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"StrongMagnitude\"), 0, 1);", + "const weakRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"WeakMagnitude\"), 0, 1);", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", + " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug the controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", "if (originalDuration - elapsedTime <= 0) return;", - "", - "if (gamepad && gamepad.vibrationActuator) {", + "//Vibration work only on game in browser.", + "if (gamepad.vibrationActuator) {", " gamepad.vibrationActuator.playEffect(\"dual-rumble\", {", " startDelay: 0,", " duration: 1000 * (originalDuration - elapsedTime),", @@ -10726,8 +10500,8 @@ " });", "}", "", - "gdjs._extensionController.players[playerId].rumble.weakMagnitude = weakRumbleMagnitude;", - "gdjs._extensionController.players[playerId].rumble.strongMagnitude = strongRumbleMagnitude;" + "player.rumble.weakMagnitude = weakRumbleMagnitude;", + "player.rumble.strongMagnitude = strongRumbleMagnitude;" ], "parameterObjects": "", "useStrict": true, @@ -10737,7 +10511,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -10768,54 +10542,10 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", - "//Get function parameters\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "\r", - "if (playerId < 0 || playerId > 4) {\r", - "\tconsole.error('Parameter gamepad identifier in condition: \"Any gamepad button released\", is not valid number, must be between 0 and 4.');\r", - "\treturn;\r", - "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", - "for (let buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) { //For each buttons on current frame.\r", - "\r", - "\tif (buttonId === undefined) {\r", - "\t\teventsFunctionContext.returnValue = false;\r", - "\t\treturn;\r", - "\t}\r", - "\r", - "\t//Get previous value or define value by default for the current button\r", - "\tgdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] || { pressed: false };\r", "\r", - "\t//Get state of the button at previous frame\r", - "\tconst previousStateButtonIsPressed = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed;\r", - "\r", - "\t//Get the state of the button on the current frame.\r", - "\tconst currentFrameStateButtonIsPressed = gamepad.buttons[buttonId].pressed;\r", - "\r", - "\t//When previousStateButtonIsPressed is true and actual button state is not pressed\r", - "\t//Player have release the button\r", - "\tif (previousStateButtonIsPressed === true && currentFrameStateButtonIsPressed === false) {\r", - "\t\tgdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed = true;\r", - "\t\teventsFunctionContext.returnValue = true;\r", - "\t\t//break;\r", - "\t\treturn;\r", - "\t} else {\r", - "\t\t//The player didn't released the button yet, the previous frame state is still true\r", - "\t\tgdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed = false;\r", - "\t\teventsFunctionContext.returnValue = false;\r", - "\t}\r", - "\r", - "\tif (currentFrameStateButtonIsPressed) gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", - "}\r", - "" + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const player = gdjs._extensionController.getPlayer(playerId)\r", + "eventsFunctionContext.returnValue = player.isAnyButtonReleased();" ], "parameterObjects": "", "useStrict": true, @@ -10825,7 +10555,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -10841,8 +10571,8 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;\r", - "eventsFunctionContext.returnValue = gdjs._extensionController.players[playerId].rumble.weakMagnitude;" + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).rumble.weakMagnitude;" ], "parameterObjects": "", "useStrict": true, @@ -10855,7 +10585,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -10871,8 +10601,8 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;\r", - "eventsFunctionContext.returnValue = gdjs._extensionController.players[playerId].rumble.strongMagnitude;" + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).rumble.strongMagnitude;" ], "parameterObjects": "", "useStrict": true, @@ -10885,7 +10615,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" } ],