File dialogs in Love2D?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
File dialogs in Love2D?
I'm working on a little modding tool in Love2D, and I'd like to have a way for the user to open/save files with a system file dialog. Is there a library or anything to support that? Thanks!
Re: File dialogs in Love2D?
There are two approaches that I know of. One is https://github.com/linux-man/lovefs which has libraries to access the filesystem, and interfaces for different Löve GUI libraries to show the dialogs.
Another one is this: viewtopic.php?f=4&t=82442 which uses FFI to call the native file dialog of each operating system, but I think it only supports Linux (GTK+) and Windows.
It's likely that either of those will need some updates, as they were originally designed for much older versions of Löve.
Another one is this: viewtopic.php?f=4&t=82442 which uses FFI to call the native file dialog of each operating system, but I think it only supports Linux (GTK+) and Windows.
It's likely that either of those will need some updates, as they were originally designed for much older versions of Löve.
Re: File dialogs in Love2D?
SLAB does this but might be a bit bloated for what you need.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: File dialogs in Love2D?
You can also use NativeFS (https://github.com/EngineerSmith/nativefs) and code your own file dialog system on top of that.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: File dialogs in Love2D?
You can use https://love2d.org/wiki/love.window.showMessageBox for user interaction, and build the file/folder list however you like. Perhaps NativeFS that zorg linked, or you can roll your own solution if you're feeling brave.
Here's a thing that I had sitting around; use it if you like with no attribution required.
Be advised that it works on Linux & Windows, fails on Wine, and is untested on Mac. Mac & other 'nix platforms should work, but I have no way to test that.
Here's a thing that I had sitting around; use it if you like with no attribution required.
Code: Select all
local system = {}
system.path = love.filesystem.getWorkingDirectory().."/test.txt"
system.list_files = "ls "
system.list_dirs_prefix = "-d -N "
system.list_dirs_suffix = "*/"
if jit.os == "Windows" then
system.list_files = "dir /b "
system.list_dirs_prefix = "/ad "
system.list_dirs_suffix = ""
end
function listFiles(path, dirOnly)
local command = system.list_files
if dirOnly then
command = command..system.list_dirs_prefix..path..system.list_dirs_suffix
else
command = command..path
end
print("> "..command, (dirOnly and "(dirOnly)" or "(everything)")) -- testing feedback
local handle = io.popen(command)
local output = handle:read("*a")
handle:close()
return output
end
function strFindLast(str, phrase)
local len = phrase:len()
local i = str:len() - len + 1
while i > 0 do
if str:sub(i, i + len - 1) == phrase then return i end
i = i - 1
end
end
function isPath(path)
local parentDir = path:sub(1, strFindLast(path, "/"))
parentDir = listFiles(parentDir, true)
return parentDir:find(path)
end
---- exists() and isdir() functions ----
-- modified from
-- https://stackoverflow.com/questions/1340230/check-if-directory-exists-in-lua
--- Check if a file or directory exists in this path
function exists(file)
local ok, err, code = os.rename(file, file)
if not ok then
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
--- Check if path is a directory
function isDir(path)
-- "/" works on Linux and Windows, but fails on Wine (everything succeeds if the file/path exists)
--~ if path == cmd.root then return true end
return exists(path.."/") --or path == cmd.root
end
--------
local isPathStr = tostring(isPath(system.path))
local isDirStr = tostring(isDir(system.path))
local listFilesStr = listFiles(system.path)
function love.draw()
love.graphics.print("Checking: " .. system.path, 10, 10)
love.graphics.print("Is that a directory? (isPath) "..isPathStr, 10, 30)
love.graphics.print("Is that a directory? (isDir) "..isDirStr, 10, 45)
end
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Re: File dialogs in Love2D?
LoveFS looks perfect! It's even using LuaFrames, which is the GUI framework I went with. Thanks so much!
Re: File dialogs in Love2D?
So does Love2D prevent you from accessing files at all outside the folder the program is in? Trying to call getInfo on files based on an absolute filepath ("C:\...") return nil.
Re: File dialogs in Love2D?
Love uses sandboxed filesystem.
You can only read/write data in AppData folder (or other OS's AppData equivalent)
And you can only read data from source folder (or from fused archive).
I don't know if lua's io library also sandboxed, but love's filesystem works in that way.
So as zorg mentioned, you need use https://github.com/EngineerSmith/nativefs library to get data outside of sandbox.
Re: File dialogs in Love2D?
Thanks for explaining!
Re: File dialogs in Love2D?
Sadly nativefs and lovefs don't play well together. I think it's due to conflicting ffi.cdef's. Poking around to see if I can get them to work together, otherwise I'll use them as inspiration for my own file load/save dialog.
Who is online
Users browsing this forum: Ahrefs [Bot] and 6 guests