浏览代码

Merge pull request #2638 from NicolasBuecher/instanciate_function

Instanciate function
David Catuhe 8 年之前
父节点
当前提交
8ff1351b33
共有 1 个文件被更改,包括 41 次插入33 次删除
  1. 41 33
      src/Tools/babylon.decorators.ts

+ 41 - 33
src/Tools/babylon.decorators.ts

@@ -2,6 +2,43 @@
     var __decoratorInitialStore = {};
     var __mergedStore = {};
 
+    var _copySource = function<T>(creationFunction: () => T, source: T, instanciate: boolean): T {
+        var destination = creationFunction();
+
+        // Tags
+        if (Tags) {
+            Tags.AddTagsTo(destination, (<any>source).tags);
+        }
+
+        var classStore = getMergedStore(destination);
+
+        // Properties
+        for (var property in classStore) {
+            var propertyDescriptor = classStore[property];
+            var sourceProperty = source[property];
+            var propertyType = propertyDescriptor.type;
+
+            if (sourceProperty !== undefined && sourceProperty !== null) {
+                switch (propertyType) {
+                    case 0:     // Value
+                    case 6:     // Mesh reference
+                        destination[property] = sourceProperty;
+                        break;
+                    case 1:     // Texture
+                    case 2:     // Color3
+                    case 3:     // FresnelParameters
+                    case 4:     // Vector2
+                    case 5:     // Vector3
+                    case 7:     // Color Curves
+                        destination[property] = instanciate?sourceProperty:sourceProperty.clone();
+                        break;
+                }
+            }
+        }
+
+        return destination;
+    };
+
     function getDirectStore(target: any): any {
         var classKey = target.getClassName();
 
@@ -255,40 +292,11 @@
         }
 
         public static Clone<T>(creationFunction: () => T, source: T): T {
-            var destination = creationFunction();
-
-            // Tags
-            if (Tags) {
-                Tags.AddTagsTo(destination, (<any>source).tags);
-            }
-            
-            var classStore = getMergedStore(destination);
-
-            // Properties
-            for (var property in classStore) {
-                var propertyDescriptor = classStore[property];
-                var sourceProperty = source[property];
-                var propertyType = propertyDescriptor.type;
-
-                if (sourceProperty !== undefined && sourceProperty !== null) {
-                    switch (propertyType) {
-                        case 0:     // Value
-                        case 6:     // Mesh reference
-                            destination[property] = sourceProperty;
-                            break;
-                        case 1:     // Texture
-                        case 2:     // Color3
-                        case 3:     // FresnelParameters
-                        case 4:     // Vector2
-                        case 5:     // Vector3
-                        case 7:     // Color Curves
-                            destination[property] = sourceProperty.clone();
-                            break;
-                    }
-                }
-            }
+            return _copySource(creationFunction, source, false);
+        }
 
-            return destination;
+        public static Instanciate<T>(creationFunction: () => T, source: T): T {
+            return _copySource(creationFunction, source, true);
         }
     }
 }