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.

153 lines
4.0 KiB

  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. public class AdditionalSurfaceOptionsHelper
  9. {
  10. private const string AdditionalOptionsStr = " Additional Surface Options";
  11. private const float ShaderKeywordButtonLayoutWidth = 15;
  12. private ParentNode m_currentOwner;
  13. [SerializeField]
  14. private List<string> m_availableOptions = new List<string>();
  15. public void Draw( ParentNode owner )
  16. {
  17. m_currentOwner = owner;
  18. bool value = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedAdditionalSurfaceOptions;
  19. NodeUtils.DrawPropertyGroup( ref value, AdditionalOptionsStr, DrawMainBody, DrawButtons );
  20. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedAdditionalSurfaceOptions = value;
  21. }
  22. void DrawButtons()
  23. {
  24. EditorGUILayout.Separator();
  25. // Add tag
  26. if( GUILayout.Button( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  27. {
  28. m_availableOptions.Add( string.Empty );
  29. EditorGUI.FocusTextInControl( null );
  30. }
  31. //Remove tag
  32. if( GUILayout.Button( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  33. {
  34. if( m_availableOptions.Count > 0 )
  35. {
  36. m_availableOptions.RemoveAt( m_availableOptions.Count - 1 );
  37. EditorGUI.FocusTextInControl( null );
  38. }
  39. }
  40. }
  41. void DrawMainBody()
  42. {
  43. EditorGUILayout.Separator();
  44. int itemCount = m_availableOptions.Count;
  45. if( itemCount == 0 )
  46. {
  47. EditorGUILayout.HelpBox( "Your list is Empty!\nUse the plus button to add one.", MessageType.Info );
  48. }
  49. int markedToDelete = -1;
  50. float originalLabelWidth = EditorGUIUtility.labelWidth;
  51. for( int i = 0; i < itemCount; i++ )
  52. {
  53. EditorGUI.indentLevel += 1;
  54. EditorGUIUtility.labelWidth = 62;
  55. EditorGUILayout.BeginHorizontal();
  56. //Option
  57. EditorGUI.BeginChangeCheck();
  58. m_availableOptions[ i ] = EditorGUILayout.TextField( "["+i+"] -", m_availableOptions[ i ] );
  59. if( EditorGUI.EndChangeCheck() )
  60. {
  61. m_availableOptions[ i ] = UIUtils.RemoveShaderInvalidCharacters( m_availableOptions[ i ] );
  62. }
  63. EditorGUIUtility.labelWidth = originalLabelWidth;
  64. {
  65. // Add new port
  66. if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  67. {
  68. m_availableOptions.Insert( i + 1, string.Empty );
  69. EditorGUI.FocusTextInControl( null );
  70. }
  71. //Remove port
  72. if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  73. {
  74. markedToDelete = i;
  75. }
  76. }
  77. EditorGUILayout.EndHorizontal();
  78. EditorGUI.indentLevel -= 1;
  79. }
  80. if( markedToDelete > -1 )
  81. {
  82. if( m_availableOptions.Count > markedToDelete )
  83. {
  84. m_availableOptions.RemoveAt( markedToDelete );
  85. EditorGUI.FocusTextInControl( null );
  86. }
  87. }
  88. EditorGUILayout.Separator();
  89. }
  90. public void ReadFromString( ref uint index, ref string[] nodeParams )
  91. {
  92. int count = Convert.ToInt32( nodeParams[ index++ ] );
  93. for( int i = 0; i < count; i++ )
  94. {
  95. m_availableOptions.Add( nodeParams[ index++ ] );
  96. }
  97. }
  98. public void WriteToString( ref string nodeInfo )
  99. {
  100. int optionsCount = m_availableOptions.Count;
  101. IOUtils.AddFieldValueToString( ref nodeInfo, optionsCount );
  102. for( int i = 0; i < optionsCount; i++ )
  103. {
  104. IOUtils.AddFieldValueToString( ref nodeInfo, m_availableOptions[ i ].ToString() );
  105. }
  106. }
  107. public void WriteToOptionalSurfaceOptions( ref string currentOptions)
  108. {
  109. int tagsCount = m_availableOptions.Count;
  110. if( tagsCount == 0 )
  111. return;
  112. string result = " ";
  113. for( int i = 0; i < tagsCount; i++ )
  114. {
  115. result += m_availableOptions[ i ];
  116. if( i < tagsCount - 1 )
  117. {
  118. result += " ";
  119. }
  120. }
  121. currentOptions = currentOptions + result;
  122. }
  123. public void Destroy()
  124. {
  125. m_availableOptions.Clear();
  126. m_availableOptions = null;
  127. m_currentOwner = null;
  128. }
  129. }
  130. }