Last active
December 14, 2017 22:07
-
-
Save pablomayobre/3684828f1196e87449d1937c0da816bb to your computer and use it in GitHub Desktop.
Add NativeFileDialogs to LÖVE filesystem API (https://github.com/mlabbe/nativefiledialog)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that both the
initial
andfilter
arguments are optional."folder"
dialogs ignore thefilter
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