GraphContextMenu.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. namespace AmplifyShaderEditor
  11. {
  12. public class ShortcutKeyData
  13. {
  14. public bool IsPressed;
  15. public System.Type NodeType;
  16. public string Name;
  17. public ShortcutKeyData( System.Type type, string name )
  18. {
  19. NodeType = type;
  20. Name = name;
  21. IsPressed = false;
  22. }
  23. }
  24. public class GraphContextMenu
  25. {
  26. private List<ContextMenuItem> m_items;
  27. private List<ContextMenuItem> m_itemFunctions;
  28. private Dictionary<System.Type, NodeAttributes> m_itemsDict;
  29. private Dictionary<System.Type, NodeAttributes> m_deprecatedItemsDict;
  30. private Dictionary<System.Type, System.Type> m_castTypes;
  31. private Dictionary<KeyCode, ShortcutKeyData> m_shortcutTypes;
  32. private KeyCode m_lastKeyPressed;
  33. private ParentGraph m_currentGraph;
  34. private bool m_correctlyLoaded = false;
  35. public GraphContextMenu( ParentGraph currentGraph )
  36. {
  37. m_currentGraph = currentGraph;
  38. m_correctlyLoaded = RefreshNodes( currentGraph );
  39. }
  40. public bool RefreshNodes( ParentGraph currentGraph )
  41. {
  42. if( m_items != null )
  43. {
  44. m_items.Clear();
  45. m_items = null;
  46. }
  47. if( m_itemFunctions != null )
  48. {
  49. m_itemFunctions.Clear();
  50. m_itemFunctions = null;
  51. }
  52. m_items = new List<ContextMenuItem>();
  53. m_itemFunctions = new List<ContextMenuItem>();
  54. if( m_itemsDict != null )
  55. m_itemsDict.Clear();
  56. m_itemsDict = new Dictionary<System.Type, NodeAttributes>();
  57. if( m_deprecatedItemsDict != null )
  58. m_deprecatedItemsDict.Clear();
  59. m_deprecatedItemsDict = new Dictionary<System.Type, NodeAttributes>();
  60. if( m_castTypes != null )
  61. m_castTypes.Clear();
  62. m_castTypes = new Dictionary<System.Type, System.Type>();
  63. if( m_shortcutTypes != null )
  64. m_shortcutTypes.Clear();
  65. m_shortcutTypes = new Dictionary<KeyCode, ShortcutKeyData>();
  66. m_lastKeyPressed = KeyCode.None;
  67. // Fetch all available nodes by their attributes
  68. try
  69. {
  70. //IEnumerable<System.Type> availableTypes = AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany( type => type.GetTypes() );
  71. var mainAssembly = Assembly.GetExecutingAssembly();
  72. Type[] availableTypes = IOUtils.GetTypesInNamespace( mainAssembly, "AmplifyShaderEditor" );
  73. try
  74. {
  75. var editorAssembly = Assembly.Load( "Assembly-CSharp-Editor" );
  76. if( mainAssembly != editorAssembly )
  77. {
  78. Type[] extraTypes = IOUtils.GetTypesInNamespace( editorAssembly, "AmplifyShaderEditor" );
  79. availableTypes = availableTypes.Concat<Type>( extraTypes ).ToArray();
  80. }
  81. }
  82. catch( Exception )
  83. {
  84. // quiet catch because we don't care if it fails to find the assembly, we'll just skip it
  85. }
  86. Type[] asmdefTypes = IOUtils.GetAssemblyTypesArray();
  87. if( asmdefTypes != null && asmdefTypes.Length > 0 )
  88. {
  89. availableTypes = availableTypes.Concat<Type>( asmdefTypes ).ToArray();
  90. }
  91. foreach( System.Type type in availableTypes )
  92. {
  93. if( !m_itemsDict.ContainsKey( type ) )
  94. {
  95. foreach( NodeAttributes attribute in Attribute.GetCustomAttributes( type ).OfType<NodeAttributes>() )
  96. {
  97. if( attribute.Available && !attribute.Deprecated )
  98. {
  99. //if ( !UIUtils.CurrentWindow.IsShaderFunctionWindow && attribute.AvailableInFunctionsOnly )
  100. // continue;
  101. if( !UIUtils.HasColorCategory( attribute.Category ) )
  102. {
  103. if( !String.IsNullOrEmpty( attribute.CustomCategoryColor ) )
  104. {
  105. try
  106. {
  107. Color color = new Color();
  108. ColorUtility.TryParseHtmlString( attribute.CustomCategoryColor , out color );
  109. UIUtils.AddColorCategory( attribute.Category , color );
  110. }
  111. catch( Exception e )
  112. {
  113. Debug.LogException( e );
  114. UIUtils.AddColorCategory( attribute.Category , Constants.DefaultCategoryColor );
  115. }
  116. }
  117. //else
  118. //{
  119. // UIUtils.AddColorCategory( attribute.Category, Constants.DefaultCategoryColor );
  120. //}
  121. }
  122. if( attribute.CastType != null && attribute.CastType.Length > 0 && type != null )
  123. {
  124. for( int i = 0 ; i < attribute.CastType.Length ; i++ )
  125. {
  126. m_castTypes.Add( attribute.CastType[ i ] , type );
  127. }
  128. }
  129. if( attribute.ShortcutKey != KeyCode.None && type != null )
  130. m_shortcutTypes.Add( attribute.ShortcutKey , new ShortcutKeyData( type , attribute.Name ) );
  131. ContextMenuItem newItem = new ContextMenuItem( attribute , type , attribute.Name , attribute.Tags , attribute.Category , attribute.Description , null , attribute.ShortcutKey );
  132. if( UIUtils.GetNodeAvailabilityInBitArray( attribute.NodeAvailabilityFlags , NodeAvailability.SurfaceShader ) )
  133. m_items.Add( newItem );
  134. else if( UIUtils.GetNodeAvailabilityInBitArray( attribute.NodeAvailabilityFlags , currentGraph.ParentWindow.CurrentNodeAvailability ) )
  135. m_items.Add( newItem );
  136. else if( UIUtils.GetNodeAvailabilityInBitArray( attribute.NodeAvailabilityFlags , currentGraph.CurrentCanvasMode ) )
  137. m_items.Add( newItem );
  138. m_itemsDict.Add( type , attribute );
  139. m_itemFunctions.Add( newItem );
  140. }
  141. else
  142. {
  143. if( !m_deprecatedItemsDict.ContainsKey(type))
  144. m_deprecatedItemsDict.Add( type , attribute );
  145. }
  146. }
  147. }
  148. }
  149. }
  150. catch( ReflectionTypeLoadException exception )
  151. {
  152. Debug.LogException( exception );
  153. return false;
  154. }
  155. string[] guids = AssetDatabase.FindAssets( "t:AmplifyShaderFunction" );
  156. List<AmplifyShaderFunction> allFunctions = new List<AmplifyShaderFunction>();
  157. for( int i = 0; i < guids.Length; i++ )
  158. {
  159. allFunctions.Add( AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( AssetDatabase.GUIDToAssetPath( guids[ i ] ) ) );
  160. }
  161. int functionCount = allFunctions.Count;
  162. if( functionCount > 0 )
  163. {
  164. m_castTypes.Add( typeof( AmplifyShaderFunction ), typeof( FunctionNode ) );
  165. }
  166. for( int i = 0; i < functionCount; i++ )
  167. {
  168. if( !allFunctions[ i ].Hidden )
  169. {
  170. NodeAttributes attribute = new NodeAttributes( allFunctions[ i ].FunctionName, allFunctions[ i ].CustomNodeCategory, allFunctions[ i ].Description, KeyCode.None, true, 0, int.MaxValue, typeof( AmplifyShaderFunction ) );
  171. System.Type type = typeof( FunctionNode );
  172. ContextMenuItem newItem = new ContextMenuItem( attribute, type, AddSpacesToSentence( attribute.Name ), attribute.Tags, attribute.Category, attribute.Description, allFunctions[ i ], attribute.ShortcutKey );
  173. m_items.Add( newItem );
  174. m_itemFunctions.Add( newItem );
  175. }
  176. }
  177. //Sort out the final list by name
  178. m_items.Sort( ( x, y ) => x.Category.CompareTo( y.Category ) );
  179. m_itemFunctions.Sort( ( x, y ) => x.Category.CompareTo( y.Category ) );
  180. return true;
  181. }
  182. public void Destroy()
  183. {
  184. for( int i = 0; i < m_items.Count; i++ )
  185. {
  186. m_items[ i ].Destroy();
  187. }
  188. for( int i = 0; i < m_itemFunctions.Count; i++ )
  189. {
  190. if( m_itemFunctions[ i ] != null )
  191. m_itemFunctions[ i ].Destroy();
  192. }
  193. m_items.Clear();
  194. m_items = null;
  195. m_itemFunctions.Clear();
  196. m_itemFunctions = null;
  197. m_itemsDict.Clear();
  198. m_itemsDict = null;
  199. m_deprecatedItemsDict.Clear();
  200. m_deprecatedItemsDict = null;
  201. m_castTypes.Clear();
  202. m_castTypes = null;
  203. m_shortcutTypes.Clear();
  204. m_shortcutTypes = null;
  205. }
  206. public static string AddSpacesToSentence( string text )
  207. {
  208. if( string.IsNullOrEmpty( text ) )
  209. return string.Empty;
  210. bool lastIsUpper = char.IsUpper( text, 0 );
  211. bool lastIsLetter = char.IsLetter( text, 0 );
  212. StringBuilder title = new StringBuilder();
  213. title.Append( text[ 0 ] );
  214. for( int i = 1; i < text.Length; i++ )
  215. {
  216. bool currIsUpper = char.IsUpper( text, i );
  217. bool currIsLetter = char.IsLetter( text, i );
  218. if( currIsUpper && !lastIsUpper && lastIsLetter )
  219. {
  220. title.Append( " " );
  221. }
  222. // if current is a number and previous is a letter we space it (ie: Rotation2D = Rotation 2D)
  223. if( lastIsLetter && char.IsNumber( text, i ) )
  224. {
  225. title.Append( " " );
  226. }
  227. // if previous is upper, current is upper and the next two following are lower then we space it (ie: UVDistortion = UV Distortion)
  228. if( i < text.Length - 1 )
  229. {
  230. bool nextIsLower = char.IsLower( text, i + 1 ) && char.IsLetter( text, i + 1 );
  231. bool lastIsLower = i < text.Length - 2 ? char.IsLower( text, i + 2 ) && char.IsLetter( text, i + 2 ) : false;
  232. if( lastIsUpper && currIsUpper && currIsLetter && nextIsLower && lastIsLower )
  233. {
  234. title.Append( " " );
  235. }
  236. }
  237. lastIsUpper = currIsUpper;
  238. lastIsLetter = currIsLetter;
  239. title.Append( text[ i ] );
  240. }
  241. return title.ToString();
  242. }
  243. public NodeAttributes GetNodeAttributesForType( System.Type type )
  244. {
  245. if( type == null )
  246. {
  247. Debug.LogError( "Invalid type detected" );
  248. return null;
  249. }
  250. if( m_itemsDict.ContainsKey( type ) )
  251. return m_itemsDict[ type ];
  252. return null;
  253. }
  254. public NodeAttributes GetDeprecatedNodeAttributesForType( System.Type type )
  255. {
  256. if( m_deprecatedItemsDict.ContainsKey( type ) )
  257. return m_deprecatedItemsDict[ type ];
  258. return null;
  259. }
  260. public void UpdateKeyPress( KeyCode key )
  261. {
  262. if( key == KeyCode.None )
  263. return;
  264. m_lastKeyPressed = key;
  265. if( m_shortcutTypes.ContainsKey( key ) )
  266. {
  267. m_shortcutTypes[ key ].IsPressed = true;
  268. }
  269. }
  270. public void UpdateKeyReleased( KeyCode key )
  271. {
  272. if( key == KeyCode.None )
  273. return;
  274. if( m_shortcutTypes.ContainsKey( key ) )
  275. {
  276. m_shortcutTypes[ key ].IsPressed = false;
  277. }
  278. }
  279. public void ResetShortcutKeyStates()
  280. {
  281. foreach( KeyValuePair<KeyCode, ShortcutKeyData> kvp in m_shortcutTypes )
  282. {
  283. kvp.Value.IsPressed = false;
  284. }
  285. }
  286. public ParentNode CreateNodeFromCastType( System.Type type )
  287. {
  288. if( m_castTypes.ContainsKey( type ) )
  289. {
  290. ParentNode newNode = (ParentNode)ScriptableObject.CreateInstance( m_castTypes[ type ] );
  291. return newNode;
  292. }
  293. return null;
  294. }
  295. public ParentNode CreateNodeFromShortcutKey()
  296. {
  297. if( m_lastKeyPressed == KeyCode.None )
  298. return null;
  299. if( m_shortcutTypes.ContainsKey( m_lastKeyPressed ) && m_shortcutTypes[ m_lastKeyPressed ].IsPressed )
  300. {
  301. ParentNode newNode = (ParentNode)ScriptableObject.CreateInstance( m_shortcutTypes[ m_lastKeyPressed ].NodeType );
  302. return newNode;
  303. }
  304. return null;
  305. }
  306. public bool CheckShortcutKey()
  307. {
  308. if( m_lastKeyPressed == KeyCode.None )
  309. return false;
  310. if( m_shortcutTypes.ContainsKey( m_lastKeyPressed ) && m_shortcutTypes[ m_lastKeyPressed ].IsPressed )
  311. {
  312. return true;
  313. }
  314. return false;
  315. }
  316. public List<ContextMenuItem> MenuItems
  317. {
  318. get
  319. {
  320. if( m_currentGraph.ParentWindow.IsShaderFunctionWindow )
  321. return m_itemFunctions;
  322. else
  323. return m_items;
  324. }
  325. }
  326. public List<ContextMenuItem> ItemFunctions { get { return m_itemFunctions; } }
  327. public KeyCode LastKeyPressed
  328. {
  329. get { return m_lastKeyPressed; }
  330. }
  331. public Dictionary<KeyCode, ShortcutKeyData> NodeShortcuts { get { return m_shortcutTypes; } }
  332. public bool CorrectlyLoaded { get { return m_correctlyLoaded; } }
  333. }
  334. }