浏览代码

gui: better message handling

David Catuhe 8 年之前
父节点
当前提交
ebce216647
共有 3 个文件被更改,包括 50 次插入8 次删除
  1. 8 0
      gui/src/advancedDynamicTexture.ts
  2. 8 0
      gui/src/controls/container.ts
  3. 34 8
      gui/src/controls/control.ts

+ 8 - 0
gui/src/advancedDynamicTexture.ts

@@ -9,6 +9,7 @@ module BABYLON.GUI {
         private _background: string;
         private _rootContainer = new Container("root");
         public _lastControlOver: Control;
+        public _toDispose: IDisposable;
 
         public get background(): string {
             return this._background;
@@ -64,6 +65,11 @@ module BABYLON.GUI {
                 this.getScene().onPointerObservable.remove(this._pointerMoveObserver);
             }
 
+            if (this._toDispose) {
+                this._toDispose.dispose();
+                this._toDispose = null;
+            }
+
             super.dispose();
         }
 
@@ -157,6 +163,8 @@ module BABYLON.GUI {
             var layer = new BABYLON.Layer(name + "_layer", null, scene, !foreground);
             layer.texture = result;
 
+            result._toDispose = layer;
+
             // Attach
             result.attach();
 

+ 8 - 0
gui/src/controls/container.ts

@@ -53,6 +53,14 @@ module BABYLON.GUI {
             // Implemented by child to be injected inside main draw
         }
 
+        public _link(root: Container, host: AdvancedDynamicTexture): void {
+            super._link(root, host);
+
+            for (var child of this._children) {
+                child._link(root, host);
+            }
+        }
+
         public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
             context.save();
             super._processMeasures(parentMeasure, context);

+ 34 - 8
gui/src/controls/control.ts

@@ -412,25 +412,51 @@ module BABYLON.GUI {
             return true;
         }
 
-        protected _processObservables(type: number): boolean {
-            if (type === BABYLON.PointerEventTypes.POINTERMOVE && this.onPointerMoveObservable.hasObservers()) {
+        protected _onPointerMove(): void {
+            if (this.onPointerMoveObservable.hasObservers()) {
                 this.onPointerMoveObservable.notifyObservers(this);
+            }
+        }
+
+        protected _onPointerOut(): void {
+            var previousControlOver = this._host._lastControlOver;
+
+            if (previousControlOver.onPointerOutObservable.hasObservers()) {
+                previousControlOver.onPointerOutObservable.notifyObservers(previousControlOver);
+            }
+        }
+
+        protected _onPointerDown(): void {
+            if (this.onPointerDownObservable.hasObservers()) {
+                this.onPointerDownObservable.notifyObservers(this);
+            }
+        }
+
+        protected _onPointerUp(): void {
+            if (this.onPointerUpObservable.hasObservers()) {
+                this.onPointerUpObservable.notifyObservers(this);
+            }
+        }
+
+        protected _processObservables(type: number): boolean {
+            if (type === BABYLON.PointerEventTypes.POINTERMOVE) {
+                this._onPointerMove();
 
                 var previousControlOver = this._host._lastControlOver;
-                if (previousControlOver && previousControlOver !== this && previousControlOver.onPointerOutObservable.hasObservers()) {
-                    previousControlOver.onPointerOutObservable.notifyObservers(previousControlOver);
+                if (previousControlOver && previousControlOver !== this) {
+                    this._onPointerOut();
                 }
                 this._host._lastControlOver = this;
                 return true;
             }
 
-            if (type === BABYLON.PointerEventTypes.POINTERDOWN && this.onPointerDownObservable.hasObservers()) {
-                this.onPointerDownObservable.notifyObservers(this);
+            if (type === BABYLON.PointerEventTypes.POINTERDOWN) {
+                this._onPointerDown();
                 return true;
             }
 
-            if (type === BABYLON.PointerEventTypes.POINTERUP && this.onPointerUpObservable.hasObservers()) {
-                this.onPointerUpObservable.notifyObservers(this);
+            if (type === BABYLON.PointerEventTypes.POINTERUP) {
+                this._onPointerUp();
                 return true;
             }