Ver código fonte

Merge pull request #826 from Temechon/master

Events can now be added to animations.
Raanan Weber 9 anos atrás
pai
commit
6a4c7b9b7f

+ 1 - 0
.gitignore

@@ -120,3 +120,4 @@ UpgradeLog*.XML
 # Do not ignore loaders/obj
 !loaders/*
 node_modules
+.idea

+ 1 - 1
Tools/Gulp/config.json

@@ -5,7 +5,7 @@
     "minNoWorkerFilename": "babylon.noworker.js",
     "minCoreFilename": "babylon.core.js",
     "declarationFilename": "babylon.d.ts",
-    "outputDirectory": "../../dist/",
+    "outputDirectory": "../../dist/preview release",
     "srcOutputDirectory": "../../src/"
   },
   "core": {

Diferenças do arquivo suprimidas por serem muito extensas
+ 28 - 18
dist/preview release/babylon.core.js


Diferenças do arquivo suprimidas por serem muito extensas
+ 2526 - 2507
dist/preview release/babylon.d.ts


Diferenças do arquivo suprimidas por serem muito extensas
+ 36 - 25
dist/preview release/babylon.js


Diferenças do arquivo suprimidas por serem muito extensas
+ 6287 - 6245
dist/preview release/babylon.max.js


Diferenças do arquivo suprimidas por serem muito extensas
+ 36 - 25
dist/preview release/babylon.noworker.js


+ 1 - 0
dist/preview release/what's new.md

@@ -17,6 +17,7 @@
     - Sprites now can be [picked](http://www.babylonjs-playground.com/#1XMVZW#4) and can use [actions](http://www.babylonjs-playground.com/#9RUHH#4) ([deltakosh](https://github.com/deltakosh))
     - New `Mesh.CreatePolyhedron()` method ([jerome](https://github.com/jbousquie))
     - New `Mesh.CreateIcoSphere()` method. [Demo here](http://www.babylonjs-playground.com/#24DUYD) (G'kar)
+    - Events can now be linked to an Animation to fire actions at a given frame [Documentation](TODO) ([temechon](https://github.com/temechon))
     - Introducing [babylon.core.js](http://doc.babylonjs.com/generals/Framework_versions) ([deltakosh](https://github.com/deltakosh))
   - **Updates**
     - Added `Animatable.goToFrame()` ([deltakosh](https://github.com/deltakosh))   

+ 45 - 1
src/Animations/babylon.animation.js

@@ -9,6 +9,18 @@ var BABYLON;
         return AnimationRange;
     })();
     BABYLON.AnimationRange = AnimationRange;
+    /**
+     * Composed of a frame, and an action function
+     */
+    var AnimationEvent = (function () {
+        function AnimationEvent(frame, action, onlyOnce) {
+            this.frame = frame;
+            this.action = action;
+            this.onlyOnce = onlyOnce;
+        }
+        return AnimationEvent;
+    })();
+    BABYLON.AnimationEvent = AnimationEvent;
     var Animation = (function () {
         function Animation(name, targetProperty, framePerSecond, dataType, loopMode) {
             this.name = name;
@@ -19,6 +31,8 @@ var BABYLON;
             this._offsetsCache = {};
             this._highLimitsCache = {};
             this._stopped = false;
+            // The set of event that will be linked to this animation
+            this._events = new Array();
             this.allowMatricesInterpolation = false;
             this._ranges = new Array();
             this.targetPropertyPath = targetProperty.split(".");
@@ -64,7 +78,25 @@ var BABYLON;
             node.animations.push(animation);
             return node.getScene().beginAnimation(node, 0, totalFrame, (animation.loopMode === 1), 1.0, onAnimationEnd);
         };
-        // Methods   
+        // Methods
+        /**
+         * Add an event to this animation.
+         */
+        Animation.prototype.addEvent = function (event) {
+            this._events.push(event);
+        };
+        /**
+         * Remove all events found at the given frame
+         * @param frame
+         */
+        Animation.prototype.removeEvents = function (frame) {
+            for (var index = 0; index < this._events.length; index++) {
+                if (this._events[index].frame === frame) {
+                    this._events.splice(index, 1);
+                    index--;
+                }
+            }
+        };
         Animation.prototype.createRange = function (name, from, to) {
             this._ranges.push(new AnimationRange(name, from, to));
         };
@@ -356,6 +388,18 @@ var BABYLON;
             var repeatCount = (ratio / range) >> 0;
             var currentFrame = returnValue ? from + ratio % range : to;
             var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
+            // Check events
+            for (var index = 0; index < this._events.length; index++) {
+                if (this._events[index].frame >= currentFrame) {
+                    var event = this._events[index];
+                    // If event should be done only once, remove it.
+                    if (event.onlyOnce) {
+                        this._events.splice(index, 1);
+                        index--;
+                    }
+                    event.action();
+                }
+            }
             // Set value
             this.setValue(currentValue);
             if (!returnValue) {

+ 45 - 1
src/Animations/babylon.animation.ts

@@ -4,6 +4,14 @@
         }
     }
 
+    /**
+     * Composed of a frame, and an action function
+     */
+    export class AnimationEvent {
+        constructor(public frame: number, public action: () => void, public onlyOnce?: boolean) {
+        }
+    }
+
     export class Animation {
         private _keys: Array<any>;
         private _offsetsCache = {};
@@ -12,6 +20,9 @@
         public _target;
         private _easingFunction: IEasingFunction;
 
+        // The set of event that will be linked to this animation
+        private _events = new Array<AnimationEvent>();
+
         public targetPropertyPath: string[];
         public currentFrame: number;
 
@@ -79,7 +90,27 @@
             this.loopMode = loopMode === undefined ? Animation.ANIMATIONLOOPMODE_CYCLE : loopMode;
         }
 
-        // Methods   
+        // Methods
+        /**
+         * Add an event to this animation.
+         */
+        public addEvent(event: AnimationEvent) : void {
+            this._events.push(event);
+        }
+
+        /**
+         * Remove all events found at the given frame
+         * @param frame
+         */
+        public removeEvents(frame:number) : void {
+            for (var index = 0; index < this._events.length; index++) {
+                if (this._events[index].frame === frame) {
+                    this._events.splice(index, 1);
+                    index--;
+                }
+            }
+        }
+
         public createRange(name: string, from: number, to: number): void {
             this._ranges.push(new AnimationRange(name, from, to));
         }
@@ -421,6 +452,19 @@
             var currentFrame = returnValue ? from + ratio % range : to;
             var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
 
+            // Check events
+            for (var index = 0; index < this._events.length; index++) {
+                if (this._events[index].frame >= currentFrame) {
+                    var event = this._events[index];
+                    // If event should be done only once, remove it.
+                    if (event.onlyOnce) {
+                        this._events.splice(index, 1);
+                        index--;
+                    }
+                    event.action();
+                }
+            }
+
             // Set value
             this.setValue(currentValue);