Explorar o código

Finalized serialization of actions (including conditions)

TODO: Triggers
moreau-mathis %!s(int64=9) %!d(string=hai) anos
pai
achega
7036d1291d

+ 1 - 18
src/Actions/babylon.action.ts

@@ -94,7 +94,7 @@
         }
         
         // Called by BABYLON.Action objects in serialize(...). Internal use
-        protected _serialize(serializedAction: any, parent?: any, target?: Node | Scene): any {
+        protected _serialize(serializedAction: any, parent?: any): any {
             var serializationObject: any = { 
                 type: 1,
                 children: [],
@@ -102,23 +102,6 @@
                 properties: serializedAction.properties || []
             };
             
-            /*
-            // If target, auto-complete
-            if (target) {
-                var targetObject = {
-                    name: "target",
-                    targetType: target instanceof Mesh ? "MeshProperties"
-                              : target instanceof Light ? "LightProperties"
-                              : target instanceof Camera ? "CameraProperties"
-                              : "Scene",
-                    value: target instanceof Scene ? "Scene" : target.name
-                }
-                
-                // Concat action's properties
-                serializationObject.properties = [targetObject].concat(serializationObject.properties);
-            }
-            */
-            
             // Serialize child
             if (this._child) {
                 this._child.serialize(serializationObject);

+ 21 - 22
src/Actions/babylon.actionManager.ts

@@ -317,28 +317,6 @@
             
             return root;
         }
-        
-        public static GetTriggerName(trigger: number): string {
-            switch (trigger) {
-                case 0:  return "NothingTrigger";
-                case 1:  return "OnPickTrigger";
-                case 2:  return "OnLeftPickTrigger";
-                case 3:  return "OnRightPickTrigger";
-                case 4:  return "OnCenterPickTrigger";
-                case 5:  return "OnPickDownTrigger";
-                case 6:  return "OnPickUpTrigger";
-                case 7:  return "OnLongPressTrigger";
-                case 8:  return "OnPointerOverTrigger";
-                case 9:  return "OnPointerOutTrigger";
-                case 10: return "OnEveryFrameTrigger";
-                case 11: return "OnIntersectionEnterTrigger";
-                case 12: return "OnIntersectionExitTrigger";
-                case 13: return "OnKeyDownTrigger";
-                case 14: return "OnKeyUpTrigger";
-                case 15: return "OnPickOutTrigger";
-                default: return "";
-            }
-        }
 
         public static Parse(parsedActions: any, object: AbstractMesh, scene: Scene) {
             var actionManager = new BABYLON.ActionManager(scene);
@@ -519,5 +497,26 @@
             }
         }
 
+        public static GetTriggerName(trigger: number): string {
+            switch (trigger) {
+                case 0:  return "NothingTrigger";
+                case 1:  return "OnPickTrigger";
+                case 2:  return "OnLeftPickTrigger";
+                case 3:  return "OnRightPickTrigger";
+                case 4:  return "OnCenterPickTrigger";
+                case 5:  return "OnPickDownTrigger";
+                case 6:  return "OnPickUpTrigger";
+                case 7:  return "OnLongPressTrigger";
+                case 8:  return "OnPointerOverTrigger";
+                case 9:  return "OnPointerOutTrigger";
+                case 10: return "OnEveryFrameTrigger";
+                case 11: return "OnIntersectionEnterTrigger";
+                case 12: return "OnIntersectionExitTrigger";
+                case 13: return "OnKeyDownTrigger";
+                case 14: return "OnKeyUpTrigger";
+                case 15: return "OnPickOutTrigger";
+                default: return "";
+            }
+        }
     }
 } 

+ 37 - 27
src/Actions/babylon.condition.ts

@@ -24,30 +24,13 @@
         public serialize(): any {
         }
         
