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.

141 lines
4.1 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. public class AdditionalPragmasHelper
  11. {
  12. private const string AdditionalPragmasStr = " Additional Pragmas";
  13. private const float ShaderKeywordButtonLayoutWidth = 15;
  14. private ParentNode m_currentOwner;
  15. [SerializeField]
  16. private List<string> m_additionalPragmas = new List<string>();
  17. public List<string> PragmaList { get { return m_additionalPragmas; } set { m_additionalPragmas = value; } }
  18. [SerializeField]
  19. private List<string> m_outsidePragmas = new List<string>();
  20. public List<string> OutsideList { get { return m_outsidePragmas; } set { m_outsidePragmas = value; } }
  21. public void Draw( ParentNode owner )
  22. {
  23. m_currentOwner = owner;
  24. bool value = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedAdditionalPragmas;
  25. NodeUtils.DrawPropertyGroup( ref value, AdditionalPragmasStr, DrawMainBody, DrawButtons );
  26. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedAdditionalPragmas = value;
  27. }
  28. void DrawButtons()
  29. {
  30. EditorGUILayout.Separator();
  31. // Add keyword
  32. if( GUILayout.Button( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  33. {
  34. m_additionalPragmas.Add( string.Empty );
  35. EditorGUI.FocusTextInControl( null );
  36. }
  37. //Remove keyword
  38. if( GUILayout.Button( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  39. {
  40. if( m_additionalPragmas.Count > 0 )
  41. {
  42. m_additionalPragmas.RemoveAt( m_additionalPragmas.Count - 1 );
  43. EditorGUI.FocusTextInControl( null );
  44. }
  45. }
  46. }
  47. void DrawMainBody()
  48. {
  49. EditorGUILayout.Separator();
  50. int itemCount = m_additionalPragmas.Count;
  51. int markedToDelete = -1;
  52. for( int i = 0; i < itemCount; i++ )
  53. {
  54. EditorGUILayout.BeginHorizontal();
  55. {
  56. EditorGUI.BeginChangeCheck();
  57. m_additionalPragmas[ i ] = EditorGUILayout.TextField( m_additionalPragmas[ i ] );
  58. if( EditorGUI.EndChangeCheck() )
  59. {
  60. m_additionalPragmas[ i ] = UIUtils.RemoveShaderInvalidCharacters( m_additionalPragmas[ i ] );
  61. }
  62. // Add new port
  63. if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  64. {
  65. m_additionalPragmas.Insert( i + 1, string.Empty );
  66. EditorGUI.FocusTextInControl( null );
  67. }
  68. //Remove port
  69. if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  70. {
  71. markedToDelete = i;
  72. }
  73. }
  74. EditorGUILayout.EndHorizontal();
  75. }
  76. if( markedToDelete > -1 )
  77. {
  78. if( m_additionalPragmas.Count > markedToDelete )
  79. {
  80. m_additionalPragmas.RemoveAt( markedToDelete );
  81. EditorGUI.FocusTextInControl( null );
  82. }
  83. }
  84. EditorGUILayout.Separator();
  85. EditorGUILayout.HelpBox( "Please add your pragmas without the #pragma keywords", MessageType.Info );
  86. }
  87. public void ReadFromString( ref uint index, ref string[] nodeParams )
  88. {
  89. int count = Convert.ToInt32( nodeParams[ index++ ] );
  90. for( int i = 0; i < count; i++ )
  91. {
  92. m_additionalPragmas.Add( nodeParams[ index++ ] );
  93. }
  94. }
  95. public void WriteToString( ref string nodeInfo )
  96. {
  97. IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalPragmas.Count );
  98. for( int i = 0; i < m_additionalPragmas.Count; i++ )
  99. {
  100. IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalPragmas[ i ] );
  101. }
  102. }
  103. public void AddToDataCollector( ref MasterNodeDataCollector dataCollector )
  104. {
  105. for( int i = 0; i < m_additionalPragmas.Count; i++ )
  106. {
  107. if( !string.IsNullOrEmpty( m_additionalPragmas[ i ] ) )
  108. dataCollector.AddToPragmas( -1, m_additionalPragmas[ i ] );
  109. }
  110. for( int i = 0; i < m_outsidePragmas.Count; i++ )
  111. {
  112. if( !string.IsNullOrEmpty( m_outsidePragmas[ i ] ) )
  113. dataCollector.AddToPragmas( -1, m_outsidePragmas[ i ] );
  114. }
  115. }
  116. public void Destroy()
  117. {
  118. m_additionalPragmas.Clear();
  119. m_additionalPragmas = null;
  120. m_currentOwner = null;
  121. }
  122. }
  123. }