Difference between revisions of "love.filedropped"

(Function)
(Examples)
 
(7 intermediate revisions by 5 users not shown)
Line 11: Line 11:
 
Nothing.
 
Nothing.
 
== Examples ==
 
== Examples ==
Read and print the file, drag-and-dropped to love window.
+
Read and print the file, drag-and-dropped to love window using [[(File):read]] and [[(File):getFilename]].
 
<source lang="lua">
 
<source lang="lua">
 
function love.filedropped(file)
 
function love.filedropped(file)
    local data = file:read()
+
file:open("r")
    print(data)
+
local data = file:read()
 +
print("Content of " .. file:getFilename() .. ' is')
 +
print(data)
 +
print("End of file")
 +
end
 +
</source>
 +
 
 +
If dropped file is a .png, use it to create a löve image using [[love.image]] and [[love.graphics]]
 +
<source lang="lua">
 +
function love.filedropped(file)
 +
filename = file:getFilename()
 +
ext = filename:match("%.%w+$")
 +
 
 +
if ext == ".png" then
 +
file:open("r")
 +
fileData = file:read("data")
 +
img = love.image.newImageData(fileData)
 +
img = love.graphics.newImage(img)
 +
end
 
end
 
end
 
</source>
 
</source>
Line 26: Line 44:
 
[[Category:Callbacks]]
 
[[Category:Callbacks]]
 
{{#set:Description=Callback function triggered when a file is dragged and dropped onto the window.}}
 
{{#set:Description=Callback function triggered when a file is dragged and dropped onto the window.}}
{{#set:Subcategory=General}}
+
{{#set:Subcategory=Window}}
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.filedropped}}
 
{{i18n|love.filedropped}}

Latest revision as of 04:54, 10 December 2022

Available since LÖVE 0.10.0
This function is not supported in earlier versions.

Callback function triggered when a file is dragged and dropped onto the window.

Function

Synopsis

love.filedropped( file )

Arguments

DroppedFile file
The unopened File object representing the file that was dropped.

Returns

Nothing.

Examples

Read and print the file, drag-and-dropped to love window using (File):read and (File):getFilename.

function love.filedropped(file)
	file:open("r")
	local data = file:read()
	print("Content of " .. file:getFilename() .. ' is')
	print(data)
	print("End of file")
end

If dropped file is a .png, use it to create a löve image using love.image and love.graphics

function love.filedropped(file)	
	filename = file:getFilename()
	ext = filename:match("%.%w+$")

	if ext == ".png" then
		file:open("r")
		fileData = file:read("data")
		img = love.image.newImageData(fileData)
		img = love.graphics.newImage(img)
	end
end

Notes

File:open must be called on the file before reading from or writing to it. File:getFilename will return the full platform-dependent path to the file.

See Also


Other Languages