Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //HEADER
- UFUNCTION(BlueprintCallable)
- static bool GetPathFromExplorer(FString& FileName, FString WindowTitle = "Choose where to save the video file", FString DefaultFileName = "NewRecording", FString FileTypes = "Video Files (*.avi)|*.avi|All Files (*.*)|*.*|");
- //CPP
- bool UVRBPLibrary::GetPathFromExplorer(FString& Path, FString WindowTitle, FString DefaultFileName, FString FileTypes )
- {
- int defaultFileExtension = 0; // Use this to set which file extension should be selected by default
- /* Create dialog and set default values */
- FileDialog* dialog = new FileDialog();
- dialog->FilterIndex = defaultFileExtension;
- dialog->Title = WindowTitle.GetCharArray().GetData();
- dialog->Flags = dialog->Flags | OFN_NOCHANGEDIR | OFN_OVERWRITEPROMPT;
- dialog->FileName = DefaultFileName.GetCharArray().GetData(); // Error at this line, somehow crashes the entire thing.
- /* Make file type filters */
- FString firstExtension; // TODO: Use array
- bool hasExtension = false;
- std::wstring filter; // Creates a WString to support '\0'
- std::string filterString = std::string(TCHAR_TO_UTF8(*FileTypes)).c_str(); // Create string because FString is just obnoxious.
- for (char& c : filterString) // For each character in FileTypes FString
- {
- if (c == '.')
- {
- if (!hasExtension)
- {
- size_t found = filterString.find(c, 0 /*Needs to be position of c*/); // Gets file extensions up to 4 characters
- firstExtension = filterString.substr(found + 1, 3).c_str(); // Only gets the first one. TODO: Enable all file types.
- hasExtension = true;
- }
- }
- if (c == '|')
- {
- filter.push_back('\0'); // Add '\0' where '|' is
- }
- else
- {
- filter.push_back(c); // Copy the character over from WString to String
- }
- }
- dialog->Filter = filter.c_str(); // Set the default file type filter
- /* Set default extension */
- FString cleanedExtension = firstExtension; // If extension is 3 characters, remove ')'
- if (cleanedExtension.Contains(TEXT(")")))
- {
- cleanedExtension.RemoveAt(cleanedExtension.Find(TEXT(")")));
- }
- dialog->DefaultExtension = cleanedExtension.GetCharArray().GetData(); // Set the default extension
- /* Open the save dialog */
- if (dialog->ShowSaveFileDialog())
- {
- Path = dialog->FileName;
- Path = Path.Replace(_T("\\"), _T("/"));
- if (dialog != nullptr)
- {
- delete dialog;
- }
- return true;
- }
- return false;
- }
Add Comment
Please, Sign In to add comment