Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New extension: File drop #1380

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions extensions/community/FileDrop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
{
"author": "",
"category": "User interface",
"extensionNamespace": "",
"fullName": "File drop",
"helpPath": "",
"iconUrl": "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0ibWRpLWZpbGUtZXllLW91dGxpbmUiIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0Ij48cGF0aCBkPSJNMTcsMThDMTcuNTYsMTggMTgsMTguNDQgMTgsMTlDMTgsMTkuNTYgMTcuNTYsMjAgMTcsMjBDMTYuNDQsMjAgMTYsMTkuNTYgMTYsMTlDMTYsMTguNDQgMTYuNDQsMTggMTcsMThNMTcsMTVDMTQuMjcsMTUgMTEuOTQsMTYuNjYgMTEsMTlDMTEuOTQsMjEuMzQgMTQuMjcsMjMgMTcsMjNDMTkuNzMsMjMgMjIuMDYsMjEuMzQgMjMsMTlDMjIuMDYsMTYuNjYgMTkuNzMsMTUgMTcsMTVNMTcsMjEuNUEyLjUsMi41IDAgMCwxIDE0LjUsMTlBMi41LDIuNSAwIDAsMSAxNywxNi41QTIuNSwyLjUgMCAwLDEgMTkuNSwxOUEyLjUsMi41IDAgMCwxIDE3LDIxLjVNOS4yNywyMEg2VjRIMTNWOUgxOFYxMy4wN0MxOC43LDEzLjE1IDE5LjM2LDEzLjMyIDIwLDEzLjU2VjhMMTQsMkg2QTIsMiAwIDAsMCA0LDRWMjBBMiwyIDAgMCwwIDYsMjJIMTAuNUMxMCwyMS40MSA5LjU5LDIwLjczIDkuMjcsMjBaIiAvPjwvc3ZnPg==",
"name": "FileDrop",
"previewIconUrl": "https://asset-resources.gdevelop.io/public-resources/Icons/874a229613921627ff0a1d3b943a84eb9795cbde9f5b14e576c4eb29822c9d24_file-eye-outline.svg",
"shortDescription": "Adds the ability to read the content, name, path, and size of a file dropped into the game's window.",
"version": "1.0.0",
"description": [
"Some examples why you might find this extension helpful:",
"",
"- Let players load text into your game with just dragging a TXT file into the window",
"",
"- Let players load up a level from a JSON file",
"",
"- If you have a multiplayer game, or any game where players can create their own profile picture or upload anything, load the image from a file that contains the base64 image. You can use other extensions, or create your own way to handle base64",
"",
"- You can use basically any format for the file(s) your game will support, as long as it's readable. For example, if you have a simple text that is readable from a \"TXT\" file (e.g.: \"text123.txt\"), but you want to use your own file format, let's say \"MYGAME\" (e.g.: text123.mygame), it will work if the content remains string. Same for everything else, like a level data (level123.json -> level123.mygame) as long as the content can be converted from JSON to variable(s)",
"",
"With this said, it's completely up to your imagination how you will use this extension."
],
"tags": [],
"authorIds": [],
"dependencies": [],
"globalVariables": [],
"sceneVariables": [],
"eventsFunctions": [
{
"description": "Listens if a file is being dropped into the window and returns its path into the scene variable \"_fileDropPath\".",
"fullName": "Listen and return path",
"functionType": "Action",
"name": "ListenAndReturnPath",
"sentence": "Listen for a file drop and return its path",
"events": [
{
"type": "BuiltinCommonInstructions::JsCode",
"inlineCode": [
"window.addEventListener('dragover', (event) => {",
" event.preventDefault(); ",
"});",
"",
"window.addEventListener('drop', (event) => {",
" event.preventDefault();",
"",
" const files = event.dataTransfer.files;",
" if (files.length > 0) {",
" const file = files[0];",
"",
" // Attempt to get the full path (might be restricted by browser security)",
" const filePath = file.path || file.name; // Fallback to file name if path is not available",
"",
" console.log(\"Dropped file path:\", filePath);",
"",
" // Store the file path in the scene variable \"_fileDropPath\"",
" runtimeScene.getVariables().get(\"_fileDropPath\").setString(filePath); ",
" }",
"});"
],
"parameterObjects": "",
"useStrict": true,
"eventsSheetExpanded": false
}
],
"parameters": [],
"objectGroups": []
},
{
"description": "Listens if a file is being dropped into the window and returns its name into the scene variable \"_fileDropName\".",
"fullName": "Listen and return name",
"functionType": "Action",
"name": "ListenAndReturnName",
"sentence": "Listen for a file drop and return its name",
"events": [
{
"type": "BuiltinCommonInstructions::JsCode",
"inlineCode": [
"window.addEventListener('dragover', (event) => {",
" event.preventDefault(); ",
"});",
"",
"window.addEventListener('drop', (event) => {",
" event.preventDefault();",
"",
" const files = event.dataTransfer.files;",
" if (files.length > 0) {",
" const file = files[0];",
"",
" const fileName = file.name; // Get only the file name",
"",
" console.log(\"Dropped file name:\", fileName);",
"",
" // Store the file name in the scene variable \"_fileDropName\"",
" runtimeScene.getVariables().get(\"_fileDropName\").setString(fileName); ",
" }",
"});"
],
"parameterObjects": "",
"useStrict": true,
"eventsSheetExpanded": false
}
],
"parameters": [],
"objectGroups": []
},
{
"description": "Listens if a file is being dropped into the window and returns its size in bytes into the scene variable \"_fileDropSize\".",
"fullName": "Listen and return size",
"functionType": "Action",
"name": "ListenAndReturnSize",
"sentence": "Listen for a file drop and return its size",
"events": [
{
"type": "BuiltinCommonInstructions::JsCode",
"inlineCode": [
"window.addEventListener('dragover', (event) => {",
" event.preventDefault(); ",
"});",
"",
"window.addEventListener('drop', (event) => {",
" event.preventDefault();",
"",
" const files = event.dataTransfer.files;",
" if (files.length > 0) {",
" const file = files[0];",
"",
" // Get the file size in bytes",
" const fileSize = file.size;",
"",
" console.log(\"File size (bytes):\", fileSize);",
"",
" // Store the file size in the scene variable \"_fileDropSize\"",
" runtimeScene.getVariables().get(\"_fileDropSize\").setNumber(fileSize); ",
" }",
"});"
],
"parameterObjects": "",
"useStrict": true,
"eventsSheetExpanded": false
}
],
"parameters": [],
"objectGroups": []
},
{
"description": "Listens if a file is being dropped into the window and returns its content as text into the scene variable \"_fileDropContent\".",
"fullName": "Listen and return content",
"functionType": "Action",
"name": "ListenAndReturnContent",
"sentence": "Listen for a file drop and return its content",
"events": [
{
"type": "BuiltinCommonInstructions::JsCode",
"inlineCode": [
"window.addEventListener('dragover', (event) => {",
" event.preventDefault(); ",
"});",
"",
"window.addEventListener('drop', (event) => {",
" event.preventDefault();",
"",
" const files = event.dataTransfer.files;",
" if (files.length > 0) {",
" const file = files[0];",
"",
" const reader = new FileReader();",
"",
" reader.onload = (e) => {",
" const fileContent = e.target.result;",
"",
" console.log(\"File content:\", fileContent);",
"",
" // Store the file content in the scene variable \"_fileDropContent\"",
" runtimeScene.getVariables().get(\"_fileDropContent\").setString(fileContent); ",
" };",
"",
" // Read the file as text",
" reader.readAsText(file);",
" }",
"});"
],
"parameterObjects": "",
"useStrict": true,
"eventsSheetExpanded": false
}
],
"parameters": [],
"objectGroups": []
}
],
"eventsBasedBehaviors": [],
"eventsBasedObjects": []
}