Просмотр исходного кода

Add shadergraphs to the New... sceneedditor context menu

The `fx.shaders` prop can now contain directories and .hlshader files
Note that Shaders are not added to new -> recents
Also note that ShagerGraph is still registered as "hlshader" for a pname
as changing it would break any existing l3d/prefab scene that uses it
Leonardo Jeanteur 4 лет назад
Родитель
Сommit
fa2fc77658

+ 2 - 1
bin/defaultProps.json

@@ -140,7 +140,8 @@
 		"h3d.shader.ColorAdd",
 		"h3d.shader.ColorMult",
 		"h3d.shader.UVDelta",
-		"h3d.shader.UVScroll"
+		"h3d.shader.UVScroll",
+		"shaders/"
 	],
 
 	"fx.tags": [ { "id": "tag", "color": "#802000" } ],

+ 81 - 32
hide/comp/SceneEditor.hx

@@ -2298,6 +2298,8 @@ class SceneEditor {
 				newItems.push(getNewShaderMenu(parent, onMake));
 				continue;
 			}
+			if(ptype == "hlshader")
+				continue;
 			var m = getNewTypeMenuItem(ptype, parent, onMake);
 			if( !groupByType )
 				newItems.push(m);
@@ -2331,17 +2333,27 @@ class SceneEditor {
 		return newItems;
 	}
 
