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.

210 lines
7.8 KiB

  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. public class DependenciesData
  9. {
  10. private string DependencyFormat = "Dependency \"{0}\"=\"{1}\"\n";
  11. public string DependencyName;
  12. public string DependencyValue;
  13. public bool DependencyFoldout = true;
  14. public DependenciesData()
  15. {
  16. DependencyName = string.Empty;
  17. DependencyValue = string.Empty;
  18. }
  19. public DependenciesData( string data )
  20. {
  21. string[] arr = data.Split( IOUtils.VALUE_SEPARATOR );
  22. if( arr.Length > 1 )
  23. {
  24. DependencyName = arr[ 0 ];
  25. DependencyValue = arr[ 1 ];
  26. }
  27. }
  28. public override string ToString()
  29. {
  30. return DependencyName + IOUtils.VALUE_SEPARATOR + DependencyValue;
  31. }
  32. public string GenerateDependency()
  33. {
  34. return string.Format( DependencyFormat, DependencyName, DependencyValue );
  35. }
  36. public bool IsValid { get { return ( !string.IsNullOrEmpty( DependencyValue ) && !string.IsNullOrEmpty( DependencyName ) ); } }
  37. }
  38. [Serializable]
  39. public class DependenciesHelper
  40. {
  41. private const string CustomDependencysStr = " Dependencies";
  42. private const string DependencyNameStr = "Name";
  43. private const string DependencyValueStr = "Value";
  44. private const float ShaderKeywordButtonLayoutWidth = 15;
  45. private ParentNode m_currentOwner;
  46. [SerializeField]
  47. private List<DependenciesData> m_availableDependencies = new List<DependenciesData>();
  48. public void Draw( ParentNode owner, bool isNested = false )
  49. {
  50. m_currentOwner = owner;
  51. bool value = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDependencies;
  52. if( isNested )
  53. {
  54. NodeUtils.DrawNestedPropertyGroup( ref value, CustomDependencysStr, DrawMainBody, DrawButtons );
  55. }
  56. else
  57. {
  58. NodeUtils.DrawPropertyGroup( ref value, CustomDependencysStr, DrawMainBody, DrawButtons );
  59. }
  60. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDependencies = value;
  61. }
  62. void DrawButtons()
  63. {
  64. EditorGUILayout.Separator();
  65. // Add Dependency
  66. if( GUILayout.Button( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  67. {
  68. m_availableDependencies.Add( new DependenciesData() );
  69. EditorGUI.FocusTextInControl( null );
  70. }
  71. //Remove Dependency
  72. if( GUILayout.Button( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  73. {
  74. if( m_availableDependencies.Count > 0 )
  75. {
  76. m_availableDependencies.RemoveAt( m_availableDependencies.Count - 1 );
  77. EditorGUI.FocusTextInControl( null );
  78. }
  79. }
  80. }
  81. void DrawMainBody()
  82. {
  83. EditorGUILayout.Separator();
  84. int itemCount = m_availableDependencies.Count;
  85. if( itemCount == 0 )
  86. {
  87. EditorGUILayout.HelpBox( "Your list is Empty!\nUse the plus button to add one.", MessageType.Info );
  88. }
  89. int markedToDelete = -1;
  90. float originalLabelWidth = EditorGUIUtility.labelWidth;
  91. for( int i = 0; i < itemCount; i++ )
  92. {
  93. m_availableDependencies[ i ].DependencyFoldout = m_currentOwner.EditorGUILayoutFoldout( m_availableDependencies[ i ].DependencyFoldout, string.Format( "[{0}] - {1}", i, m_availableDependencies[ i ].DependencyName ) );
  94. if( m_availableDependencies[ i ].DependencyFoldout )
  95. {
  96. EditorGUI.indentLevel += 1;
  97. EditorGUIUtility.labelWidth = 70;
  98. //Dependency Name
  99. EditorGUI.BeginChangeCheck();
  100. m_availableDependencies[ i ].DependencyName = EditorGUILayout.TextField( DependencyNameStr, m_availableDependencies[ i ].DependencyName );
  101. if( EditorGUI.EndChangeCheck() )
  102. {
  103. m_availableDependencies[ i ].DependencyName = UIUtils.RemoveShaderInvalidCharacters( m_availableDependencies[ i ].DependencyName );
  104. }
  105. //Dependency Value
  106. EditorGUI.BeginChangeCheck();
  107. m_availableDependencies[ i ].DependencyValue = EditorGUILayout.TextField( DependencyValueStr, m_availableDependencies[ i ].DependencyValue );
  108. if( EditorGUI.EndChangeCheck() )
  109. {
  110. m_availableDependencies[ i ].DependencyValue = UIUtils.RemoveShaderInvalidCharacters( m_availableDependencies[ i ].DependencyValue );
  111. }
  112. EditorGUIUtility.labelWidth = originalLabelWidth;
  113. EditorGUILayout.BeginHorizontal();
  114. {
  115. GUILayout.Label( " " );
  116. // Add new port
  117. if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  118. {
  119. m_availableDependencies.Insert( i + 1, new DependenciesData() );
  120. EditorGUI.FocusTextInControl( null );
  121. }
  122. //Remove port
  123. if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  124. {
  125. markedToDelete = i;
  126. }
  127. }
  128. EditorGUILayout.EndHorizontal();
  129. EditorGUI.indentLevel -= 1;
  130. }
  131. }
  132. if( markedToDelete > -1 )
  133. {
  134. if( m_availableDependencies.Count > markedToDelete )
  135. {
  136. m_availableDependencies.RemoveAt( markedToDelete );
  137. EditorGUI.FocusTextInControl( null );
  138. }
  139. }
  140. EditorGUILayout.Separator();
  141. }
  142. public void ReadFromString( ref uint index, ref string[] nodeParams )
  143. {
  144. int count = Convert.ToInt32( nodeParams[ index++ ] );
  145. for( int i = 0; i < count; i++ )
  146. {
  147. m_availableDependencies.Add( new DependenciesData( nodeParams[ index++ ] ) );
  148. }
  149. }
  150. public void WriteToString( ref string nodeInfo )
  151. {
  152. int dependencyCount = m_availableDependencies.Count;
  153. IOUtils.AddFieldValueToString( ref nodeInfo, dependencyCount );
  154. for( int i = 0; i < dependencyCount; i++ )
  155. {
  156. IOUtils.AddFieldValueToString( ref nodeInfo, m_availableDependencies[ i ].ToString() );
  157. }
  158. }
  159. public string GenerateDependencies()
  160. {
  161. int dependencyCount = m_availableDependencies.Count;
  162. string result = dependencyCount == 0 ? string.Empty : "\n";
  163. UIUtils.ShaderIndentLevel++;
  164. for( int i = 0; i < dependencyCount; i++ )
  165. {
  166. if( m_availableDependencies[ i ].IsValid )
  167. {
  168. result += UIUtils.ShaderIndentTabs + m_availableDependencies[ i ].GenerateDependency();
  169. }
  170. }
  171. UIUtils.ShaderIndentLevel--;
  172. return result;
  173. }
  174. public void Destroy()
  175. {
  176. m_availableDependencies.Clear();
  177. m_availableDependencies = null;
  178. m_currentOwner = null;
  179. }
  180. public bool HasDependencies { get { return m_availableDependencies.Count > 0; } }
  181. }
  182. }