浏览代码

Make reference names editable throught text field and add reference name sanitation

/main
Jens Holm 7 年前
当前提交
42451460
共有 4 个文件被更改,包括 70 次插入45 次删除
  1. 35
      com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 37
      com.unity.shadergraph/Editor/Data/Graphs/AbstractShaderProperty.cs
  3. 2
      com.unity.shadergraph/Editor/Data/Graphs/IShaderProperty.cs
  4. 41
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardFieldPropertyView.cs

35
com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs


public string SanitizePropertyReferenceName(string referenceName)
{
referenceName = referenceName.Trim();
referenceName = Regex.Replace(referenceName, @"\s+", "_");
referenceName = Regex.Replace(referenceName, @"^[^A-Za-z_]", "_");
referenceName = Regex.Replace(referenceName, @"[^A-Za-z_0-9]", "_");
// Similat to teh property names, duplicate names are handled by appending `_n`
// to the end of the referene name.
if (m_Properties.Any(p => p.referenceName == referenceName && p.guid != guid))
{
// Strip out the "_n" part of the name.
var baseRegex = new Regex(@"^(.*)_(\d+)$");
var baseMatch = baseRegex.Match(referenceName);
if (baseMatch.Success)
referenceName = baseMatch.Groups[1].Value;
var regex = new Regex(@"^" + Regex.Escape(referenceName) + @"_(\d+)$");
var existingDuplicateNumbers = m_Properties.Where(p => p.guid != guid).Select(p => regex.Match(p.referenceName)).Where(m => m.Success).Select(m => int.Parse(m.Groups[1].Value)).Where(n => n > 0).Distinct().ToList();
var duplicateNumber = 1;
existingDuplicateNumbers.Sort();
if (existingDuplicateNumbers.Any() && existingDuplicateNumbers.First() == 1)
{
duplicateNumber = existingDuplicateNumbers.Last() + 1;
for (var i = 1; i < existingDuplicateNumbers.Count; i++)
{
if (existingDuplicateNumbers[i - 1] != existingDuplicateNumbers[i] - 1)
{
duplicateNumber = existingDuplicateNumbers[i - 1] + 1;
break;
}
}
}
referenceName = string.Format("{0}_{1}", referenceName, duplicateNumber);
}
return referenceName;
}

37
com.unity.shadergraph/Editor/Data/Graphs/AbstractShaderProperty.cs


private string m_Name;
[SerializeField]
private string m_CustomReferenceName;
[SerializeField]
[SerializeField]
private bool m_UseCustomReferenceName = false;
[SerializeField]
private SerializableGuid m_Guid = new SerializableGuid();

set { m_Name = value; }
}
public string customReferenceName
{
get
{
if (string.IsNullOrEmpty(m_CustomReferenceName))
m_CustomReferenceName = "_" + displayName;
return m_CustomReferenceName;
}
set { m_CustomReferenceName = value; }
}
string m_DefaultReferenceName;
public string referenceName

if (useCustomReferenceName)
{
return customReferenceName;
}
if (string.IsNullOrEmpty(overrideReferenceName))
{
if (string.IsNullOrEmpty(m_DefaultReferenceName))

}
}
public string overrideReferenceName { get; set; }
[SerializeField]
string m_OverrideReferenceName;
public string overrideReferenceName
{
get { return m_OverrideReferenceName; }
set { m_OverrideReferenceName = value; }
}
public abstract PropertyType propertyType { get; }

{
get { return m_GeneratePropertyBlock; }
set { m_GeneratePropertyBlock = value; }
}
public bool useCustomReferenceName
{
get { return m_UseCustomReferenceName; }
set { m_UseCustomReferenceName = value; }
}
public abstract Vector4 defaultValue { get; }

2
com.unity.shadergraph/Editor/Data/Graphs/IShaderProperty.cs


PropertyType propertyType { get; }
Guid guid { get; }
bool generatePropertyBlock { get; set; }
bool useCustomReferenceName { get; set; }
string customReferenceName { get; set; }
Vector4 defaultValue { get; }
string overrideReferenceName { get; set; }

41
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardFieldPropertyView.cs


using System;
using System.Linq;
using UnityEditor.Graphing.Util;
using UnityEngine.Experimental.UIElements.StyleSheets;
using Toggle = UnityEngine.Experimental.UIElements.Toggle;
namespace UnityEditor.ShaderGraph.Drawing

readonly AbstractMaterialGraph m_Graph;
Toggle m_UseCustomReferenceNameToggle;
IShaderProperty m_Property;
static Type s_ContextualMenuManipulator = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypesOrNothing()).FirstOrDefault(t => t.FullName == "UnityEngine.Experimental.UIElements.ContextualMenuManipulator");
m_Property = property;
m_UseCustomReferenceNameToggle = new Toggle(() =>
{
property.useCustomReferenceName = m_UseCustomReferenceNameToggle.on;
m_ReferenceNameField.SetEnabled(property.useCustomReferenceName);
m_ReferenceNameField.value = property.referenceName;
DirtyNodes(ModificationScope.Graph);
});
m_UseCustomReferenceNameToggle.on = property.useCustomReferenceName;
m_ReferenceNameField.SetEnabled(property.useCustomReferenceName);
if (!property.useCustomReferenceName)
return;
property.customReferenceName = newName.newValue;
string newReferenceName = m_Graph.SanitizePropertyReferenceName(newName.newValue);
property.overrideReferenceName = newReferenceName;
m_ReferenceNameField.value = newReferenceName;
m_ReferenceNameField.style.fontStyle = StyleValue<FontStyle>.Create(FontStyle.Bold);
AddRow("Specify reference name", m_UseCustomReferenceNameToggle);
if (!string.IsNullOrEmpty(property.overrideReferenceName))
m_ReferenceNameField.style.fontStyle = StyleValue<FontStyle>.Create(FontStyle.Bold);
if (property is Vector1ShaderProperty)
{

AddToClassList("sgblackboardFieldPropertyView");
this.AddManipulator((IManipulator)Activator.CreateInstance(s_ContextualMenuManipulator, (Action<ContextualMenuPopulateEvent>)BuildContextualMenu));
}
void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
evt.menu.AppendAction("Reset reference name", e =>
{
m_Property.overrideReferenceName = null;
m_ReferenceNameField.value = m_Property.referenceName;
m_ReferenceNameField.style.fontStyle = StyleValue<FontStyle>.Create(FontStyle.Normal);
DirtyNodes(ModificationScope.Graph);
}, ContextualMenu.MenuAction.AlwaysEnabled);
}
VisualElement AddRow(string labelText, VisualElement control)

正在加载...
取消
保存