-        protected _serialize(serializedCondition: any, target?: Node | Scene): any {
-            var serializationObject = { 
-                type: 3, // Condition
+        protected _serialize(serializedCondition: any): any {
+            return { 
+                type: 2, // Condition
                 children: [],
                 name: serializedCondition.name,
                 properties: serializedCondition.properties
             };
-            
-            // If target, auto-complete
-            if (target) {
-                var targetObject = {
-                    name: "target",
-                    targetType: target instanceof Mesh ? "MeshProperties"
-                              : target instanceof Light ? "LightProperties"
-                              : target instanceof Camera ? "CameraProperties"
-                              : "Scene",
-                    value: target instanceof Scene ? "Scene" : target.name
-                }
-                
-                // Concat action's properties
-                serializationObject.properties = [targetObject].concat(serializationObject.properties);
-            }
-            
-            return serializationObject;
         }
     }
 
@@ -78,12 +61,14 @@
         public _actionManager: ActionManager;
 
         private _target: any;
+        private _effectiveTarget: any;
         private _property: string;
 
         constructor(actionManager: ActionManager, target: any, public propertyPath: string, public value: any, public operator: number = ValueCondition.IsEqual) {
             super(actionManager);
 
-            this._target = this._getEffectiveTarget(target, this.propertyPath);
+            this._target = target;
+            this._effectiveTarget = this._getEffectiveTarget(target, this.propertyPath);
             this._property = this._getProperty(this.propertyPath);
         }
 
@@ -91,23 +76,45 @@
         public isValid(): boolean {
             switch (this.operator) {
                 case ValueCondition.IsGreater:
-                    return this._target[this._property] > this.value;
+                    return this._effectiveTarget[this._property] > this.value;
                 case ValueCondition.IsLesser:
-                    return this._target[this._property] < this.value;
+                    return this._effectiveTarget[this._property] < this.value;
                 case ValueCondition.IsEqual:
                 case ValueCondition.IsDifferent:
                     var check: boolean;
 
                     if (this.value.equals) {
-                        check = this.value.equals(this._target[this._property]);
+                        check = this.value.equals(this._effectiveTarget[this._property]);
                     } else {
-                        check = this.value === this._target[this._property];
+                        check = this.value === this._effectiveTarget[this._property];
                     }
                     return this.operator === ValueCondition.IsEqual ? check : !check;
             }
 
             return false;
         }
+        
+        public serialize(): any {
+            return this._serialize({
+               name: "ValueCondition",
+               properties: [
+                   Action._GetTargetProperty(this._target),
+                   { name: "propertyPath", value: this.propertyPath },
+                   { name: "value", value: Action._SerializeValueAsString(this.value) },
+                   { name: "operator", value: ValueCondition.GetOperatorName(this.operator) }
+                ]
+            });
+        }
+        
+        public static GetOperatorName(operator: number): string {
+            switch (operator) {
+                case ValueCondition._IsEqual: return "IsEqual";
+                case ValueCondition._IsDifferent: return "IsDifferent";
+                case ValueCondition._IsGreater: return "IsGreater";
+                case ValueCondition._IsLesser: return "IsLesser";
+                default: return "";
+            }
+        }
     }
 
     export class PredicateCondition extends Condition {
@@ -144,8 +151,11 @@
         public serialize(): any {
             return this._serialize({
                name: "StateCondition",
-               properties: [{ name: "value", value: this.value }]
-            }, this._target);
+               properties: [
+                   Action._GetTargetProperty(this._target),
+                   { name: "value", value: this.value }
+                ]
+            });
         }
     }
 

+ 6 - 6
src/Actions/babylon.directActions.ts

@@ -25,7 +25,7 @@
                     Action._GetTargetProperty(this._target),
                     { name: "propertyPath", value: this.propertyPath }
                 ]
-            }, parent, this._target);
+            }, parent);
         }
     }
 
@@ -48,7 +48,7 @@
                     Action._GetTargetProperty(this._target),
                     { name: "value", value: this.value }
                 ]
-            }, parent, this._target);
+            }, parent);
         }
     }
 
@@ -79,7 +79,7 @@
                     { name: "propertyPath", value: this.propertyPath },
                     { name: "value", value: Action._SerializeValueAsString(this.value) }
                 ]
-            }, parent, this._target);
+            }, parent);
         }
     }
 
@@ -114,7 +114,7 @@
                     { name: "propertyPath", value: this.propertyPath },
                     { name: "value", value: Action._SerializeValueAsString(this.value) }
                 ]
-            }, parent, this._target);
+            }, parent);
         }
     }
 
@@ -143,7 +143,7 @@
                     { name: "to", value: String(this.to) },
                     { name: "loop", value: Action._SerializeValueAsString(this.loop) || false }
                 ]
-            }, parent, this._target);
+            }, parent);
         }
     }
 
@@ -167,7 +167,7 @@
             return super._serialize({
                 name: "StopAnimationAction",
                 properties: [Action._GetTargetProperty(this._target)]
-            }, parent, this._target);
+            }, parent);
         }
     }
 

+ 1 - 1
src/Actions/babylon.interpolateValueAction.ts

@@ -65,7 +65,7 @@
                     { name: "duration", value: Action._SerializeValueAsString(this.duration) },
                     { name: "stopOtherAnimations", value: Action._SerializeValueAsString(this.stopOtherAnimations) || false }
                 ]
-            }, parent, this._target);
+            }, parent);
         }
     }
 }