-	function getNewTypeMenuItem(ptype: String, parent: PrefabElement, onMake: PrefabElement->Void, ?label: String) : hide.comp.ContextMenu.ContextMenuItem {
+	function getNewTypeMenuItem(
+		ptype: String,
+		parent: PrefabElement,
+		onMake: PrefabElement->Void,
+		?label: String,
+		?objectName: String,
+		?path: String
+	) : hide.comp.ContextMenu.ContextMenuItem {
 		var pmodel = hrt.prefab.Library.getRegistered().get(ptype);
 		return {
 			label : label != null ? label : pmodel.inf.name,
 			click : function() {
-				function make(?path) {
+				function make(?sourcePath) {
 					var p = Type.createInstance(pmodel.cl, [parent]);
 					@:privateAccess p.type = ptype;
-					if(path != null)
-						p.source = path;
-					autoName(p);
+					if(sourcePath != null)
+						p.source = sourcePath;
+					if( objectName != null)
+						p.name = objectName;
+					else
+						autoName(p);
 					if(onMake != null)
 						onMake(p);
 					var recents : Array<String> = ide.currentConfig.get("sceneeditor.newrecents", []);
@@ -2353,17 +2365,18 @@ class SceneEditor {
 					return p;
 				}
 
-				if( pmodel.inf.fileSource != null )
-					ide.chooseFile(pmodel.inf.fileSource, function(path) {
+				if( pmodel.inf.fileSource != null ) {
+					if( path != null ) {
 						var p = make(path);
 						addElements([p]);
 						var recents : Array<String> = ide.currentConfig.get("sceneeditor.newrecents", []);
 						recents.remove(p.type);
-						recents.unshift(p.type);
-						var recentSize : Int = view.config.get("sceneeditor.recentsize");
-						if (recents.length > recentSize) recents.splice(recentSize, recents.length - recentSize);
-						ide.currentConfig.set("sceneeditor.newrecents", recents);
-					});
+					} else {
+						ide.chooseFile(pmodel.inf.fileSource, function(path) {
+							addElements([make(path)]);
+						});
+					}
+				}
 				else
 					addElements([make()]);
 			},
@@ -2371,38 +2384,74 @@ class SceneEditor {
 		};
 	}
 
-	function getNewShaderMenu(parentElt: PrefabElement, onMake: PrefabElement->Void) : hide.comp.ContextMenu.ContextMenuItem {
-		var custom = getNewTypeMenuItem("shader", parentElt, onMake, "Custom...");
-
-		function shaderItem(name, path) : hide.comp.ContextMenu.ContextMenuItem {
-			return {
-				label : name,
-				click : function() {
-					var s = new hrt.prefab.DynamicShader(parentElt);
-					s.source = path;
-					s.name = name;
-					addElements([s]);
-				}
-			}
+	function getNewShaderMenu(parentElt: PrefabElement, ?onMake: PrefabElement->Void) : hide.comp.ContextMenu.ContextMenuItem {
+		function isClassShader(path) {
+			if(StringTools.endsWith(path, ".hx")) path = path.substr(0, -3);
+			var cpath = path.split("/").join(".");
+			var cl = cast Type.resolveClass(cpath);
+			return cl != null;
 		}
 
-		var menu = [custom];
+		var shModel = hrt.prefab.Library.getRegistered().get("shader");
+		var graphModel = hrt.prefab.Library.getRegistered().get("hlshader");
+		var custom = {
+			label : "Custom...",
+			click : function() {
+				ide.chooseFile(shModel.inf.fileSource.concat(graphModel.inf.fileSource), function(path) {
+					var cl = isClassShader(path) ? shModel.cl : graphModel.cl;
+					var p = Type.createInstance(cl, [parentElt]);
+					p.source = path;
+					autoName(p);
+					if(onMake != null)
+						onMake(p);
+					addElements([p]);
+				});
+			},
+			icon : shModel.inf.icon,
+		};
 
-		var shaders : Array<String> = hide.Ide.inst.currentConfig.get("fx.shaders", []);
-		for(path in shaders) {
+		function classShaderItem(path) : hide.comp.ContextMenu.ContextMenuItem {
 			var name = path;
 			if(StringTools.endsWith(name, ".hx")) {
-				name = name.substr(0, -3);
-				name = name.split("/").pop();
+				name = new haxe.io.Path(path).file;
 			}
 			else {
 				name = name.split(".").pop();
 			}
-			menu.push(shaderItem(name, path));
+			return getNewTypeMenuItem("shader", parentElt, onMake, name, name, path);
+		}
+
+		function graphShaderItem(path) : hide.comp.ContextMenu.ContextMenuItem {
+			var name = new haxe.io.Path(path).file;
+			return getNewTypeMenuItem("hlshader", parentElt, onMake, name, name, path);
+		}
+
+		var menu : Array<hide.comp.ContextMenu.ContextMenuItem> = [];
+
+		var shaders : Array<String> = hide.Ide.inst.currentConfig.get("fx.shaders", []);
+		for(path in shaders) {
+			var fullPath = ide.getPath(path);
+			if( isClassShader(path) ) {
+				menu.push(classShaderItem(path));
+			} else if( StringTools.endsWith(path, ".hlshader")) {
+				menu.push(graphShaderItem(path));
+			} else if( sys.FileSystem.exists(fullPath) && sys.FileSystem.isDirectory(fullPath) ) {
+				for( c in sys.FileSystem.readDirectory(fullPath) ) {
+					var relPath = ide.makeRelative(fullPath + "/" + c);
+					if( isClassShader(relPath) ) {
+						menu.push(classShaderItem(relPath));
+					} else if( StringTools.endsWith(relPath, ".hlshader")) {
+						menu.push(graphShaderItem(relPath));
+					}
+				}
+			}
 		}
 
+		menu.sort(function(l1,l2) return Reflect.compare(l1.label,l2.label));
+		menu.unshift(custom);
+
 		return {
-			label: "Shaders",
+			label: "Shader",
 			menu: menu
 		};
 	}

+ 1 - 1
hide/view/shadereditor/ShaderEditor.hx

@@ -320,7 +320,7 @@ class ShaderEditor extends hide.view.Graph {
 		var libPaths : Array<String> = config.get("shadergraph.libfolders", ["shaders"]);
 		for( lpath in libPaths ) {
 			var basePath = ide.getPath(lpath);
-			if( !sys.FileSystem.isDirectory(basePath) )
+			if( !sys.FileSystem.exists(basePath) || !sys.FileSystem.isDirectory(basePath) )
 				continue;
 			for( c in sys.FileSystem.readDirectory(basePath) ) {
 				var relPath = ide.makeRelative(basePath + "/" + c);

+ 1 - 1
hrt/prefab/ShaderGraph.hx

@@ -26,7 +26,7 @@ class ShaderGraph extends DynamicShader {
 
 	#if editor
 	override function getHideProps() : HideProps {
-		return { icon : "cog", name : "Shader Graph", fileSource : ["hlshader"], allowParent : function(p) return p.to(Object2D) != null || p.to(Object3D) != null };
+		return { icon : "scribd", name : "Shader Graph", fileSource : ["hlshader"], allowParent : function(p) return p.to(Object2D) != null || p.to(Object3D) != null };
 	}
 
 	override function edit( ctx : EditContext ) {