StandaloneFileBrowser.mm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3. typedef void (*callbackFunc)(const char *);
  4. static callbackFunc asyncCallback;
  5. @interface StandaloneFileBrowser : NSObject<UIDocumentPickerDelegate>
  6. + (void) createOpenPanel:(NSString*)title
  7. directory:(NSString*)directory
  8. filters:(NSString*)filters
  9. multiselect:(BOOL)multiselect
  10. canChooseFiles:(BOOL)canChooseFiles
  11. canChooseFolders:(BOOL)canChooseFolders
  12. save:(BOOL)save;
  13. @end
  14. extern "C" {
  15. const char* DialogOpenFilePanel(const char* title,
  16. const char* directory,
  17. const char* filters,
  18. bool multiselect);
  19. void DialogOpenFilePanelAsync(const char* title,
  20. const char* directory,
  21. const char* filters,
  22. bool multiselect,
  23. callbackFunc cb);
  24. const char* DialogOpenFolderPanel(const char* title,
  25. const char* directory,
  26. bool multiselect);
  27. void DialogOpenFolderPanelAsync(const char* title,
  28. const char* directory,
  29. bool multiselect,
  30. callbackFunc cb);
  31. const char* DialogSaveFilePanel(const char* title,
  32. const char* directory,
  33. const char* defaultName,
  34. const char* filters);
  35. void DialogSaveFilePanelAsync(const char* title,
  36. const char* directory,
  37. const char* defaultName,
  38. const char* filters,
  39. callbackFunc cb);
  40. UIViewController* UnityGetGLViewController();
  41. }
  42. const char* DialogOpenFilePanel(const char* title,
  43. const char* directory,
  44. const char* filters,
  45. bool multiselect) {
  46. return 0;
  47. }
  48. void DialogOpenFilePanelAsync(const char* title,
  49. const char* directory,
  50. const char* filters,
  51. bool multiselect,
  52. callbackFunc cb) {
  53. asyncCallback = cb;
  54. [StandaloneFileBrowser createOpenPanel:[NSString stringWithUTF8String:title ?: ""]
  55. directory:[NSString stringWithUTF8String:directory ?: ""]
  56. filters:[NSString stringWithUTF8String:filters ?: ""]
  57. multiselect:multiselect
  58. canChooseFiles:YES
  59. canChooseFolders:NO
  60. save:NO];
  61. }
  62. const char* DialogOpenFolderPanel(const char* title,
  63. const char* directory,
  64. bool multiselect) {
  65. return 0;
  66. }
  67. void DialogOpenFolderPanelAsync(const char* title,
  68. const char* directory,
  69. bool multiselect,
  70. callbackFunc cb) {
  71. asyncCallback = cb;
  72. [StandaloneFileBrowser createOpenPanel:[NSString stringWithUTF8String:title ?: ""]
  73. directory:[NSString stringWithUTF8String:directory ?: ""]
  74. filters:@""
  75. multiselect:multiselect
  76. canChooseFiles:NO
  77. canChooseFolders:YES
  78. save:NO];
  79. }
  80. const char* DialogSaveFilePanel(const char* title,
  81. const char* directory,
  82. const char* defaultName,
  83. const char* filters) {
  84. return 0;
  85. }
  86. void DialogSaveFilePanelAsync(const char* title,
  87. const char* directory,
  88. const char* defaultName,
  89. const char* filters,
  90. callbackFunc cb) {
  91. asyncCallback = cb;
  92. [StandaloneFileBrowser createOpenPanel:[NSString stringWithUTF8String:title ?: ""]
  93. directory:[NSString stringWithUTF8String:directory ?: ""]
  94. filters:@""
  95. multiselect:NO
  96. canChooseFiles:YES
  97. canChooseFolders:NO
  98. save:YES];
  99. }
  100. @implementation StandaloneFileBrowser
  101. + (void) documentPicker:(UIDocumentPickerViewController *)controller
  102. didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls {
  103. NSString* pathsStr = @"";
  104. NSMutableArray* paths = [NSMutableArray arrayWithCapacity:[urls count]];
  105. for (int i = 0; i < [urls count]; i++) {
  106. NSURL* url = [urls objectAtIndex:i];
  107. [paths addObject:[url path]];
  108. }
  109. NSString* seperator = [NSString stringWithFormat:@"%c", 28];
  110. pathsStr = [paths componentsJoinedByString:seperator];
  111. if (asyncCallback) {
  112. asyncCallback([pathsStr UTF8String]);
  113. }
  114. }
  115. + (void) documentPicker:(UIDocumentPickerViewController *)controller
  116. didPickDocumentAtURL:(NSURL *)url {
  117. NSString* pathsStr = @"";
  118. NSMutableArray* paths = [NSMutableArray arrayWithCapacity:1];
  119. [paths addObject:[url path]];
  120. NSString* seperator = [NSString stringWithFormat:@"%c", 28];
  121. pathsStr = [paths componentsJoinedByString:seperator];
  122. if (asyncCallback) {
  123. asyncCallback([pathsStr UTF8String]);
  124. }
  125. }
  126. + (void) createOpenPanel:(NSString*)title
  127. directory:(NSString*)directory
  128. filters:(NSString*)filters
  129. multiselect:(BOOL)multiselect
  130. canChooseFiles:(BOOL)canChooseFiles
  131. canChooseFolders:(BOOL)canChooseFolders
  132. save:(BOOL)save
  133. {
  134. NSURL* url = [NSURL URLWithString:directory];
  135. NSMutableArray* fileTypes = [[NSMutableArray alloc] init];
  136. if (canChooseFiles) {
  137. [fileTypes addObject:@"public.item"];
  138. }
  139. if (canChooseFolders) {
  140. [fileTypes addObject:@"public.directory"];
  141. }
  142. UIDocumentPickerViewController *controller = [[UIDocumentPickerViewController alloc]
  143. initWithDocumentTypes:fileTypes
  144. inMode:save?UIDocumentPickerModeExportToService:UIDocumentPickerModeImport];
  145. [controller setTitle:title];
  146. [controller setDirectoryURL:url];
  147. [controller setAllowsMultipleSelection:multiselect];
  148. [controller setShouldShowFileExtensions:true];
  149. [controller setDelegate:self];
  150. [UnityGetGLViewController() presentViewController:controller animated:YES completion:nil];
  151. }
  152. @end