Browse Source

Texture and Sprite2D changes

Texture: implement a onLoadObservable to be notified when the texture is loaded

Sprite2D: sprite get the size of the texture if not specified, even if the texture is not loaded yet
nockawa 9 năm trước cách đây
mục cha
commit
041c9a9a0f

+ 8 - 3
src/Canvas2d/babylon.sprite2d.ts

@@ -311,9 +311,14 @@
             this.alignToPixel = (settings.alignToPixel == null) ? true : settings.alignToPixel;
             this.isAlphaTest = true;
 
-            if (settings.spriteSize==null) {
-                var s = texture.getSize();
-                this.size = new Size(s.width, s.height);
+            if (settings.spriteSize == null) {
+                if (texture.isReady()) {
+                    this.size = <Size>texture.getSize();
+                } else {
+                    texture.onLoadObservable.add(() => {
+                        this.size = <Size>texture.getSize();
+                    });
+                }
             }
         }
 

+ 6 - 6
src/Materials/Textures/babylon.baseTexture.ts

@@ -95,25 +95,25 @@
 
         public getSize(): ISize  {
             if (this._texture._width) {
-                return { width: this._texture._width, height: this._texture._height };
+                return new Size(this._texture._width, this._texture._height);
             }
 
             if (this._texture._size) {
-                return { width: this._texture._size, height: this._texture._size };
+                return new Size(this._texture._size, this._texture._size);
             }
 
-            return { width: 0, height: 0 };
+            return Size.Zero();
         }
 
         public getBaseSize(): ISize {
             if (!this.isReady() || !this._texture)
-                return { width: 0, height: 0 };
+                return Size.Zero();
 
             if (this._texture._size) {
-                return { width: this._texture._size, height: this._texture._size };
+                return new Size(this._texture._size, this._texture._size);
             }
 
-            return { width: this._texture._baseWidth, height: this._texture._baseHeight };
+            return new Size(this._texture._baseWidth, this._texture._baseHeight);
         }
 
         public scale(ratio: number): void {

+ 20 - 7
src/Materials/Textures/babylon.texture.ts

@@ -70,6 +70,7 @@
         private _deleteBuffer: boolean;
         private _delayedOnLoad: () => void;
         private _delayedOnError: () => void;
+        private _onLoadObservarble: Observable<boolean>;
 
         constructor(url: string, scene: Scene, noMipmap?: boolean, invertY?: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null, buffer: any = null, deleteBuffer: boolean = false) {
             super(scene);
@@ -88,24 +89,29 @@
 
             this._texture = this._getFromCache(url, noMipmap, samplingMode);
 
+            let load = () => {
+                if (this._onLoadObservarble && this._onLoadObservarble.hasObservers()) {
+                    this.onLoadObservable.notifyObservers(true);
+                }
+                if (onLoad) {
+                    onLoad();
+                }
+            }
+
             if (!this._texture) {
                 if (!scene.useDelayedTextureLoading) {
-                    this._texture = scene.getEngine().createTexture(url, noMipmap, invertY, scene, this._samplingMode, onLoad, onError, this._buffer);
+                    this._texture = scene.getEngine().createTexture(url, noMipmap, invertY, scene, this._samplingMode, load, onError, this._buffer);
                     if (deleteBuffer) {
                         delete this._buffer;
                     }
                 } else {
                     this.delayLoadState = Engine.DELAYLOADSTATE_NOTLOADED;
 
-                    this._delayedOnLoad = onLoad;
+                    this._delayedOnLoad = load;
                     this._delayedOnError = onError;
                 }
             } else {
-                Tools.SetImmediate(() => {
-                    if (onLoad) {
-                        onLoad();
-                    }
-                });
+                Tools.SetImmediate(() => load());
             }
         }
 
@@ -245,6 +251,13 @@
             }, this);       
         }
 
+        public get onLoadObservable(): Observable<boolean> {
+            if (!this._onLoadObservarble) {
+                this._onLoadObservarble = new Observable<boolean>();
+            }
+            return this._onLoadObservarble;
+        }
+
         // Statics
         public static CreateFromBase64String(data: string, name: string, scene: Scene, noMipmap?: boolean, invertY?: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null): Texture {
             return new Texture("data:" + name, scene, noMipmap, invertY, samplingMode, onLoad, onError, data);