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.

254 lines
7.9 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using System.Collections.Generic;
  6. using UnityEditor;
  7. namespace AmplifyShaderEditor
  8. {
  9. public class ClipboardData
  10. {
  11. public string Data = string.Empty;
  12. public string Connections = string.Empty;
  13. public int OldNodeId = -1;
  14. public int NewNodeId = -1;
  15. public ClipboardData( string data, string connections, int oldNodeId )
  16. {
  17. Data = data;
  18. Connections = connections;
  19. OldNodeId = oldNodeId;
  20. }
  21. public override string ToString()
  22. {
  23. return Data + IOUtils.CLIPBOARD_DATA_SEPARATOR + Connections + IOUtils.CLIPBOARD_DATA_SEPARATOR + OldNodeId + IOUtils.CLIPBOARD_DATA_SEPARATOR + NewNodeId;
  24. }
  25. }
  26. public class Clipboard
  27. {
  28. public const string ClipboardId = "AMPLIFY_CLIPBOARD_ID";
  29. private readonly string[] ClipboardTagId = { "#CLIP_ITEM#" };
  30. private List<ClipboardData> m_clipboardStrData;
  31. private Dictionary<int, ClipboardData> m_clipboardAuxData;
  32. private Dictionary<string, ClipboardData> m_multiPassMasterNodeData;
  33. public Clipboard()
  34. {
  35. m_clipboardStrData = new List<ClipboardData>();
  36. m_clipboardAuxData = new Dictionary<int, ClipboardData>();
  37. m_multiPassMasterNodeData = new Dictionary<string, ClipboardData>();
  38. }
  39. public void AddMultiPassNodesToClipboard( List<TemplateMultiPassMasterNode> masterNodes )
  40. {
  41. m_multiPassMasterNodeData.Clear();
  42. int templatesAmount = masterNodes.Count;
  43. for( int i = 0; i < templatesAmount; i++ )
  44. {
  45. if( !masterNodes[ i ].InvalidNode )
  46. {
  47. string data = string.Empty;
  48. string connection = string.Empty;
  49. System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
  50. masterNodes[ i ].FullWriteToString( ref data, ref connection );
  51. System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
  52. ClipboardData clipboardData = new ClipboardData( data, connection, masterNodes[ i ].UniqueId );
  53. m_multiPassMasterNodeData.Add( masterNodes[ i ].PassUniqueName, clipboardData );
  54. }
  55. }
  56. }
  57. public void GetMultiPassNodesFromClipboard( List<TemplateMultiPassMasterNode> masterNodes )
  58. {
  59. int templatesAmount = masterNodes.Count;
  60. for( int i = 0; i < templatesAmount; i++ )
  61. {
  62. if( m_multiPassMasterNodeData.ContainsKey( masterNodes[ i ].PassUniqueName ) )
  63. {
  64. ClipboardData nodeData = m_multiPassMasterNodeData[ masterNodes[ i ].PassUniqueName ];
  65. string[] nodeParams = nodeData.Data.Split( IOUtils.FIELD_SEPARATOR );
  66. System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
  67. masterNodes[ i ].FullReadFromString( ref nodeParams );
  68. System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
  69. }
  70. }
  71. for( int i = 0; i < templatesAmount; i++ )
  72. {
  73. if( m_multiPassMasterNodeData.ContainsKey( masterNodes[ i ].PassUniqueName ) )
  74. {
  75. masterNodes[ i ].SetReadOptions();
  76. masterNodes[ i ].ForceOptionsRefresh();
  77. }
  78. }
  79. m_multiPassMasterNodeData.Clear();
  80. }
  81. public void AddToClipboard( List<ParentNode> selectedNodes , Vector3 initialPosition, ParentGraph graph )
  82. {
  83. //m_clipboardStrData.Clear();
  84. //m_clipboardAuxData.Clear();
  85. string clipboardData = IOUtils.Vector3ToString( initialPosition ) + ClipboardTagId[ 0 ];
  86. int masterNodeId = UIUtils.CurrentWindow.CurrentGraph.CurrentMasterNodeId;
  87. int count = selectedNodes.Count;
  88. for ( int i = 0; i < count; i++ )
  89. {
  90. if ( UIUtils.CurrentWindow.IsShaderFunctionWindow || !graph.IsMasterNode( selectedNodes[ i ] ))
  91. {
  92. string nodeData = string.Empty;
  93. string connections = string.Empty;
  94. selectedNodes[ i ].ClipboardFullWriteToString( ref nodeData, ref connections );
  95. clipboardData += nodeData;
  96. if ( !string.IsNullOrEmpty( connections ) )
  97. {
  98. connections = connections.Substring( 0, connections.Length - 1 );
  99. clipboardData += "\n" + connections;
  100. }
  101. if ( i < count - 1 )
  102. clipboardData += ClipboardTagId[ 0 ];
  103. //ClipboardData data = new ClipboardData( nodeData, connections, selectedNodes[ i ].UniqueId );
  104. //m_clipboardStrData.Add( data );
  105. //m_clipboardAuxData.Add( selectedNodes[ i ].UniqueId, data );
  106. }
  107. }
  108. if ( !string.IsNullOrEmpty( clipboardData ) )
  109. {
  110. EditorPrefs.SetString( ClipboardId, clipboardData );
  111. }
  112. //for ( int i = 0; i < selectedNodes.Count; i++ )
  113. //{
  114. // if ( selectedNodes[ i ].UniqueId != masterNodeId )
  115. // {
  116. // WireNode wireNode = selectedNodes[ i ] as WireNode;
  117. // if ( wireNode != null )
  118. // {
  119. // if ( !IsNodeChainValid( selectedNodes[ i ], true ) || !IsNodeChainValid( selectedNodes[ i ], false ) )
  120. // {
  121. // UnityEngine.Debug.Log( "found invalid wire port" );
  122. // }
  123. // }
  124. // }
  125. //}
  126. }
  127. public Vector3 GetDataFromEditorPrefs()
  128. {
  129. Vector3 initialPos = Vector3.zero;
  130. m_clipboardStrData.Clear();
  131. m_clipboardAuxData.Clear();
  132. string clipboardData = EditorPrefs.GetString( ClipboardId, string.Empty );
  133. if ( !string.IsNullOrEmpty( clipboardData ) )
  134. {
  135. string[] clipboardDataArray = clipboardData.Split( ClipboardTagId, StringSplitOptions.None );
  136. initialPos = IOUtils.StringToVector3( clipboardDataArray[0] );
  137. for ( int i = 1; i < clipboardDataArray.Length; i++ )
  138. {
  139. if ( !string.IsNullOrEmpty( clipboardDataArray[ i ] ) )
  140. {
  141. int wiresIndex = clipboardDataArray[ i ].IndexOf( IOUtils.LINE_TERMINATOR );
  142. string nodeData = string.Empty;
  143. string connections = string.Empty;
  144. if ( wiresIndex < 0 )
  145. {
  146. nodeData = clipboardDataArray[ i ];
  147. }
  148. else
  149. {
  150. nodeData = clipboardDataArray[ i ].Substring( 0, wiresIndex );
  151. connections = clipboardDataArray[ i ].Substring( wiresIndex + 1 );
  152. }
  153. string[] nodeDataArr = nodeData.Split( IOUtils.FIELD_SEPARATOR );
  154. if ( nodeDataArr.Length > 2 )
  155. {
  156. int nodeId = Convert.ToInt32( nodeDataArr[ 2 ] );
  157. ClipboardData data = new ClipboardData( nodeData, connections, nodeId );
  158. m_clipboardStrData.Add( data );
  159. m_clipboardAuxData.Add( nodeId, data );
  160. }
  161. }
  162. }
  163. }
  164. return initialPos;
  165. }
  166. public bool IsNodeChainValid( ParentNode currentNode, bool forward )
  167. {
  168. WireNode wireNode = currentNode as WireNode;
  169. if ( wireNode == null )
  170. {
  171. return m_clipboardAuxData.ContainsKey( currentNode.UniqueId );
  172. }
  173. if ( forward )
  174. {
  175. if ( wireNode.InputPorts[ 0 ].ExternalReferences.Count > 0 )
  176. {
  177. int nodeId = wireNode.InputPorts[ 0 ].ExternalReferences[ 0 ].NodeId;
  178. if ( m_clipboardAuxData.ContainsKey( nodeId ) )
  179. {
  180. return IsNodeChainValid( UIUtils.GetNode( nodeId ), forward );
  181. }
  182. }
  183. }
  184. else
  185. {
  186. int nodeId = wireNode.OutputPorts[ 0 ].ExternalReferences[ 0 ].NodeId;
  187. if ( m_clipboardAuxData.ContainsKey( nodeId ) )
  188. {
  189. return IsNodeChainValid( UIUtils.GetNode( nodeId ), forward );
  190. }
  191. }
  192. return false;
  193. }
  194. public void GenerateFullString()
  195. {
  196. string data = string.Empty;
  197. for ( int i = 0; i < m_clipboardStrData.Count; i++ )
  198. {
  199. data += m_clipboardStrData[ i ].ToString();
  200. if ( i < m_clipboardStrData.Count - 1 )
  201. {
  202. data += IOUtils.LINE_TERMINATOR;
  203. }
  204. }
  205. }
  206. public void ClearClipboard()
  207. {
  208. m_clipboardStrData.Clear();
  209. m_clipboardAuxData.Clear();
  210. m_multiPassMasterNodeData.Clear();
  211. }
  212. public ClipboardData GetClipboardData( int oldNodeId )
  213. {
  214. if ( m_clipboardAuxData.ContainsKey( oldNodeId ) )
  215. return m_clipboardAuxData[ oldNodeId ];
  216. return null;
  217. }
  218. public int GeNewNodeId( int oldNodeId )
  219. {
  220. if ( m_clipboardAuxData.ContainsKey( oldNodeId ) )
  221. return m_clipboardAuxData[ oldNodeId ].NewNodeId;
  222. return -1;
  223. }
  224. public List<ClipboardData> CurrentClipboardStrData
  225. {
  226. get { return m_clipboardStrData; }
  227. }
  228. public bool HasCachedMasterNodes { get { return m_multiPassMasterNodeData.Count > 0; } }
  229. }
  230. }