123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Rendering;
- namespace UnityEditor.Rendering
- {
- using IProvider = FilterWindow.IProvider;
- using Element = FilterWindow.Element;
- using GroupElement = FilterWindow.GroupElement;
- class VolumeComponentProvider : IProvider
- {
- class VolumeComponentElement : Element
- {
- public Type type;
- public VolumeComponentElement(int level, string label, Type type)
- {
- this.level = level;
- this.type = type;
- // TODO: Add support for custom icons
- content = new GUIContent(label);
- }
- }
- class PathNode : IComparable<PathNode>
- {
- public List<PathNode> nodes = new List<PathNode>();
- public string name;
- public Type type;
- public int CompareTo(PathNode other)
- {
- return name.CompareTo(other.name);
- }
- }
- public Vector2 position { get; set; }
- VolumeProfile m_Target;
- VolumeComponentListEditor m_TargetEditor;
- public VolumeComponentProvider(VolumeProfile target, VolumeComponentListEditor targetEditor)
- {
- m_Target = target;
- m_TargetEditor = targetEditor;
- }
- public void CreateComponentTree(List<Element> tree)
- {
- tree.Add(new GroupElement(0, "Volume Overrides"));
- var types = VolumeManager.instance.baseComponentTypeArray;
- var rootNode = new PathNode();
- foreach (var t in types)
- {
- // Skip components that have already been added to the volume
- if (m_Target.Has(t))
- continue;
- string path = string.Empty;
- // Look for a VolumeComponentMenu attribute
- var attrs = t.GetCustomAttributes(false);
- bool skipComponent = false;
- foreach (var attr in attrs)
- {
- var attrMenu = attr as VolumeComponentMenu;
- if (attrMenu != null)
- path = attrMenu.menu;
- var attrDeprecated = attr as VolumeComponentDeprecated;
- if (attrDeprecated != null)
- skipComponent = true;
- }
- if (skipComponent)
- continue;
- // If no attribute or in case something went wrong when grabbing it, fallback to a
- // beautified class name
- if (string.IsNullOrEmpty(path))
- path = ObjectNames.NicifyVariableName(t.Name);
- // Prep the categories & types tree
- AddNode(rootNode, path, t);
- }
- // Recursively add all elements to the tree
- Traverse(rootNode, 1, tree);
- }
- public bool GoToChild(Element element, bool addIfComponent)
- {
- if (element is VolumeComponentElement)
- {
- var e = (VolumeComponentElement)element;
- m_TargetEditor.AddComponent(e.type);
- return true;
- }
- return false;
- }
- void AddNode(PathNode root, string path, Type type)
- {
- var current = root;
- var parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var part in parts)
- {
- var child = current.nodes.Find(x => x.name == part);
- if (child == null)
- {
- child = new PathNode { name = part, type = type };
- current.nodes.Add(child);
- }
- current = child;
- }
- }
- void Traverse(PathNode node, int depth, List<Element> tree)
- {
- node.nodes.Sort();
- foreach (var n in node.nodes)
- {
- if (n.nodes.Count > 0) // Group
- {
- tree.Add(new GroupElement(depth, n.name));
- Traverse(n, depth + 1, tree);
- }
- else // Element
- {
- tree.Add(new VolumeComponentElement(depth, n.name, n.type));
- }
- }
- }
- }
- }
|