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.

393 lines
11 KiB

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