Browse Source

Serialized all direct actions and interpolate value action

moreau-mathis 9 năm trước cách đây
mục cha
commit
c03edc3878

+ 39 - 0
src/Actions/babylon.action.ts

@@ -102,6 +102,7 @@
                 properties: serializedAction.properties || []
             };
             
+            /*
             // If target, auto-complete
             if (target) {
                 var targetObject = {
@@ -116,6 +117,7 @@
                 // Concat action's properties
                 serializationObject.properties = [targetObject].concat(serializationObject.properties);
             }
+            */
             
             // Serialize child
             if (this._child) {
@@ -138,5 +140,42 @@
             }
             return serializationObject;
         }
+        
+        public static _SerializeValueAsString = (value: any): string => {
+            if (typeof value === "number") {
+                return String(value);
+            }
+            
+            if (typeof value === "boolean") {
+                return value ? "true" : "false";
+            }
+            
+            if (value instanceof Vector2) {
+                return String(value.x + ", " + value.y);
+            }
+            if (value instanceof Vector3) {
+                return String(value.x + ", " + value.y + ", " + value.z);
+            }
+            
+            if (value instanceof Color3) {
+                return String(value.r + ", " + value.g + ", " + value.b);
+            }
+            if (value instanceof Color4) {
+                return String(value.r + ", " + value.g + ", " + value.b + ", " + value.a);
+            }
+            
+            return value; // String
+        };
+    
+        public static _GetTargetProperty = (target: Scene | Node) => {
+            return {
+                name: "target",
+                targetType: target instanceof Mesh ? "MeshProperties"
+                            : target instanceof Light ? "LightProperties"
+                            : target instanceof Camera ? "CameraProperties"
+                            : "Scene",
+                value: target instanceof Scene ? "Scene" : target.name
+            }  
+        };
     }
 }

+ 113 - 11
src/Actions/babylon.directActions.ts

@@ -1,26 +1,30 @@
 module BABYLON {
     export class SwitchBooleanAction extends Action {
         private _target: any;
+        private _effectiveTarget: any;
         private _property: string;
 
         constructor(triggerOptions: any, target: any, public propertyPath: string, condition?: Condition) {
             super(triggerOptions, condition);
-            this._target = target;
+            this._target = this._effectiveTarget = target;
         }
 
         public _prepare(): void {
-            this._target = this._getEffectiveTarget(this._target, this.propertyPath);
+            this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
             this._property = this._getProperty(this.propertyPath);
         }
 
         public execute(): void {
-            this._target[this._property] = !this._target[this._property];
+            this._effectiveTarget[this._property] = !this._effectiveTarget[this._property];
         }
         
         public serialize(parent: any): any {
             return super._serialize({
                 name: "SwitchBooleanAction",
-                properties: [{ name: "propertyPath", value: this.propertyPath }]
+                properties: [
+                    Action._GetTargetProperty(this._target),
+                    { name: "propertyPath", value: this.propertyPath }
+                ]
             }, parent, this._target);
         }
     }
@@ -36,47 +40,81 @@
         public execute(): void {
             this._target.state = this.value;
         }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "SetStateAction",
+                properties: [
+                    Action._GetTargetProperty(this._target),
+                    { name: "value", value: this.value }
+                ]
+            }, parent, this._target);
+        }
     }
 
     export class SetValueAction extends Action {
         private _target: any;
+        private _effectiveTarget: any;
         private _property: string;
 
         constructor(triggerOptions: any, target: any, public propertyPath: string, public value: any, condition?: Condition) {
             super(triggerOptions, condition);
-            this._target = target;
+            this._target = this._effectiveTarget = target;
         }
 
         public _prepare(): void {
-            this._target = this._getEffectiveTarget(this._target, this.propertyPath);
+            this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
             this._property = this._getProperty(this.propertyPath);
         }
 
         public execute(): void {
-            this._target[this._property] = this.value;
+            this._effectiveTarget[this._property] = this.value;
+        }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "SetValueAction",
+                properties: [
+                    Action._GetTargetProperty(this._target),
+                    { name: "propertyPath", value: this.propertyPath },
+                    { name: "value", value: Action._SerializeValueAsString(this.value) }
+                ]
+            }, parent, this._target);
         }
     }
 
     export class IncrementValueAction extends Action {
         private _target: any;
+        private _effectiveTarget: any;
         private _property: string;
 
         constructor(triggerOptions: any, target: any, public propertyPath: string, public value: any, condition?: Condition) {
             super(triggerOptions, condition);
-            this._target = target;
+            this._target = this._effectiveTarget = target;
         }
 
         public _prepare(): void {
-            this._target = this._getEffectiveTarget(this._target, this.propertyPath);
+            this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
             this._property = this._getProperty(this.propertyPath);
 
-            if (typeof this._target[this._property] !== "number") {
+            if (typeof this._effectiveTarget[this._property] !== "number") {
                 Tools.Warn("Warning: IncrementValueAction can only be used with number values");
             }
         }
 
         public execute(): void {
-            this._target[this._property] += this.value;
+            this._effectiveTarget[this._property] += this.value;
+        }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "IncrementValueAction",
+                properties: [
+                    Action._GetTargetProperty(this._target),
+                    { name: "propertyPath", value: this.propertyPath },
+                    { name: "value", value: Action._SerializeValueAsString(this.value) }
+                ]
+            }, parent, this._target);
         }
     }
 
