A game about forced loneliness, made by TACStudios
at master 128 lines 3.9 kB view raw
1using System; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.Rendering; 5 6namespace UnityEditor.Rendering 7{ 8 using IProvider = FilterWindow.IProvider; 9 using Element = FilterWindow.Element; 10 using GroupElement = FilterWindow.GroupElement; 11 12 class VolumeComponentProvider : IProvider 13 { 14 class VolumeComponentElement : Element 15 { 16 public Type type; 17 18 public VolumeComponentElement(int level, string label, Type type) 19 { 20 this.level = level; 21 this.type = type; 22 // TODO: Add support for custom icons 23 content = new GUIContent(label); 24 } 25 } 26 27 class PathNode : IComparable<PathNode> 28 { 29 public List<PathNode> nodes = new List<PathNode>(); 30 public string name; 31 public Type type; 32 33 public int CompareTo(PathNode other) 34 { 35 return name.CompareTo(other.name); 36 } 37 } 38 39 public Vector2 position { get; set; } 40 41 VolumeProfile m_Target; 42 VolumeComponentListEditor m_TargetEditor; 43 44 public VolumeComponentProvider(VolumeProfile target, VolumeComponentListEditor targetEditor) 45 { 46 m_Target = target; 47 m_TargetEditor = targetEditor; 48 } 49 50 public void CreateComponentTree(List<Element> tree) 51 { 52 var currentPipelineAssetType = GraphicsSettings.currentRenderPipelineAssetType; 53 if (currentPipelineAssetType == null) 54 { 55 tree.Add(new GroupElement(0, "No SRP in use")); 56 return; 57 } 58 59 tree.Add(new GroupElement(0, "Volume Overrides")); 60 61 var volumeComponentTypesFiltered = VolumeManager.instance.GetVolumeComponentsForDisplay(currentPipelineAssetType); 62 if (volumeComponentTypesFiltered.Count == 0) 63 return; 64 65 var rootNode = new PathNode(); 66 foreach (var (path, t) in volumeComponentTypesFiltered) 67 { 68 // Skip components that have already been added to the volume 69 if (m_Target.Has(t)) 70 continue; 71 72 // Prep the categories & types tree 73 AddNode(rootNode, path, t); 74 } 75 76 // Recursively add all elements to the tree 77 Traverse(rootNode, 1, tree); 78 } 79 80 public bool GoToChild(Element element, bool addIfComponent) 81 { 82 if (element is VolumeComponentElement volumeComponentElement) 83 { 84 m_TargetEditor.AddComponent(volumeComponentElement.type); 85 return true; 86 } 87 88 return false; 89 } 90 91 void AddNode(PathNode root, string path, Type type) 92 { 93 var current = root; 94 var parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); 95 96 foreach (var part in parts) 97 { 98 var child = current.nodes.Find(x => x.name == part); 99 100 if (child == null) 101 { 102 child = new PathNode { name = part, type = type }; 103 current.nodes.Add(child); 104 } 105 106 current = child; 107 } 108 } 109 110 void Traverse(PathNode node, int depth, List<Element> tree) 111 { 112 node.nodes.Sort(); 113 114 foreach (var n in node.nodes) 115 { 116 if (n.nodes.Count > 0) // Group 117 { 118 tree.Add(new GroupElement(depth, n.name)); 119 Traverse(n, depth + 1, tree); 120 } 121 else // Element 122 { 123 tree.Add(new VolumeComponentElement(depth, n.name, n.type)); 124 } 125 } 126 } 127 } 128}