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.

193 lines
5.4 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 UnityEditor;
  6. using UnityEngine;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. public class TemplateAdditionalParentHelper : TemplateModuleParent
  11. {
  12. private string NativeFoldoutStr = "Native";
  13. protected string m_helpBoxMessage = string.Empty;
  14. private const float ShaderKeywordButtonLayoutWidth = 15;
  15. private ParentNode m_currentOwner;
  16. [SerializeField]
  17. protected List<string> m_nativeItems = new List<string>();
  18. [SerializeField]
  19. protected bool m_nativeItemsFoldout = false;
  20. [SerializeField]
  21. protected List<string> m_additionalItems = new List<string>();
  22. [SerializeField]
  23. protected List<string> m_outsideItems = new List<string>();
  24. public TemplateAdditionalParentHelper( string moduleName ) : base( moduleName ) { }
  25. public bool IsValid { set{ m_validData = value; } get{ return m_validData; } }
  26. public void FillNativeItems( List<string> nativeItems )
  27. {
  28. m_nativeItems.Clear();
  29. m_nativeItems.AddRange( nativeItems );
  30. }
  31. public void Draw( ParentNode owner )
  32. {
  33. m_currentOwner = owner;
  34. bool foldout = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedAdditionalDefines;
  35. NodeUtils.DrawNestedPropertyGroup( ref foldout, m_moduleName, DrawMainBody, DrawButtons );
  36. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedAdditionalDefines = foldout;
  37. }
  38. public void CopyFrom( TemplateAdditionalParentHelper other )
  39. {
  40. m_additionalItems.Clear();
  41. m_outsideItems.Clear();
  42. int otherAdditionalItemsCount = other.ItemsList.Count;
  43. for( int i = 0; i < otherAdditionalItemsCount; i++ )
  44. {
  45. m_additionalItems.Add( other.ItemsList[ i ] );
  46. }
  47. int otherOusideItemsCount = other.OutsideList.Count;
  48. for( int i = 0; i < otherOusideItemsCount; i++ )
  49. {
  50. m_outsideItems.Add( other.OutsideList[ i ] );
  51. }
  52. }
  53. void DrawButtons()
  54. {
  55. EditorGUILayout.Separator();
  56. // Add keyword
  57. if( GUILayout.Button( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  58. {
  59. m_additionalItems.Add( string.Empty );
  60. EditorGUI.FocusTextInControl( null );
  61. m_isDirty = true;
  62. }
  63. //Remove keyword
  64. if( GUILayout.Button( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  65. {
  66. if( m_additionalItems.Count > 0 )
  67. {
  68. m_additionalItems.RemoveAt( m_additionalItems.Count - 1 );
  69. EditorGUI.FocusTextInControl( null );
  70. }
  71. m_isDirty = true;
  72. }
  73. }
  74. void DrawNativeItems()
  75. {
  76. EditorGUILayout.Separator();
  77. EditorGUI.indentLevel++;
  78. int count = m_nativeItems.Count;
  79. for ( int i = 0; i < count; i++ )
  80. {
  81. EditorGUILayout.LabelField( m_nativeItems[i] );
  82. }
  83. EditorGUI.indentLevel--;
  84. EditorGUILayout.Separator();
  85. }
  86. void DrawMainBody()
  87. {
  88. EditorGUILayout.Separator();
  89. if( m_nativeItems.Count > 0 )
  90. {
  91. NodeUtils.DrawNestedPropertyGroup( ref m_nativeItemsFoldout, NativeFoldoutStr, DrawNativeItems ,4);
  92. }
  93. int itemCount = m_additionalItems.Count;
  94. int markedToDelete = -1;
  95. for( int i = 0; i < itemCount; i++ )
  96. {
  97. EditorGUILayout.BeginHorizontal();
  98. {
  99. EditorGUI.BeginChangeCheck();
  100. m_additionalItems[ i ] = EditorGUILayout.TextField( m_additionalItems[ i ] );
  101. if( EditorGUI.EndChangeCheck() )
  102. {
  103. m_additionalItems[ i ] = UIUtils.RemoveShaderInvalidCharacters( m_additionalItems[ i ] );
  104. m_isDirty = true;
  105. }
  106. // Add new port
  107. if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  108. {
  109. m_additionalItems.Insert( i + 1, string.Empty );
  110. EditorGUI.FocusTextInControl( null );
  111. m_isDirty = true;
  112. }
  113. //Remove port
  114. if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
  115. {
  116. markedToDelete = i;
  117. m_isDirty = true;
  118. }
  119. }
  120. EditorGUILayout.EndHorizontal();
  121. }
  122. if( markedToDelete > -1 )
  123. {
  124. if( m_additionalItems.Count > markedToDelete )
  125. {
  126. m_additionalItems.RemoveAt( markedToDelete );
  127. EditorGUI.FocusTextInControl( null );
  128. }
  129. }
  130. EditorGUILayout.Separator();
  131. EditorGUILayout.HelpBox( m_helpBoxMessage, MessageType.Info );
  132. }
  133. public override void ReadFromString( ref uint index, ref string[] nodeParams )
  134. {
  135. try
  136. {
  137. int count = Convert.ToInt32( nodeParams[ index++ ] );
  138. for( int i = 0; i < count; i++ )
  139. {
  140. m_additionalItems.Add( nodeParams[ index++ ] );
  141. }
  142. }
  143. catch( Exception e )
  144. {
  145. Debug.LogException( e );
  146. }
  147. }
  148. public override void WriteToString( ref string nodeInfo )
  149. {
  150. IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalItems.Count );
  151. for( int i = 0; i < m_additionalItems.Count; i++ )
  152. {
  153. IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalItems[ i ] );
  154. }
  155. }
  156. public virtual void AddToDataCollector( ref MasterNodeDataCollector dataCollector , TemplateIncludePragmaContainter nativesContainer ) { }
  157. public override void Destroy()
  158. {
  159. m_additionalItems.Clear();
  160. m_additionalItems = null;
  161. m_currentOwner = null;
  162. m_nativeItems.Clear();
  163. m_nativeItems = null;
  164. }
  165. public List<string> ItemsList { get { return m_additionalItems; } set { m_additionalItems = value; } }
  166. public List<string> OutsideList { get { return m_outsideItems; } set { m_outsideItems = value; } }
  167. }
  168. }