Clipboard.cs 8.1 KB

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