@@ -95,6 +133,18 @@
             var scene = this._actionManager.getScene();
             scene.beginAnimation(this._target, this.from, this.to, this.loop);
         }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "PlayAnimationAction",
+                properties: [
+                    Action._GetTargetProperty(this._target),
+                    { name: "from", value: String(this.from) },
+                    { name: "to", value: String(this.to) },
+                    { name: "loop", value: Action._SerializeValueAsString(this.loop) || false }
+                ]
+            }, parent, this._target);
+        }
     }
 
     export class StopAnimationAction extends Action {
@@ -112,6 +162,13 @@
             var scene = this._actionManager.getScene();
             scene.stopAnimation(this._target);
         }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "StopAnimationAction",
+                properties: [Action._GetTargetProperty(this._target)]
+            }, parent, this._target);
+        }
     }
 
     export class DoNothingAction extends Action {
@@ -121,6 +178,13 @@
 
         public execute(): void {
         }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "DoNothingAction",
+                properties: []
+            }, parent);
+        }
     }
 
     export class CombineAction extends Action {
@@ -140,6 +204,20 @@
                 this.children[index].execute(evt);
             }
         }
+        
+        public serialize(parent: any): any {
+            var serializationObject = super._serialize({
+                name: "CombineAction",
+                properties: [],
+                combine: []
+            }, parent);
+            
+            for (var i=0; i < this.children.length; i++) {
+                serializationObject.combine.push(this.children[i].serialize(null));
+            }
+            
+            return serializationObject;
+        }
     }
 
     export class ExecuteCodeAction extends Action {
@@ -177,6 +255,16 @@
 
             this._target.parent = this._parent;
         }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "SetParentAction",
+                properties: [
+                    Action._GetTargetProperty(this._target),
+                    Action._GetTargetProperty(this._parent),
+                ]
+            }, parent);
+        }
     }
 
     export class PlaySoundAction extends Action {
@@ -194,6 +282,13 @@
             if (this._sound !== undefined)
                 this._sound.play();
         }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "PlaySoundAction",
+                properties: [{ name: "sound", value: this._sound.name }]
+            }, parent);
+        }
     }
 
     export class StopSoundAction extends Action {
@@ -211,5 +306,12 @@
             if (this._sound !== undefined)
                 this._sound.stop();
         }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "StopSoundAction",
+                properties: [{ name: "sound", value: this._sound.name }]
+            }, parent);
+        }
     }
 } 

+ 19 - 5
src/Actions/babylon.interpolateValueAction.ts

@@ -1,16 +1,17 @@
 module BABYLON {
     export class InterpolateValueAction extends Action {
         private _target: any;
+        private _effectiveTarget: any;
         private _property: string;
 
         constructor(triggerOptions: any, target: any, public propertyPath: string, public value: any, public duration: number = 1000, condition?: Condition, public stopOtherAnimations?: boolean, public onInterpolationDone?: () => void) {
             super(triggerOptions, condition);
 
-            this._target = target;
+            this._target = this._effectiveTarget = target;
         }
 
         public _prepare(): void {
-            this._target = this._getEffectiveTarget(this._target, this.propertyPath);
+            this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
             this._property = this._getProperty(this.propertyPath);
         }
 
@@ -19,7 +20,7 @@
             var keys = [
                 {
                     frame: 0,
-                    value: this._target[this._property]
+                    value: this._effectiveTarget[this._property]
                 }, {
                     frame: 100,
                     value: this.value
@@ -48,10 +49,23 @@
             animation.setKeys(keys);
 
             if (this.stopOtherAnimations) {
-                scene.stopAnimation(this._target);
+                scene.stopAnimation(this._effectiveTarget);
             }
 
-            scene.beginDirectAnimation(this._target, [animation], 0, 100, false, 1, this.onInterpolationDone);
+            scene.beginDirectAnimation(this._effectiveTarget, [animation], 0, 100, false, 1, this.onInterpolationDone);
+        }
+        
+        public serialize(parent: any): any {
+            return super._serialize({
+                name: "InterpolateValueAction",
+                properties: [
+                    Action._GetTargetProperty(this._target),
+                    { name: "propertyPath", value: this.propertyPath },
+                    { name: "value", value: Action._SerializeValueAsString(this.value) },
+                    { name: "duration", value: Action._SerializeValueAsString(this.duration) },
+                    { name: "stopOtherAnimations", value: Action._SerializeValueAsString(this.stopOtherAnimations) || false }
+                ]
+            }, parent, this._target);
         }
     }
 }