暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HlslFunctionView.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. using UnityEditor.UIElements;
  6. using UnityEditor.Graphing;
  7. namespace UnityEditor.ShaderGraph.Drawing
  8. {
  9. [Serializable]
  10. public enum HlslSourceType { File, String };
  11. internal class HlslFunctionView : VisualElement
  12. {
  13. private EnumField m_Type;
  14. private TextField m_FunctionName;
  15. private ObjectField m_FunctionSource;
  16. private TextField m_FunctionBody;
  17. internal HlslFunctionView(CustomFunctionNode node)
  18. {
  19. styleSheets.Add(Resources.Load<StyleSheet>("Styles/HlslFunctionView"));
  20. Draw(node);
  21. }
  22. private void Draw(CustomFunctionNode node)
  23. {
  24. var currentControls = this.Children().ToArray();
  25. for (int i = 0; i < currentControls.Length; i++)
  26. currentControls[i].RemoveFromHierarchy();
  27. m_Type = new EnumField(node.sourceType);
  28. m_Type.RegisterValueChangedCallback(s =>
  29. {
  30. if ((HlslSourceType)s.newValue != node.sourceType)
  31. {
  32. node.owner.owner.RegisterCompleteObjectUndo("Change Function Type");
  33. node.sourceType = (HlslSourceType)s.newValue;
  34. Draw(node);
  35. node.owner.ClearErrorsForNode(node);
  36. node.ValidateNode();
  37. node.Dirty(ModificationScope.Graph);
  38. }
  39. });
  40. m_FunctionName = new TextField { value = node.functionName, multiline = false };
  41. m_FunctionName.RegisterCallback<FocusInEvent>(s =>
  42. {
  43. if (m_FunctionName.value == CustomFunctionNode.defaultFunctionName)
  44. m_FunctionName.value = "";
  45. });
  46. m_FunctionName.RegisterCallback<FocusOutEvent>(s =>
  47. {
  48. if (m_FunctionName.value == "")
  49. m_FunctionName.value = CustomFunctionNode.defaultFunctionName;
  50. else
  51. m_FunctionName.value = NodeUtils.ConvertToValidHLSLIdentifier(m_FunctionName.value);
  52. if (m_FunctionName.value != node.functionName)
  53. {
  54. node.owner.owner.RegisterCompleteObjectUndo("Change Function Name");
  55. node.functionName = m_FunctionName.value;
  56. node.ValidateNode();
  57. node.Dirty(ModificationScope.Graph);
  58. }
  59. });
  60. string path = AssetDatabase.GUIDToAssetPath(node.functionSource);
  61. m_FunctionSource = new ObjectField() { value = AssetDatabase.LoadAssetAtPath<ShaderInclude>(path), objectType = typeof(ShaderInclude) };
  62. m_FunctionSource.RegisterValueChangedCallback(s =>
  63. {
  64. long localId;
  65. string guidString = string.Empty;
  66. if (s.newValue != null)
  67. {
  68. AssetDatabase.TryGetGUIDAndLocalFileIdentifier((ShaderInclude)s.newValue, out guidString, out localId);
  69. }
  70. if (guidString != node.functionSource)
  71. {
  72. node.owner.owner.RegisterCompleteObjectUndo("Change Function Source");
  73. node.functionSource = guidString;
  74. node.ValidateNode();
  75. node.Dirty(ModificationScope.Graph);
  76. }
  77. });
  78. m_FunctionBody = new TextField { value = node.functionBody, multiline = true };
  79. m_FunctionBody.RegisterCallback<FocusInEvent>(s =>
  80. {
  81. if (m_FunctionBody.value == CustomFunctionNode.defaultFunctionBody)
  82. m_FunctionBody.value = "";
  83. });
  84. m_FunctionBody.RegisterCallback<FocusOutEvent>(s =>
  85. {
  86. if (m_FunctionBody.value == "")
  87. m_FunctionBody.value = CustomFunctionNode.defaultFunctionBody;
  88. if (m_FunctionBody.value != node.functionBody)
  89. {
  90. node.owner.owner.RegisterCompleteObjectUndo("Change Function Body");
  91. node.functionBody = m_FunctionBody.value;
  92. node.ValidateNode();
  93. node.Dirty(ModificationScope.Graph);
  94. }
  95. });
  96. VisualElement typeRow = new VisualElement() { name = "Row" };
  97. {
  98. typeRow.Add(new Label("Type"));
  99. typeRow.Add(m_Type);
  100. }
  101. Add(typeRow);
  102. VisualElement nameRow = new VisualElement() { name = "Row" };
  103. {
  104. nameRow.Add(new Label("Name"));
  105. nameRow.Add(m_FunctionName);
  106. }
  107. Add(nameRow);
  108. switch (node.sourceType)
  109. {
  110. case HlslSourceType.File:
  111. VisualElement sourceRow = new VisualElement() { name = "Row" };
  112. {
  113. sourceRow.Add(new Label("Source"));
  114. sourceRow.Add(m_FunctionSource);
  115. }
  116. Add(sourceRow);
  117. break;
  118. case HlslSourceType.String:
  119. VisualElement bodyRow = new VisualElement() { name = "Row" };
  120. {
  121. bodyRow.Add(new Label("Body"));
  122. bodyRow.style.height = 200;
  123. bodyRow.Add(m_FunctionBody);
  124. }
  125. Add(bodyRow);
  126. break;
  127. }
  128. }
  129. }
  130. }