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.

201 lines
5.3 KiB

  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable] public class UsageListSamplerNodes : NodeUsageRegister<SamplerNode> { }
  8. [Serializable] public class UsageListFloatIntNodes : NodeUsageRegister<PropertyNode> { }
  9. [Serializable] public class UsageListTexturePropertyNodes : NodeUsageRegister<TexturePropertyNode> { }
  10. [Serializable] public class UsageListTextureArrayNodes : NodeUsageRegister<TextureArrayNode> { }
  11. [Serializable] public class UsageListPropertyNodes : NodeUsageRegister<PropertyNode> { }
  12. [Serializable] public class UsageListScreenColorNodes : NodeUsageRegister<ScreenColorNode> { }
  13. [Serializable] public class UsageListRegisterLocalVarNodes : NodeUsageRegister<RegisterLocalVarNode> { }
  14. [Serializable] public class UsageListFunctionInputNodes : NodeUsageRegister<FunctionInput> { }
  15. [Serializable] public class UsageListFunctionNodes : NodeUsageRegister<FunctionNode> { }
  16. [Serializable] public class UsageListFunctionOutputNodes : NodeUsageRegister<FunctionOutput> { }
  17. [Serializable] public class UsageListFunctionSwitchNodes : NodeUsageRegister<FunctionSwitch> { }
  18. [Serializable] public class UsageListFunctionSwitchCopyNodes : NodeUsageRegister<FunctionSwitch> { }
  19. [Serializable] public class UsageListTemplateMultiPassMasterNodes : NodeUsageRegister<TemplateMultiPassMasterNode> { }
  20. [Serializable] public class UsageListCustomExpressionsOnFunctionMode : NodeUsageRegister<CustomExpressionNode> { }
  21. [Serializable] public class UsageListGlobalArrayNodes : NodeUsageRegister<GlobalArrayNode> { }
  22. [Serializable]
  23. public class NodeUsageRegister<T> where T : ParentNode
  24. {
  25. // Sampler Nodes registry
  26. [SerializeField]
  27. private List<T> m_nodes;
  28. [SerializeField]
  29. private string[] m_nodesArr;
  30. [SerializeField]
  31. private int[] m_nodeIDs;
  32. [SerializeField]
  33. ParentGraph m_containerGraph;
  34. public NodeUsageRegister()
  35. {
  36. m_nodesArr = new string[ 0 ];
  37. m_nodeIDs = new int[ 0 ];
  38. m_nodes = new List<T>();
  39. }
  40. public void Destroy()
  41. {
  42. m_nodes.Clear();
  43. m_nodes = null;
  44. m_nodesArr = null;
  45. m_nodeIDs = null;
  46. }
  47. public void Clear()
  48. {
  49. m_nodes.Clear();
  50. }
  51. public int AddNode( T node )
  52. {
  53. if( node == null )
  54. return -1;
  55. if( !m_nodes.Contains( node ) )
  56. {
  57. if( m_containerGraph != null )
  58. {
  59. Undo.RegisterCompleteObjectUndo( m_containerGraph.ParentWindow, Constants.UndoRegisterNodeId );
  60. Undo.RegisterCompleteObjectUndo( m_containerGraph, Constants.UndoRegisterNodeId );
  61. }
  62. m_nodes.Add( node );
  63. UpdateNodeArr();
  64. return m_nodes.Count - 1;
  65. }
  66. else if( node.UniqueId > -1 )
  67. {
  68. UpdateNodeArr();
  69. }
  70. return -1;
  71. }
  72. public bool HasNode( int uniqueId )
  73. {
  74. return m_nodes.FindIndex( x => x.UniqueId == uniqueId ) > -1 ? true : false;
  75. //int count = m_nodes.Count;
  76. //for( int i = 0; i < count; i++ )
  77. //{
  78. // if( m_nodes[ i ].UniqueId == uniqueId )
  79. // return true;
  80. //}
  81. //return false;
  82. }
  83. public void RemoveNode( T node )
  84. {
  85. if( node == null )
  86. return;
  87. if( m_nodes.Contains( node ) )
  88. {
  89. if( m_containerGraph != null )
  90. {
  91. Undo.RegisterCompleteObjectUndo( m_containerGraph.ParentWindow, Constants.UndoUnregisterNodeId );
  92. Undo.RegisterCompleteObjectUndo( m_containerGraph, Constants.UndoUnregisterNodeId );
  93. }
  94. m_nodes.Remove( node );
  95. UpdateNodeArr();
  96. }
  97. }
  98. public void UpdateNodeArr()
  99. {
  100. int nodeCount = m_nodes.Count;
  101. if( nodeCount != m_nodesArr.Length )
  102. {
  103. m_nodesArr = new string[ nodeCount ];
  104. m_nodeIDs = new int[ nodeCount ];
  105. }
  106. for( int i = 0; i < nodeCount; i++ )
  107. {
  108. m_nodesArr[ i ] = m_nodes[ i ].DataToArray;
  109. m_nodeIDs[ i ] = m_nodes[ i ].UniqueId;
  110. }
  111. }
  112. public T GetNode( int idx )
  113. {
  114. if( idx > -1 && idx < m_nodes.Count )
  115. {
  116. return m_nodes[ idx ];
  117. }
  118. return null;
  119. }
  120. public T GetNodeByUniqueId( int uniqueId )
  121. {
  122. return m_nodes.Find( x => x.UniqueId == uniqueId );
  123. }
  124. public T GetNodeByDataToArray( string data )
  125. {
  126. return m_nodes.Find( x => x.DataToArray.Equals( data ));
  127. }
  128. public int GetNodeRegisterIdx( int uniqueId )
  129. {
  130. return m_nodes.FindIndex( x => x.UniqueId == uniqueId );
  131. //int count = m_nodes.Count;
  132. //for( int i = 0; i < count; i++ )
  133. //{
  134. // if( m_nodes[ i ].UniqueId == uniqueId )
  135. // {
  136. // return i;
  137. // }
  138. //}
  139. //return -1;
  140. }
  141. public void UpdateDataOnNode( int uniqueId, string data )
  142. {
  143. int index = m_nodes.FindIndex( x => x.UniqueId == uniqueId );
  144. if( index > -1 )
  145. {
  146. m_nodesArr[ index ] = data;
  147. m_nodeIDs[ index ] = uniqueId;
  148. }
  149. //int count = m_nodes.Count;
  150. //for( int i = 0; i < count; i++ )
  151. //{
  152. // if( m_nodes[ i ].UniqueId == uniqueId )
  153. // {
  154. // m_nodesArr[ i ] = data;
  155. // m_nodeIDs[ i ] = uniqueId;
  156. // }
  157. //}
  158. }
  159. public void Dump()
  160. {
  161. string data = string.Empty;
  162. for( int i = 0; i < m_nodesArr.Length; i++ )
  163. {
  164. data += m_nodesArr[ i ] + " " + m_nodeIDs[ i ] + '\n';
  165. }
  166. Debug.Log( data );
  167. }
  168. public string[] NodesArr { get { return m_nodesArr; } }
  169. public int[] NodeIds { get { return m_nodeIDs; } }
  170. public List<T> NodesList { get { return m_nodes; } }
  171. public int Count { get { return m_nodes.Count; } }
  172. public ParentGraph ContainerGraph { get { return m_containerGraph; } set { m_containerGraph = value; } }
  173. }
  174. }