babylon.filesInput.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. module BABYLON {
  2. export class FilesInput {
  3. public static FilesToLoad: File[] = new Array<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: ProgressEvent) => void;
  9. private _additionalRenderLoopLogicCallback: () => void;
  10. private _textureLoadingCallback: (remaining: number) => void;
  11. private _startingProcessingFilesCallback: () => void;
  12. private _onReloadCallback: (sceneFile: File) => void;
  13. private _elementToMonitor: HTMLElement;
  14. private _sceneFileToLoad: File;
  15. private _filesToLoad: File[];
  16. constructor(engine: Engine, scene: Scene, sceneLoadedCallback: (sceneFile: File, scene: Scene) => void, progressCallback: (progress: ProgressEvent) => void, additionalRenderLoopLogicCallback: () => void,
  17. textureLoadingCallback: (remaining: number) => void, startingProcessingFilesCallback: () => void, onReloadCallback: (sceneFile: File) => void) {
  18. this._engine = engine;
  19. this._currentScene = scene;
  20. this._sceneLoadedCallback = sceneLoadedCallback;
  21. this._progressCallback = progressCallback;
  22. this._additionalRenderLoopLogicCallback = additionalRenderLoopLogicCallback;
  23. this._textureLoadingCallback = textureLoadingCallback;
  24. this._startingProcessingFilesCallback = startingProcessingFilesCallback;
  25. this._onReloadCallback = onReloadCallback;
  26. }
  27. private _dragEnterHandler: (any) => void;
  28. private _dragOverHandler: (any) => void;
  29. private _dropHandler: (any) => void;
  30. public monitorElementForDragNDrop(elementToMonitor: HTMLElement): void {
  31. if (elementToMonitor) {
  32. this._elementToMonitor = elementToMonitor;
  33. this._dragEnterHandler = (e) => { this.drag(e); };
  34. this._dragOverHandler = (e) => { this.drag(e); };
  35. this._dropHandler = (e) => { this.drop(e); };
  36. this._elementToMonitor.addEventListener("dragenter", this._dragEnterHandler, false);
  37. this._elementToMonitor.addEventListener("dragover", this._dragOverHandler, false);
  38. this._elementToMonitor.addEventListener("drop", this._dropHandler, false);
  39. }
  40. }
  41. public dispose() {
  42. if (!this._elementToMonitor) {
  43. return;
  44. }
  45. this._elementToMonitor.removeEventListener("dragenter", this._dragEnterHandler);
  46. this._elementToMonitor.removeEventListener("dragover", this._dragOverHandler);
  47. this._elementToMonitor.removeEventListener("drop", this._dropHandler);
  48. }
  49. private renderFunction(): void {
  50. if (this._additionalRenderLoopLogicCallback) {
  51. this._additionalRenderLoopLogicCallback();
  52. }
  53. if (this._currentScene) {
  54. if (this._textureLoadingCallback) {
  55. var remaining = this._currentScene.getWaitingItemsCount();
  56. if (remaining > 0) {
  57. this._textureLoadingCallback(remaining);
  58. }
  59. }
  60. this._currentScene.render();
  61. }
  62. }
  63. private drag(e: DragEvent): void {
  64. e.stopPropagation();
  65. e.preventDefault();
  66. }
  67. private drop(eventDrop: DragEvent): void {
  68. eventDrop.stopPropagation();
  69. eventDrop.preventDefault();
  70. this.loadFiles(eventDrop);
  71. }
  72. private _handleFolderDrop(entry: any, files: Array<any>, callback: () => void): void {
  73. var reader = entry.createReader(),
  74. relativePath = entry.fullPath.replace(/^\//, "").replace(/(.+?)\/?$/, "$1/");
  75. reader.readEntries((fileEntries) => {
  76. var remaining = fileEntries.length;
  77. for (let fileEntry of fileEntries) {
  78. if (fileEntry.isFile) { // We only support one level
  79. fileEntry.file(function(file) {
  80. file.correctName = relativePath + file.name;
  81. files.push(file);
  82. remaining--;
  83. if (remaining === 0) {
  84. callback();
  85. }
  86. });
  87. } else {
  88. remaining--;
  89. if (remaining === 0) {
  90. callback();
  91. }
  92. }
  93. }
  94. });
  95. }
  96. private _processFiles(files: Array<any>): void {
  97. var skippedFiles = 0;
  98. for (var i = 0; i < files.length; i++) {
  99. var name = files[i].correctName.toLowerCase();
  100. var extension = name.split('.').pop();
  101. if (!this.onProcessFileCallback(files[i], name, extension)) {
  102. skippedFiles++;
  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. if (this._onReloadCallback) {
  114. this._onReloadCallback(this._sceneFileToLoad);
  115. }
  116. else if (skippedFiles < files.length) {
  117. this.reload();
  118. }
  119. }
  120. public loadFiles(event): void {
  121. if (this._startingProcessingFilesCallback) this._startingProcessingFilesCallback();
  122. // Handling data transfer via drag'n'drop
  123. if (event && event.dataTransfer && event.dataTransfer.files) {
  124. this._filesToLoad = event.dataTransfer.files;
  125. }
  126. // Handling files from input files
  127. if (event && event.target && event.target.files) {
  128. this._filesToLoad = event.target.files;
  129. }
  130. if (this._filesToLoad && this._filesToLoad.length > 0) {
  131. let files = [];
  132. let folders = [];
  133. var items = event.dataTransfer ? event.dataTransfer.items : null;
  134. for (var i = 0; i < this._filesToLoad.length; i++) {
  135. let fileToLoad:any = this._filesToLoad[i];
  136. let name = fileToLoad.name.toLowerCase();
  137. let type = fileToLoad.type;
  138. let entry;
  139. fileToLoad.correctName = name;
  140. if (items) {
  141. let item = items[i];
  142. if (item.getAsEntry) {
  143. entry = item.getAsEntry();
  144. } else if (item.webkitGetAsEntry) {
  145. entry = item.webkitGetAsEntry();
  146. }
  147. }
  148. if (!entry) {
  149. files.push(fileToLoad);
  150. } else {
  151. if (entry.isDirectory) {
  152. folders.push(entry);
  153. } else {
  154. files.push(fileToLoad);
  155. }
  156. }
  157. }
  158. if (folders.length === 0) {
  159. this._processFiles(files);
  160. } else {
  161. var remaining = folders.length;
  162. // Extract folder content
  163. for (var folder of folders) {
  164. this._handleFolderDrop(folder, files, () => {
  165. remaining--;
  166. if (remaining === 0) {
  167. this._processFiles(files);
  168. }
  169. });
  170. }
  171. }
  172. }
  173. }
  174. public reload() {
  175. // If a ".babylon" file has been provided
  176. if (this._sceneFileToLoad) {
  177. if (this._currentScene) {
  178. if (Tools.errorsCount > 0) {
  179. Tools.ClearLogCache();
  180. Tools.Log("Babylon.js engine (v" + Engine.Version + ") launched");
  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. }, progress => {
  196. if (this._progressCallback) {
  197. this._progressCallback(progress);
  198. }
  199. });
  200. }
  201. else {
  202. Tools.Error("Please provide a valid .babylon file.");
  203. }
  204. }
  205. }
  206. }