File dialogs in Love2D?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
sarako
Prole
Posts: 14
Joined: Sat Jan 24, 2015 3:12 pm

File dialogs in Love2D?

Post by sarako »

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!
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: File dialogs in Love2D?

Post by pgimeno »

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.
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: File dialogs in Love2D?

Post by togFox »

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
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: File dialogs in Love2D?

Post by zorg »

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 :3True 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.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: File dialogs in Love2D?

Post by milon »

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.

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

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.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
sarako
Prole
Posts: 14
Joined: Sat Jan 24, 2015 3:12 pm

Re: File dialogs in Love2D?

Post by sarako »

LoveFS looks perfect! It's even using LuaFrames, which is the GUI framework I went with. Thanks so much!
sarako
Prole
Posts: 14
Joined: Sat Jan 24, 2015 3:12 pm

Re: File dialogs in Love2D?

Post by sarako »

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.
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: File dialogs in Love2D?

Post by GVovkiv »

sarako wrote: Thu May 19, 2022 5:38 pm 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.
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.
sarako
Prole
Posts: 14
Joined: Sat Jan 24, 2015 3:12 pm

Re: File dialogs in Love2D?

Post by sarako »

Thanks for explaining!
sarako
Prole
Posts: 14
Joined: Sat Jan 24, 2015 3:12 pm

Re: File dialogs in Love2D?

Post by sarako »

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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests