babylon.filesInput.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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: File[] = new Array();
  13. public static FilesToLoad: File[] = 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: DragEvent): void {
  52. e.stopPropagation();
  53. e.preventDefault();
  54. }
  55. private drop(eventDrop: DragEvent): 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. let name = this._filesToLoad[i].name.toLowerCase();
  73. let extension = name.split('.').pop();
  74. let type = this._filesToLoad[i].type;
  75. if (extension === "jpg" || extension === "png" || extension === "bmp" || extension === "jpeg" ||
  76. type === "image/jpeg" || type === "image/png" || type === "image/bmp") {
  77. FilesInput.FilesTextures[name] = this._filesToLoad[i];
  78. }
  79. else {
  80. if ((extension === "babylon" || extension === "stl" || extension === "obj")
  81. && name.indexOf(".binary.babylon") === -1 && name.indexOf(".incremental.babylon") === -1) {
  82. this._sceneFileToLoad = this._filesToLoad[i];
  83. }
  84. else {
  85. FilesInput.FilesToLoad[name] = this._filesToLoad[i];
  86. }
  87. }
  88. }
  89. this.reload();
  90. }
  91. }
  92. public reload() {
  93. var that = this;
  94. // If a ".babylon" file has been provided
  95. if (this._sceneFileToLoad) {
  96. if (this._currentScene) {
  97. if (Tools.errorsCount > 0) {
  98. Tools.ClearLogCache();
  99. Tools.Log("Babylon.js engine (v" + Engine.Version + ") launched");
  100. }
  101. this._engine.stopRenderLoop();
  102. this._currentScene.dispose();
  103. }
  104. SceneLoader.Load("file:", this._sceneFileToLoad, this._engine, (newScene) => {
  105. that._currentScene = newScene;
  106. // Wait for textures and shaders to be ready
  107. that._currentScene.executeWhenReady(() => {
  108. // Attach camera to canvas inputs
  109. if (!that._currentScene.activeCamera || that._currentScene.lights.length === 0) {
  110. that._currentScene.createDefaultCameraOrLight();
  111. }
  112. that._currentScene.activeCamera.attachControl(that._canvas);
  113. if (that._sceneLoadedCallback) {
  114. that._sceneLoadedCallback(this._sceneFileToLoad, that._currentScene);
  115. }
  116. that._engine.runRenderLoop(() => { that.renderFunction(); });
  117. });
  118. }, progress => {
  119. if (this._progressCallback) {
  120. this._progressCallback(progress);
  121. }
  122. });
  123. }
  124. else {
  125. Tools.Error("Please provide a valid .babylon file.");
  126. }
  127. }
  128. }
  129. }