Assignment for RMIT Mixed Reality in 2020
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.

203 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] public class UsageListStaticSwitchNodes : NodeUsageRegister<StaticSwitch> { }
  23. [Serializable]
  24. public class NodeUsageRegister<T> where T : ParentNode
  25. {
  26. public delegate void ReorderEvent();
  27. public event ReorderEvent OnReorderEventComplete;
  28. [SerializeField]
  29. public bool ReorderOnChange = false;
  30. // Sampler Nodes registry
  31. [SerializeField]
  32. private List<T> m_nodes;
  33. [SerializeField]
  34. private string[] m_nodesArr;
  35. [SerializeField]
  36. private int[] m_nodeIDs;
  37. [SerializeField]
  38. ParentGraph m_containerGraph;
  39. public NodeUsageRegister()
  40. {
  41. m_nodesArr = new string[ 0 ];
  42. m_nodeIDs = new int[ 0 ];
  43. m_nodes = new List<T>();
  44. }
  45. public void Destroy()
  46. {
  47. m_nodes.Clear();
  48. m_nodes = null;
  49. m_nodesArr = null;
  50. m_nodeIDs = null;
  51. }
  52. public void Clear()
  53. {
  54. m_nodes.Clear();
  55. }
  56. public int AddNode( T node )
  57. {
  58. if( node == null )
  59. return -1;
  60. if( !m_nodes.Contains( node ) )
  61. {
  62. if( m_containerGraph != null )
  63. {
  64. Undo.RegisterCompleteObjectUndo( m_containerGraph.ParentWindow, Constants.UndoRegisterNodeId );
  65. Undo.RegisterCompleteObjectUndo( m_containerGraph, Constants.UndoRegisterNodeId );
  66. }
  67. m_nodes.Add( node );
  68. ReorderNodes();
  69. UpdateNodeArr();
  70. return m_nodes.Count - 1;
  71. }
  72. else if( node.UniqueId > -1 )
  73. {
  74. UpdateNodeArr();
  75. }
  76. return -1;
  77. }
  78. public bool HasNode( int uniqueId )
  79. {
  80. return m_nodes.FindIndex( x => x.UniqueId == uniqueId ) > -1 ? true : false;
  81. }
  82. public void RemoveNode( T node )
  83. {
  84. if( node == null )
  85. return;
  86. if( m_nodes.Contains( node ) )
  87. {
  88. if( m_containerGraph != null )
  89. {
  90. Undo.RegisterCompleteObjectUndo( m_containerGraph.ParentWindow, Constants.UndoUnregisterNodeId );
  91. Undo.RegisterCompleteObjectUndo( m_containerGraph, Constants.UndoUnregisterNodeId );
  92. }
  93. m_nodes.Remove( node );
  94. ReorderNodes();
  95. UpdateNodeArr();
  96. }
  97. }
  98. public void ReorderNodes()
  99. {
  100. if( ReorderOnChange )
  101. {
  102. m_nodes.Sort( ( x, y ) => ( x.DataToArray.CompareTo( y.DataToArray ) ) );
  103. if( OnReorderEventComplete != null )
  104. {
  105. OnReorderEventComplete();
  106. }
  107. }
  108. }
  109. public void UpdateNodeArr()
  110. {
  111. int nodeCount = m_nodes.Count;
  112. if( nodeCount != m_nodesArr.Length )
  113. {
  114. m_nodesArr = new string[ nodeCount ];
  115. m_nodeIDs = new int[ nodeCount ];
  116. }
  117. for( int i = 0; i < nodeCount; i++ )
  118. {
  119. m_nodesArr[ i ] = m_nodes[ i ].DataToArray;
  120. m_nodeIDs[ i ] = m_nodes[ i ].UniqueId;
  121. }
  122. }
  123. public T GetNode( int idx )
  124. {
  125. if( idx > -1 && idx < m_nodes.Count )
  126. {
  127. return m_nodes[ idx ];
  128. }
  129. return null;
  130. }
  131. public T GetNodeByUniqueId( int uniqueId )
  132. {
  133. return m_nodes.Find( x => x.UniqueId == uniqueId );
  134. }
  135. public T GetNodeByDataToArray( string data )
  136. {
  137. return m_nodes.Find( x => x.DataToArray.Equals( data ));
  138. }
  139. public int GetNodeRegisterIdx( int uniqueId )
  140. {
  141. return m_nodes.FindIndex( x => x.UniqueId == uniqueId );
  142. }
  143. public void UpdateDataOnNode( int uniqueId, string data )
  144. {
  145. if( ReorderOnChange )
  146. {
  147. ReorderNodes();
  148. UpdateNodeArr();
  149. }
  150. else
  151. {
  152. int index = m_nodes.FindIndex( x => x.UniqueId == uniqueId );
  153. if( index > -1 )
  154. {
  155. m_nodesArr[ index ] = data;
  156. m_nodeIDs[ index ] = uniqueId;
  157. }
  158. }
  159. }
  160. public void Dump()
  161. {
  162. string data = string.Empty;
  163. for( int i = 0; i < m_nodesArr.Length; i++ )
  164. {
  165. data += m_nodesArr[ i ] + " " + m_nodeIDs[ i ] + '\n';
  166. }
  167. Debug.Log( data );
  168. }
  169. public string[] NodesArr { get { return m_nodesArr; } }
  170. public int[] NodeIds { get { return m_nodeIDs; } }
  171. public List<T> NodesList { get { return m_nodes; } }
  172. public int Count { get { return m_nodes.Count; } }
  173. public ParentGraph ContainerGraph { get { return m_containerGraph; } set { m_containerGraph = value; } }
  174. }
  175. }