Guest User

Untitled

a guest
Jun 8th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. //HEADER
  2.  
  3.     UFUNCTION(BlueprintCallable)
  4.         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 (*.*)|*.*|");
  5.  
  6. //CPP
  7.  
  8. bool UVRBPLibrary::GetPathFromExplorer(FString& Path, FString WindowTitle, FString DefaultFileName, FString FileTypes )
  9. {
  10.     int defaultFileExtension = 0;                                               // Use this to set which file extension should be selected by default
  11.  
  12.     /* Create dialog and set default values */
  13.     FileDialog* dialog = new FileDialog();
  14.     dialog->FilterIndex = defaultFileExtension;
  15.     dialog->Title = WindowTitle.GetCharArray().GetData();
  16.     dialog->Flags = dialog->Flags | OFN_NOCHANGEDIR | OFN_OVERWRITEPROMPT;
  17.    
  18.     dialog->FileName = DefaultFileName.GetCharArray().GetData();                // Error at this  line, somehow crashes the entire thing.
  19.  
  20.  
  21.     /* Make file type filters */
  22.     FString firstExtension;                                                     // TODO: Use array
  23.     bool hasExtension = false;
  24.     std::wstring filter;                                                        // Creates a WString to support '\0'
  25.     std::string filterString = std::string(TCHAR_TO_UTF8(*FileTypes)).c_str();  // Create string because FString is just obnoxious.
  26.     for (char& c : filterString)                                                // For each character in FileTypes FString
  27.     {
  28.         if (c == '.')
  29.         {
  30.             if (!hasExtension)
  31.             {
  32.                 size_t found = filterString.find(c, 0 /*Needs to be position of c*/);       // Gets file extensions up to 4 characters
  33.                 firstExtension = filterString.substr(found + 1, 3).c_str();                 // Only gets the first one. TODO: Enable all file types.
  34.                 hasExtension = true;
  35.             }
  36.         }
  37.  
  38.         if (c == '|')
  39.         {
  40.             filter.push_back('\0');                                             // Add '\0' where '|' is
  41.         }
  42.  
  43.         else
  44.         {
  45.             filter.push_back(c);                                                // Copy the character over from WString to String
  46.         }
  47.     }
  48.     dialog->Filter = filter.c_str();                                            // Set the default file type filter
  49.  
  50.  
  51.     /* Set default extension */
  52.     FString cleanedExtension = firstExtension;                                  // If extension is 3 characters, remove ')'
  53.     if (cleanedExtension.Contains(TEXT(")")))
  54.     {
  55.         cleanedExtension.RemoveAt(cleanedExtension.Find(TEXT(")")));
  56.     }
  57.     dialog->DefaultExtension = cleanedExtension.GetCharArray().GetData();       // Set the default extension
  58.  
  59.  
  60.  
  61.     /* Open the save dialog */
  62.     if (dialog->ShowSaveFileDialog())
  63.     {
  64.         Path = dialog->FileName;
  65.         Path = Path.Replace(_T("\\"), _T("/"));
  66.  
  67.         if (dialog != nullptr)
  68.         {
  69.             delete dialog;
  70.         }
  71.  
  72.         return true;
  73.     }
  74.  
  75.  
  76.     return false;
  77. }
Add Comment
Please, Sign In to add comment