babylon.filesInput.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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: () => 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: () => 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. else {
  110. FilesInput.FilesToLoad[name] = files[i];
  111. }
  112. }
  113. }
  114. public loadFiles(event: any): void {
  115. if (this._startingProcessingFilesCallback) this._startingProcessingFilesCallback();
  116. // Handling data transfer via drag'n'drop
  117. if (event && event.dataTransfer && event.dataTransfer.files) {
  118. this._filesToLoad = event.dataTransfer.files;
  119. }
  120. // Handling files from input files
  121. if (event && event.target && event.target.files) {
  122. this._filesToLoad = event.target.files;
  123. }
  124. if (this._filesToLoad && this._filesToLoad.length > 0) {
  125. let files = new Array<File>();
  126. let folders = [];
  127. var items = event.dataTransfer ? event.dataTransfer.items : null;
  128. for (var i = 0; i < this._filesToLoad.length; i++) {
  129. let fileToLoad: any = this._filesToLoad[i];
  130. let name = fileToLoad.name.toLowerCase();
  131. let entry;
  132. fileToLoad.correctName = name;
  133. if (items) {
  134. let item = items[i];
  135. if (item.getAsEntry) {
  136. entry = item.getAsEntry();
  137. } else if (item.webkitGetAsEntry) {
  138. entry = item.webkitGetAsEntry();
  139. }
  140. }
  141. if (!entry) {
  142. files.push(fileToLoad);
  143. } else {
  144. if (entry.isDirectory) {
  145. folders.push(entry);
  146. } else {
  147. files.push(fileToLoad);
  148. }
  149. }
  150. }
  151. if (folders.length === 0) {
  152. this._processFiles(files);
  153. this._processReload();
  154. } else {
  155. var remaining = { count: folders.length };
  156. for (var folder of folders) {
  157. this._traverseFolder(folder, files, remaining, () => {
  158. this._processFiles(files);
  159. if (remaining.count === 0) {
  160. this._processReload();
  161. }
  162. });
  163. }
  164. }
  165. }
  166. }
  167. private _processReload() {
  168. if (this._onReloadCallback) {
  169. this._onReloadCallback(this._sceneFileToLoad);
  170. }
  171. else {
  172. this.reload();
  173. }
  174. }
  175. public reload() {
  176. // If a scene file has been provided
  177. if (this._sceneFileToLoad) {
  178. if (this._currentScene) {
  179. if (Tools.errorsCount > 0) {
  180. Tools.ClearLogCache();
  181. }
  182. this._engine.stopRenderLoop();
  183. this._currentScene.dispose();
  184. }
  185. SceneLoader.Load("file:", this._sceneFileToLoad, this._engine, (newScene) => {
  186. this._currentScene = newScene;
  187. if (this._sceneLoadedCallback) {
  188. this._sceneLoadedCallback(this._sceneFileToLoad, this._currentScene);
  189. }
  190. // Wait for textures and shaders to be ready
  191. this._currentScene.executeWhenReady(() => {
  192. this._engine.runRenderLoop(() => {
  193. this.renderFunction();
  194. });
  195. });
  196. }, progress => {
  197. if (this._progressCallback) {
  198. this._progressCallback(progress);
  199. }
  200. }, (scene, message) => {
  201. this._currentScene = scene;
  202. if (this._errorCallback) {
  203. this._errorCallback(this._sceneFileToLoad, this._currentScene, message);
  204. }
  205. });
  206. }
  207. else {
  208. Tools.Error("Please provide a valid .babylon file.");
  209. }
  210. }
  211. }
  212. }