babylon.filesInput.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. module BABYLON {
  2. export class FilesInput {
  3. public static FilesToLoad: { [key: string]: File } = {};
  4. public onProcessFileCallback: (file: File, name: string, extension: string) => true = () => { return true; };
  5. private _engine: Engine;
  6. private _currentScene: Scene;
  7. private _sceneLoadedCallback: (sceneFile: File, scene: Scene) => void;
  8. private _progressCallback: (progress: SceneLoaderProgressEvent) => void;
  9. private _additionalRenderLoopLogicCallback: () => void;
  10. private _textureLoadingCallback: (remaining: number) => void;
  11. private _startingProcessingFilesCallback: (files?: File[]) => void;
  12. private _onReloadCallback: (sceneFile: File) => void;
  13. private _errorCallback: (sceneFile: File, scene: Scene, message: string) => void;
  14. private _elementToMonitor: HTMLElement;
  15. private _sceneFileToLoad: File;
  16. private _filesToLoad: File[];
  17. constructor(engine: Engine, scene: Scene, sceneLoadedCallback: (sceneFile: File, scene: Scene) => void, progressCallback: (progress: SceneLoaderProgressEvent) => void, additionalRenderLoopLogicCallback: () => void,
  18. textureLoadingCallback: (remaining: number) => void, startingProcessingFilesCallback: (files?: File[]) => void, onReloadCallback: (sceneFile: File) => void, errorCallback: (sceneFile: File, scene: Scene, message: string) => void) {
  19. this._engine = engine;
  20. this._currentScene = scene;
  21. this._sceneLoadedCallback = sceneLoadedCallback;
  22. this._progressCallback = progressCallback;
  23. this._additionalRenderLoopLogicCallback = additionalRenderLoopLogicCallback;
  24. this._textureLoadingCallback = textureLoadingCallback;
  25. this._startingProcessingFilesCallback = startingProcessingFilesCallback;
  26. this._onReloadCallback = onReloadCallback;
  27. this._errorCallback = errorCallback;
  28. }
  29. private _dragEnterHandler: (e: any) => void;
  30. private _dragOverHandler: (e: any) => void;
  31. private _dropHandler: (e: any) => void;
  32. public monitorElementForDragNDrop(elementToMonitor: HTMLElement): void {
  33. if (elementToMonitor) {
  34. this._elementToMonitor = elementToMonitor;
  35. this._dragEnterHandler = (e) => { this.drag(e); };
  36. this._dragOverHandler = (e) => { this.drag(e); };
  37. this._dropHandler = (e) => { this.drop(e); };
  38. this._elementToMonitor.addEventListener("dragenter", this._dragEnterHandler, false);
  39. this._elementToMonitor.addEventListener("dragover", this._dragOverHandler, false);
  40. this._elementToMonitor.addEventListener("drop", this._dropHandler, false);
  41. }
  42. }
  43. public dispose() {
  44. if (!this._elementToMonitor) {
  45. return;
  46. }
  47. this._elementToMonitor.removeEventListener("dragenter", this._dragEnterHandler);
  48. this._elementToMonitor.removeEventListener("dragover", this._dragOverHandler);
  49. this._elementToMonitor.removeEventListener("drop", this._dropHandler);
  50. }
  51. private renderFunction(): void {
  52. if (this._additionalRenderLoopLogicCallback) {
  53. this._additionalRenderLoopLogicCallback();
  54. }
  55. if (this._currentScene) {
  56. if (this._textureLoadingCallback) {
  57. var remaining = this._currentScene.getWaitingItemsCount();
  58. if (remaining > 0) {
  59. this._textureLoadingCallback(remaining);
  60. }
  61. }
  62. this._currentScene.render();
  63. }
  64. }
  65. private drag(e: DragEvent): void {
  66. e.stopPropagation();
  67. e.preventDefault();
  68. }
  69. private drop(eventDrop: DragEvent): void {
  70. eventDrop.stopPropagation();
  71. eventDrop.preventDefault();
  72. this.loadFiles(eventDrop);
  73. }
  74. private _traverseFolder(folder: any, files: Array<any>, remaining: { count: number }, callback: () => void) {
  75. var reader = folder.createReader();
  76. var relativePath = folder.fullPath.replace(/^\//, "").replace(/(.+?)\/?$/, "$1/");
  77. reader.readEntries((entries: any) => {
  78. remaining.count += entries.length;
  79. for (let entry of entries) {
  80. if (entry.isFile) {
  81. entry.file((file: any) => {
  82. file.correctName = relativePath + file.name;
  83. files.push(file);
  84. if (--remaining.count === 0) {
  85. callback();
  86. }
  87. });
  88. }
  89. else if (entry.isDirectory) {
  90. this._traverseFolder(entry, files, remaining, callback);
  91. }
  92. }
  93. if (--remaining.count) {
  94. callback();
  95. }
  96. });
  97. }
  98. private _processFiles(files: Array<any>): void {
  99. for (var i = 0; i < files.length; i++) {
  100. var name = files[i].correctName.toLowerCase();
  101. var extension = name.split('.').pop();
  102. if (!this.onProcessFileCallback(files[i], name, extension)) {
  103. continue;
  104. }
  105. if ((extension === "babylon" || extension === "stl" || extension === "obj" || extension === "gltf" || extension === "glb")
  106. && name.indexOf(".binary.babylon") === -1 && name.indexOf(".incremental.babylon") === -1) {
  107. this._sceneFileToLoad = files[i];
  108. }
  109. FilesInput.FilesToLoad[name] = files[i];
  110. }
  111. }
  112. public loadFiles(event: any): void {
  113. // Handling data transfer via drag'n'drop
  114. if (event && event.dataTransfer && event.dataTransfer.files) {
  115. this._filesToLoad = event.dataTransfer.files;
  116. }
  117. // Handling files from input files
  118. if (event && event.target && event.target.files) {
  119. this._filesToLoad = event.target.files;
  120. }
  121. if (!this._filesToLoad || this._filesToLoad.length === 0) {
  122. return;
  123. }
  124. if (this._startingProcessingFilesCallback) {
  125. this._startingProcessingFilesCallback(this._filesToLoad);
  126. }
  127. if (this._filesToLoad && this._filesToLoad.length > 0) {
  128. let files = new Array<File>();
  129. let folders = [];
  130. var items = event.dataTransfer ? event.dataTransfer.items : null;
  131. for (var i = 0; i < this._filesToLoad.length; i++) {
  132. let fileToLoad: any = this._filesToLoad[i];
  133. let name = fileToLoad.name.toLowerCase();
  134. let entry;
  135. fileToLoad.correctName = name;
  136. if (items) {
  137. let item = items[i];
  138. if (item.getAsEntry) {
  139. entry = item.getAsEntry();
  140. } else if (item.webkitGetAsEntry) {
  141. entry = item.webkitGetAsEntry();
  142. }
  143. }
  144. if (!entry) {
  145. files.push(fileToLoad);
  146. } else {
  147. if (entry.isDirectory) {
  148. folders.push(entry);
  149. } else {
  150. files.push(fileToLoad);
  151. }
  152. }
  153. }
  154. if (folders.length === 0) {
  155. this._processFiles(files);
  156. this._processReload();
  157. } else {
  158. var remaining = { count: folders.length };
  159. for (var folder of folders) {
  160. this._traverseFolder(folder, files, remaining, () => {
  161. this._processFiles(files);
  162. if (remaining.count === 0) {
  163. this._processReload();
  164. }
  165. });
  166. }
  167. }
  168. }
  169. }
  170. private _processReload() {
  171. if (this._onReloadCallback) {
  172. this._onReloadCallback(this._sceneFileToLoad);
  173. }
  174. else {
  175. this.reload();
  176. }
  177. }
  178. public reload() {
  179. // If a scene file has been provided
  180. if (this._sceneFileToLoad) {
  181. if (this._currentScene) {
  182. if (Tools.errorsCount > 0) {
  183. Tools.ClearLogCache();
  184. }
  185. this._engine.stopRenderLoop();
  186. }
  187. SceneLoader.LoadAsync("file:", this._sceneFileToLoad.name, this._engine, progress => {
  188. if (this._progressCallback) {
  189. this._progressCallback(progress);
  190. }
  191. }).then(scene => {
  192. if (this._currentScene) {
  193. this._currentScene.dispose();
  194. }
  195. this._currentScene = scene;
  196. if (this._sceneLoadedCallback) {
  197. this._sceneLoadedCallback(this._sceneFileToLoad, this._currentScene);
  198. }
  199. // Wait for textures and shaders to be ready
  200. this._currentScene.executeWhenReady(() => {
  201. this._engine.runRenderLoop(() => {
  202. this.renderFunction();
  203. });
  204. });
  205. }).catch(error => {
  206. if (this._errorCallback) {
  207. this._errorCallback(this._sceneFileToLoad, this._currentScene, error.message);
  208. }
  209. });
  210. }
  211. else {
  212. Tools.Error("Please provide a valid .babylon file.");
  213. }
  214. }
  215. }
  216. }