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.

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