babylon.filesInput.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. module BABYLON {
  2. export class FilesInput {
  3. private _engine: Engine;
  4. private _currentScene: Scene;
  5. private _canvas: HTMLCanvasElement;
  6. private _sceneLoadedCallback;
  7. private _progressCallback;
  8. private _additionnalRenderLoopLogicCallback;
  9. private _textureLoadingCallback;
  10. private _startingProcessingFilesCallback;
  11. private _elementToMonitor: HTMLElement;
  12. public static FilesTextures: any[] = new Array();
  13. public static FilesToLoad: any[] = new Array();
  14. private _sceneFileToLoad: File;
  15. private _filesToLoad: File[];
  16. /// Register to core BabylonJS object: engine, scene, rendering canvas, callback function when the scene will be loaded,
  17. /// loading progress callback and optionnal addionnal logic to call in the rendering loop
  18. constructor(p_engine: Engine, p_scene: Scene, p_canvas: HTMLCanvasElement, p_sceneLoadedCallback,
  19. p_progressCallback, p_additionnalRenderLoopLogicCallback, p_textureLoadingCallback, p_startingProcessingFilesCallback) {
  20. this._engine = p_engine;
  21. this._canvas = p_canvas;
  22. this._currentScene = p_scene;
  23. this._sceneLoadedCallback = p_sceneLoadedCallback;
  24. this._progressCallback = p_progressCallback;
  25. this._additionnalRenderLoopLogicCallback = p_additionnalRenderLoopLogicCallback;
  26. this._textureLoadingCallback = p_textureLoadingCallback;
  27. this._startingProcessingFilesCallback = p_startingProcessingFilesCallback;
  28. }
  29. public monitorElementForDragNDrop(p_elementToMonitor: HTMLElement): void {
  30. if (p_elementToMonitor) {
  31. this._elementToMonitor = p_elementToMonitor;
  32. this._elementToMonitor.addEventListener("dragenter", (e) => { this.drag(e); }, false);
  33. this._elementToMonitor.addEventListener("dragover", (e) => { this.drag(e); }, false);
  34. this._elementToMonitor.addEventListener("drop", (e) => { this.drop(e); }, false);
  35. }
  36. }
  37. private renderFunction(): void {
  38. if (this._additionnalRenderLoopLogicCallback) {
  39. this._additionnalRenderLoopLogicCallback();
  40. }
  41. if (this._currentScene) {
  42. if (this._textureLoadingCallback) {
  43. var remaining = this._currentScene.getWaitingItemsCount();
  44. if (remaining > 0) {
  45. this._textureLoadingCallback(remaining);
  46. }
  47. }
  48. this._currentScene.render();
  49. }
  50. }
  51. private drag(e): void {
  52. e.stopPropagation();
  53. e.preventDefault();
  54. }
  55. private drop(eventDrop): void {
  56. eventDrop.stopPropagation();
  57. eventDrop.preventDefault();
  58. this.loadFiles(eventDrop);
  59. }
  60. public loadFiles(event): void {
  61. if (this._startingProcessingFilesCallback) this._startingProcessingFilesCallback();
  62. // Handling data transfer via drag'n'drop
  63. if (event && event.dataTransfer && event.dataTransfer.files) {
  64. this._filesToLoad = event.dataTransfer.files;
  65. }
  66. // Handling files from input files
  67. if (event && event.target && event.target.files) {
  68. this._filesToLoad = event.target.files;
  69. }
  70. if (this._filesToLoad && this._filesToLoad.length > 0) {
  71. for (var i = 0; i < this._filesToLoad.length; i++) {
  72. switch (this._filesToLoad[i].type) {
  73. case "image/jpeg":
  74. case "image/png":
  75. case "image/bmp":
  76. FilesInput.FilesTextures[this._filesToLoad[i].name] = this._filesToLoad[i];
  77. break;
  78. case "image/targa":
  79. case "image/vnd.ms-dds":
  80. case "audio/wav":
  81. case "audio/x-wav":
  82. case "audio/mp3":
  83. case "audio/mpeg":
  84. case "audio/mpeg3":
  85. case "audio/x-mpeg-3":
  86. case "audio/ogg":
  87. FilesInput.FilesToLoad[this._filesToLoad[i].name] = this._filesToLoad[i];
  88. break;
  89. default:
  90. if (this._filesToLoad[i].name.indexOf(".babylon") !== -1 && this._filesToLoad[i].name.indexOf(".manifest") === -1
  91. && this._filesToLoad[i].name.indexOf(".incremental") === -1 && this._filesToLoad[i].name.indexOf(".babylonmeshdata") === -1
  92. && this._filesToLoad[i].name.indexOf(".babylongeometrydata") === -1) {
  93. this._sceneFileToLoad = this._filesToLoad[i];
  94. }
  95. break;
  96. }
  97. }
  98. this.reload();
  99. }
  100. }
  101. public reload() {
  102. var that = this;
  103. // If a ".babylon" file has been provided
  104. if (this._sceneFileToLoad) {
  105. if (this._currentScene) {
  106. this._engine.stopRenderLoop();
  107. this._currentScene.dispose();
  108. }
  109. SceneLoader.Load("file:", this._sceneFileToLoad, this._engine, (newScene) => {
  110. that._currentScene = newScene;
  111. // Wait for textures and shaders to be ready
  112. that._currentScene.executeWhenReady(() => {
  113. // Attach camera to canvas inputs
  114. if (!that._currentScene.activeCamera || that._currentScene.lights.length === 0) {
  115. that._currentScene.createDefaultCameraOrLight();
  116. }
  117. that._currentScene.activeCamera.attachControl(that._canvas);
  118. if (that._sceneLoadedCallback) {
  119. that._sceneLoadedCallback(this._sceneFileToLoad, that._currentScene);
  120. }
  121. that._engine.runRenderLoop(() => { that.renderFunction(); });
  122. });
  123. }, progress => {
  124. if (this._progressCallback) {
  125. this._progressCallback(progress);
  126. }
  127. });
  128. }
  129. else {
  130. Tools.Error("Please provide a valid .babylon file.");
  131. }
  132. }
  133. }
  134. }