Browse Source

make sure that copy/paste of prefab also copy its data files

Nicolas Cannasse 3 năm trước cách đây
mục cha
commit
a9a71a2000
2 tập tin đã thay đổi với 41 bổ sung7 xóa
  1. 37 4
      hide/comp/SceneEditor.hx
  2. 4 3
      hide/ui/View.hx

+ 37 - 4
hide/comp/SceneEditor.hx

@@ -951,7 +951,7 @@ class SceneEditor {
 					if(obj3d.follow != null) {
 						if(obj3d.followPositionOnly)
 							parentMat.setPosition(obj3d.follow.getAbsPos().getPosition());
-						else 
+						else
 							parentMat = obj3d.follow.getAbsPos().clone();
 					}
 					var invParent = parentMat;
@@ -1861,7 +1861,8 @@ class SceneEditor {
 	function onCopy() {
 		if(curEdit == null) return;
 		if(curEdit.rootElements.length == 1) {
-			view.setClipboard(curEdit.rootElements[0].saveData(), "prefab");
+			var prefab = curEdit.rootElements[0];
+			view.setClipboard(prefab.saveData(), "prefab", { source : view.state.path, name : prefab.name });
 		}
 		else {
 			var lib = new hrt.prefab.Library();
@@ -1872,15 +1873,44 @@ class SceneEditor {
 		}
 	}
 
+	function getDataPath( prefabName : String, ?sourceFile : String ) {
+		if( sourceFile == null ) sourceFile = view.state.path;
+		var datPath = new haxe.io.Path(sourceFile);
+		datPath.ext = "dat";
+		return ide.getPath(datPath.toString()+"/"+prefabName);
+	}
+
 	function onPaste() {
 		var parent : PrefabElement = sceneData;
 		if(curEdit != null && curEdit.elements.length > 0) {
 			parent = curEdit.elements[0];
 		}
-		var obj = haxe.Json.parse(haxe.Json.stringify(view.getClipboard("prefab")));
+		var opts : { ref : {source:String,name:String} } = { ref : null };
+		var obj = view.getClipboard("prefab",opts);
 		if(obj != null) {
 			var p = hrt.prefab.Prefab.loadPrefab(obj, parent);
 			autoName(p);
+
+			if( opts.ref != null && opts.ref.source != null && opts.ref.name != null ) {
+				// copy data
+				var srcDir = getDataPath(opts.ref.name, opts.ref.source);
+				if( sys.FileSystem.exists(srcDir) && sys.FileSystem.isDirectory(srcDir) ) {
+					var dstDir = getDataPath(p.name);
+					function copyRec( src : String, dst : String ) {
+						if( !sys.FileSystem.exists(dst) ) sys.FileSystem.createDirectory(dst);
+						for( f in sys.FileSystem.readDirectory(src) ) {
+							var file = src+"/"+f;
+							if( sys.FileSystem.isDirectory(file) ) {
+								copyRec(file,dst+"/"+f);
+								continue;
+							}
+							sys.io.File.copy(file,dst+"/"+f);
+						}
+					}
+					copyRec(srcDir, dstDir);
+				}
+			}
+
 			addElements([p]);
 		}
 		else {
@@ -2258,12 +2288,15 @@ class SceneEditor {
 		}
 	}
 
-	function autoName(p : PrefabElement) {
+	function autoName(p : PrefabElement ) {
 
 		var uniqueName = false;
 		if( p.type == "volumetricLightmap" || p.type == "light" )
 			uniqueName = true;
 
+		if( !uniqueName && sys.FileSystem.exists(getDataPath(p.name)) )
+			uniqueName = true;
+
 		var prefix = null;
 		if(p.name != null && p.name.length > 0) {
 			if(uniqueName)

+ 4 - 3
hide/ui/View.hx

@@ -73,8 +73,8 @@ class View<T> extends hide.comp.Component {
 		return Type.getClassName(Type.getClass(this));
 	}
 
-	public function setClipboard( v : Dynamic, ?type : String ) {
-		nw.Clipboard.get().set(ide.toJSON({ type : type == null ? viewClass : type, value : v }));
+	public function setClipboard( v : Dynamic, ?type : String, ?opts : {} ) {
+		nw.Clipboard.get().set(ide.toJSON({ type : type == null ? viewClass : type, value : v, opts : opts }));
 	}
 
 	public function hasClipboard( ?type : String ) {
@@ -83,9 +83,10 @@ class View<T> extends hide.comp.Component {
 		return v != null && v.type == type;
 	}
 
-	public function getClipboard( ?type : String ) : Dynamic {
+	public function getClipboard( ?type : String, ?opts : { ref : Dynamic } ) : Dynamic {
 		if( type == null ) type = viewClass;
 		var v : Dynamic = try haxe.Json.parse(nw.Clipboard.get().get()) catch( e : Dynamic ) null;
+		if( v != null && opts != null ) opts.ref = v.opts;
 		return v == null || v.type != type ? null : v.value;
 	}