GUI: add multi-selection capability to file dialog

This commit is contained in:
tildearrow 2022-07-15 02:23:16 -05:00
parent d085f76c7f
commit 666b0d581a
5 changed files with 126 additions and 31 deletions

View File

@ -260,6 +260,20 @@ void FurnaceGUI::drawDebug() {
ImGui::Unindent();
ImGui::TreePop();
}
if (ImGui::TreeNode("File Selection Test")) {
if (ImGui::Button("Test Open")) {
openFileDialog(GUI_FILE_TEST_OPEN);
}
ImGui::SameLine();
if (ImGui::Button("Test Open Multi")) {
openFileDialog(GUI_FILE_TEST_OPEN_MULTI);
}
ImGui::SameLine();
if (ImGui::Button("Test Save")) {
openFileDialog(GUI_FILE_TEST_SAVE);
}
ImGui::TreePop();
}
if (ImGui::TreeNode("Playground")) {
if (pgSys<0 || pgSys>=e->song.systemLen) pgSys=0;
if (ImGui::BeginCombo("System",fmt::sprintf("%d. %s",pgSys+1,e->getSystemName(e->song.system[pgSys])).c_str())) {

View File

@ -10,13 +10,14 @@
#ifdef USE_NFD
struct NFDState {
bool isSave;
bool isSave, allowMultiple;
String header;
std::vector<String> filter;
String path;
FileDialogSelectCallback clickCallback;
NFDState(bool save, String h, std::vector<String> filt, String pa, FileDialogSelectCallback cc):
NFDState(bool save, String h, std::vector<String> filt, String pa, FileDialogSelectCallback cc, bool multi):
isSave(save),
allowMultiple(multi),
header(h),
filter(filt),
path(pa),
@ -61,7 +62,7 @@ void _nfdThread(const NFDState state, std::atomic<bool>* ok, String* result, boo
}
#endif
bool FurnaceGUIFileDialog::openLoad(String header, std::vector<String> filter, const char* noSysFilter, String path, double dpiScale, FileDialogSelectCallback clickCallback) {
bool FurnaceGUIFileDialog::openLoad(String header, std::vector<String> filter, const char* noSysFilter, String path, double dpiScale, FileDialogSelectCallback clickCallback, bool allowMultiple) {
if (opened) return false;
saving=false;
curPath=path;
@ -70,18 +71,18 @@ bool FurnaceGUIFileDialog::openLoad(String header, std::vector<String> filter, c
#ifdef USE_NFD
dialogOK=false;
#ifdef NFD_NON_THREADED
_nfdThread(NFDState(false,header,filter,path,clickCallback),&dialogOK,&nfdResult,&hasError);
_nfdThread(NFDState(false,header,filter,path,clickCallback,allowMultiple),&dialogOK,&nfdResult,&hasError);
#else
dialogO=new std::thread(_nfdThread,NFDState(false,header,filter,path,clickCallback),&dialogOK,&nfdResult,&hasError);
dialogO=new std::thread(_nfdThread,NFDState(false,header,filter,path,clickCallback,allowMultiple),&dialogOK,&nfdResult,&hasError);
#endif
#else
dialogO=new pfd::open_file(header,path,filter);
dialogO=new pfd::open_file(header,path,filter,allowMultiple?(pfd::opt::multiselect):(pfd::opt::none));
hasError=!pfd::settings::available();
#endif
} else {
hasError=false;
ImGuiFileDialog::Instance()->DpiScale=dpiScale;
ImGuiFileDialog::Instance()->OpenModal("FileDialog",header,noSysFilter,path,1,nullptr,0,clickCallback);
ImGuiFileDialog::Instance()->OpenModal("FileDialog",header,noSysFilter,path,allowMultiple?999:1,nullptr,0,clickCallback);
}
opened=true;
return true;
@ -96,9 +97,9 @@ bool FurnaceGUIFileDialog::openSave(String header, std::vector<String> filter, c
#ifdef USE_NFD
dialogOK=false;
#ifdef NFD_NON_THREADED
_nfdThread(NFDState(true,header,filter,path,NULL),&dialogOK,&nfdResult,&hasError);
_nfdThread(NFDState(true,header,filter,path,NULL,false),&dialogOK,&nfdResult,&hasError);
#else
dialogS=new std::thread(_nfdThread,NFDState(true,header,filter,path,NULL),&dialogOK,&nfdResult,&hasError);
dialogS=new std::thread(_nfdThread,NFDState(true,header,filter,path,NULL,false),&dialogOK,&nfdResult,&hasError);
#endif
#else
dialogS=new pfd::save_file(header,path,filter);
@ -115,7 +116,7 @@ bool FurnaceGUIFileDialog::openSave(String header, std::vector<String> filter, c
bool FurnaceGUIFileDialog::accepted() {
if (sysDialog) {
return (fileName!="");
return (!fileName.empty());
} else {
return ImGuiFileDialog::Instance()->IsOk();
}
@ -153,10 +154,15 @@ bool FurnaceGUIFileDialog::render(const ImVec2& min, const ImVec2& max) {
if (sysDialog) {
#ifdef USE_NFD
if (dialogOK) {
fileName=nfdResult;
size_t dsPos=fileName.rfind(DIR_SEPARATOR);
if (dsPos!=String::npos) curPath=fileName.substr(0,dsPos);
logD("returning %s",fileName.c_str());
fileName.clear();
fileName.push_back(nfdResult);
if (!fileName.empty()) {
size_t dsPos=fileName[0].rfind(DIR_SEPARATOR);
if (dsPos!=String::npos) curPath=fileName[0].substr(0,dsPos);
}
for (String& i: fileName) {
logD("- returning %s",i);
}
dialogOK=false;
return true;
}
@ -165,10 +171,11 @@ bool FurnaceGUIFileDialog::render(const ImVec2& min, const ImVec2& max) {
if (saving) {
if (dialogS!=NULL) {
if (dialogS->ready(0)) {
fileName=dialogS->result();
size_t dsPos=fileName.rfind(DIR_SEPARATOR);
if (dsPos!=String::npos) curPath=fileName.substr(0,dsPos);
logD("returning %s",fileName.c_str());
fileName.clear();
fileName.push_back(dialogS->result());
size_t dsPos=fileName[0].rfind(DIR_SEPARATOR);
if (dsPos!=String::npos) curPath=fileName[0].substr(0,dsPos);
logD("returning %s",fileName[0]);
return true;
}
}
@ -176,13 +183,19 @@ bool FurnaceGUIFileDialog::render(const ImVec2& min, const ImVec2& max) {
if (dialogO!=NULL) {
if (dialogO->ready(0)) {
if (dialogO->result().empty()) {
fileName="";
fileName.clear();
logD("returning nothing");
} else {
fileName=dialogO->result()[0];
size_t dsPos=fileName.rfind(DIR_SEPARATOR);
if (dsPos!=String::npos) curPath=fileName.substr(0,dsPos);
logD("returning %s",fileName.c_str());
fileName=dialogO->result();
if (fileName.empty()) {
// don't touch
} else {
size_t dsPos=fileName[0].rfind(DIR_SEPARATOR);
if (dsPos!=String::npos) curPath=fileName[0].substr(0,dsPos);
for (String& i: fileName) {
logD("- returning %s",i);
}
}
}
return true;
}
@ -217,10 +230,12 @@ String FurnaceGUIFileDialog::getPath() {
}
}
String FurnaceGUIFileDialog::getFileName() {
std::vector<String>& FurnaceGUIFileDialog::getFileName() {
if (sysDialog) {
return fileName;
} else {
return ImGuiFileDialog::Instance()->GetFilePathName();
fileName.clear();
fileName.push_back(ImGuiFileDialog::Instance()->GetFilePathName());
return fileName;
}
}

View File

@ -30,7 +30,7 @@ class FurnaceGUIFileDialog {
bool saving;
bool hasError;
String curPath;
String fileName;
std::vector<String> fileName;
#ifdef USE_NFD
std::thread* dialogO;
std::thread* dialogS;
@ -41,7 +41,7 @@ class FurnaceGUIFileDialog {
pfd::save_file* dialogS;
#endif
public:
bool openLoad(String header, std::vector<String> filter, const char* noSysFilter, String path, double dpiScale, FileDialogSelectCallback clickCallback=NULL);
bool openLoad(String header, std::vector<String> filter, const char* noSysFilter, String path, double dpiScale, FileDialogSelectCallback clickCallback=NULL, bool allowMultiple=false);
bool openSave(String header, std::vector<String> filter, const char* noSysFilter, String path, double dpiScale);
bool accepted();
void close();
@ -49,7 +49,7 @@ class FurnaceGUIFileDialog {
bool isOpen();
bool isError();
String getPath();
String getFileName();
std::vector<String>& getFileName();
explicit FurnaceGUIFileDialog(bool system):
sysDialog(system),
opened(false),

View File

@ -1495,6 +1495,43 @@ void FurnaceGUI::openFileDialog(FurnaceGUIFileDialogs type) {
dpiScale
);
break;
case GUI_FILE_TEST_OPEN:
if (!dirExists(workingDirTest)) workingDirTest=getHomeDir();
hasOpened=fileDialog->openLoad(
"Open Test",
{"compatible files", "*.fur *.dmf *.mod",
"another option", "*.wav *.ttf",
"all files", ".*"},
"compatible files{.fur,.dmf,.mod},another option{.wav,.ttf},.*",
workingDirTest,
dpiScale
);
break;
case GUI_FILE_TEST_OPEN_MULTI:
if (!dirExists(workingDirTest)) workingDirTest=getHomeDir();
hasOpened=fileDialog->openLoad(
"Open Test (Multi)",
{"compatible files", "*.fur *.dmf *.mod",
"another option", "*.wav *.ttf",
"all files", ".*"},
"compatible files{.fur,.dmf,.mod},another option{.wav,.ttf},.*",
workingDirTest,
dpiScale,
NULL,
true
);
break;
case GUI_FILE_TEST_SAVE:
if (!dirExists(workingDirTest)) workingDirTest=getHomeDir();
hasOpened=fileDialog->openSave(
"Save Test",
{"Furnace song", "*.fur",
"DefleMask module", "*.dmf"},
"Furnace song{.fur},DefleMask module{.dmf}",
workingDirTest,
dpiScale
);
break;
}
if (hasOpened) curFileDialog=type;
//ImGui::GetIO().ConfigFlags|=ImGuiConfigFlags_NavEnableKeyboard;
@ -3220,6 +3257,11 @@ bool FurnaceGUI::loop() {
case GUI_FILE_MU5_ROM_OPEN:
workingDirROM=fileDialog->getPath()+DIR_SEPARATOR_STR;
break;
case GUI_FILE_TEST_OPEN:
case GUI_FILE_TEST_OPEN_MULTI:
case GUI_FILE_TEST_SAVE:
workingDirTest=fileDialog->getPath()+DIR_SEPARATOR_STR;
break;
}
if (fileDialog->isError()) {
#if defined(_WIN32) || defined(__APPLE__)
@ -3229,7 +3271,11 @@ bool FurnaceGUI::loop() {
#endif
}
if (fileDialog->accepted()) {
fileName=fileDialog->getFileName();
if (fileDialog->getFileName().empty()) {
fileName="";
} else {
fileName=fileDialog->getFileName()[0];
}
if (fileName!="") {
if (curFileDialog==GUI_FILE_SAVE) {
// we can't tell whether the user chose .dmf or .fur in the system file picker
@ -3468,6 +3514,20 @@ bool FurnaceGUI::loop() {
case GUI_FILE_MU5_ROM_OPEN:
settings.mu5Path=copyOfName;
break;
case GUI_FILE_TEST_OPEN:
showWarning(fmt::sprintf("You opened: %s",copyOfName),GUI_WARN_GENERIC);
break;
case GUI_FILE_TEST_OPEN_MULTI: {
String msg="You opened:";
for (String i: fileDialog->getFileName()) {
msg+=fmt::sprintf("\n- %s",i);
}
showWarning(msg,GUI_WARN_GENERIC);
break;
}
case GUI_FILE_TEST_SAVE:
showWarning(fmt::sprintf("You saved: %s",copyOfName),GUI_WARN_GENERIC);
break;
}
curFileDialog=GUI_FILE_OPEN;
}
@ -4018,6 +4078,7 @@ bool FurnaceGUI::init() {
workingDirColors=e->getConfString("lastDirColors",workingDir);
workingDirKeybinds=e->getConfString("lastDirKeybinds",workingDir);
workingDirLayout=e->getConfString("lastDirLayout",workingDir);
workingDirTest=e->getConfString("lastDirTest",workingDir);
editControlsOpen=e->getConfBool("editControlsOpen",true);
ordersOpen=e->getConfBool("ordersOpen",true);
@ -4255,6 +4316,7 @@ bool FurnaceGUI::finish() {
e->setConf("lastDirColors",workingDirColors);
e->setConf("lastDirKeybinds",workingDirKeybinds);
e->setConf("lastDirLayout",workingDirLayout);
e->setConf("lastDirTest",workingDirTest);
// commit last open windows
e->setConf("editControlsOpen",editControlsOpen);

View File

@ -277,7 +277,11 @@ enum FurnaceGUIFileDialogs {
GUI_FILE_EXPORT_LAYOUT,
GUI_FILE_YRW801_ROM_OPEN,
GUI_FILE_TG100_ROM_OPEN,
GUI_FILE_MU5_ROM_OPEN
GUI_FILE_MU5_ROM_OPEN,
GUI_FILE_TEST_OPEN,
GUI_FILE_TEST_OPEN_MULTI,
GUI_FILE_TEST_SAVE
};
enum FurnaceGUIWarnings {
@ -943,7 +947,7 @@ class FurnaceGUI {
bool updateSampleTex;
String workingDir, fileName, clipboard, warnString, errorString, lastError, curFileName, nextFile;
String workingDirSong, workingDirIns, workingDirWave, workingDirSample, workingDirAudioExport, workingDirVGMExport, workingDirFont, workingDirColors, workingDirKeybinds, workingDirLayout, workingDirROM;
String workingDirSong, workingDirIns, workingDirWave, workingDirSample, workingDirAudioExport, workingDirVGMExport, workingDirFont, workingDirColors, workingDirKeybinds, workingDirLayout, workingDirROM, workingDirTest;
String mmlString[32];
String mmlStringW;