Skip to content

Instantly share code, notes, and snippets.

@pablomayobre
Last active December 14, 2017 22:07
Show Gist options
  • Save pablomayobre/3684828f1196e87449d1937c0da816bb to your computer and use it in GitHub Desktop.
Save pablomayobre/3684828f1196e87449d1937c0da816bb to your computer and use it in GitHub Desktop.
Add NativeFileDialogs to LÖVE filesystem API (https://github.com/mlabbe/nativefiledialog)
enum DialogType
{
DIALOG_SAVE,
DIALOG_OPEN,
DIALOG_FOLDER,
DIALOG_MULTIPLE;
DIALOG_MAX_ENUM
};
#import <nfd.h>
void Filesystem::showDialog(DialogType type, const char *filter, const char *dir, std::vector<std::string> &items)
{
nfdresult_t status;
if (type == DIALOG_MULTIPLE)
{
nfdpathset_t files = nullptr;
status = NFD_OpenDialogMultiple(filter, initial, &files);
if (status != NFD_ERROR && files != nullptr)
{
size_t count = NFD_PathSet_GetCount(files);
for (size_t i = 0; i < count; ++i)
{
items.push_back(NFD_PathSet_GetPath(files, i));
allowMountingForPath(items.back())
}
NFD_PathSet_Free(set);
}
}
else
{
nfdchar_t* file = nullptr;
switch (type)
{
case DIALOG_FOLDER:
status = NFD_PickFolder(dir, &file);
case DIALOG_SAVE:
status = NFD_SaveDialog(filter, dir, &file);
case DIALOG_OPEN:
status = NFD_OpenDialog(filter, dir, &file);
default:
throw love::Exception("Invalid dialog type");
}
if (status != NFD_ERROR && file != nullptr)
{
items.push_back(file);
allowMountingForPath(file);
}
}
if (status == NFD_ERROR)
throw love::Exception(NFD_GetError());
}
bool Filesystem::getConstant(const char *in, DialogType &out)
{
return dialogTypes.find(in, out);
}
bool Filesystem::getConstant(DialogType in, const char *&out)
{
return dialogTypes.find(in, out);
}
std::vector<std::string> Filesystem::getConstants(DialogType)
{
return dialogTypes.getNames();
}
StringMap<Filesystem::DialogType, Filesystem::DIALOG_MAX_ENUM>::Entry Filesystem::dialogTypeEntries[] =
{
{ "save", DIALOG_SAVE },
{ "open", DIALOG_OPEN },
{ "folder", DIALOG_FOLDER },
{ "multiple", DIALOG_MULTIPLE }
};
StringMap<Filesystem::DialogType, Filesystem::DIALOG_MAX_ENUM> Filesystem::dialogTypes(Filesystem::dialogTypeEntries, sizeof(Filesystem::dialogTypeEntries));
@pablomayobre
Copy link
Author

pablomayobre commented Dec 14, 2017

Note that both the initial and filter arguments are optional.

"folder" dialogs ignore the filter argument altogether.

"multiple" is the only type of dialog that pushes more than one entry to the returned vector. The others return the single path of the file/folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment