PortLegendInfo.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEditor;
  4. using UnityEngine;
  5. using System.Collections.Generic;
  6. namespace AmplifyShaderEditor
  7. {
  8. class NodeDescriptionInfo
  9. {
  10. public bool FoldoutValue;
  11. public string Category;
  12. public string[,] Contents;
  13. }
  14. public sealed class PortLegendInfo : EditorWindow
  15. {
  16. private const string NoASEWindowWarning = "Please Open the ASE to get access to shortcut info";
  17. private const float PixelSeparator = 5;
  18. private const string EditorShortcutsTitle = "Editor Shortcuts";
  19. private const string MenuShortcutsTitle = "Menu Shortcuts";
  20. private const string NodesShortcutsTitle = "Nodes Shortcuts";
  21. private const string PortShortcutsTitle = "Port Shortcuts";
  22. private const string PortLegendTitle = "Port Legend";
  23. private const string NodesDescTitle = "Node Info";
  24. private const string CompatibleAssetsTitle = "Compatible Assets";
  25. private const string KeyboardUsageTemplate = "[{0}] - {1}";
  26. private const string m_lockedStr = "Locked Port";
  27. private const float WindowSizeX = 350;
  28. private const float WindowSizeY = 300;
  29. private const float WindowPosX = 5;
  30. private const float WindowPosY = 5;
  31. private int TitleLabelWidth = 150;
  32. private Rect m_availableArea;
  33. private bool m_portAreaFoldout = true;
  34. private bool m_editorShortcutAreaFoldout = true;
  35. private bool m_menuShortcutAreaFoldout = true;
  36. private bool m_nodesShortcutAreaFoldout = true;
  37. private bool m_nodesDescriptionAreaFoldout = true;
  38. private bool m_compatibleAssetsFoldout = true;
  39. private Vector2 m_currentScrollPos;
  40. private GUIStyle m_portStyle;
  41. private GUIStyle m_labelStyleBold;
  42. private GUIStyle m_labelStyle;
  43. private GUIStyle m_nodeInfoLabelStyleBold;
  44. private GUIStyle m_nodeInfoLabelStyle;
  45. private GUIStyle m_nodeInfoFoldoutStyle;
  46. private GUIContent m_content = new GUIContent( "Helper", "Shows helper info for ASE users" );
  47. private bool m_init = true;
  48. private List<ShortcutItem> m_editorShortcuts = null;
  49. private List<ShortcutItem> m_nodesShortcuts = null;
  50. private List<NodeDescriptionInfo> m_nodeDescriptionsInfo = null;
  51. private List<string[]> m_compatibleAssetsInfo = null;
  52. public static PortLegendInfo OpenWindow()
  53. {
  54. PortLegendInfo currentWindow = ( PortLegendInfo ) PortLegendInfo.GetWindow( typeof( PortLegendInfo ), false );
  55. currentWindow.minSize = new Vector2( WindowSizeX, WindowSizeY );
  56. currentWindow.maxSize = new Vector2( WindowSizeX * 2, 2 * WindowSizeY ); ;
  57. currentWindow.wantsMouseMove = true;
  58. return currentWindow;
  59. }
  60. public void Init()
  61. {
  62. m_init = false;
  63. wantsMouseMove = false;
  64. titleContent = m_content;
  65. m_portStyle = new GUIStyle( UIUtils.GetCustomStyle( CustomStyle.PortEmptyIcon ) );
  66. m_portStyle.alignment = TextAnchor.MiddleLeft;
  67. m_portStyle.imagePosition = ImagePosition.ImageOnly;
  68. m_portStyle.margin = new RectOffset( 5, 0, 5, 0 );
  69. m_labelStyleBold = new GUIStyle( UIUtils.InputPortLabel );
  70. m_labelStyleBold.fontStyle = FontStyle.Bold;
  71. m_labelStyleBold.fontSize = ( int ) ( Constants.TextFieldFontSize );
  72. m_labelStyle = new GUIStyle( UIUtils.InputPortLabel );
  73. m_labelStyle.clipping = TextClipping.Overflow;
  74. m_labelStyle.imagePosition = ImagePosition.TextOnly;
  75. m_labelStyle.contentOffset = new Vector2( -10, 0 );
  76. m_labelStyle.fontSize = ( int ) ( Constants.TextFieldFontSize );
  77. m_nodeInfoLabelStyleBold = new GUIStyle( UIUtils.InputPortLabel );
  78. m_nodeInfoLabelStyleBold.fontStyle = FontStyle.Bold;
  79. m_nodeInfoLabelStyleBold.fontSize = ( int ) ( Constants.TextFieldFontSize );
  80. m_nodeInfoLabelStyle = new GUIStyle( UIUtils.InputPortLabel );
  81. m_nodeInfoLabelStyle.clipping = TextClipping.Clip;
  82. m_nodeInfoLabelStyle.imagePosition = ImagePosition.TextOnly;
  83. m_nodeInfoLabelStyle.fontSize = ( int ) ( Constants.TextFieldFontSize );
  84. m_nodeInfoFoldoutStyle = new GUIStyle( ( GUIStyle ) "foldout" );
  85. m_nodeInfoFoldoutStyle.fontStyle = FontStyle.Bold;
  86. if ( !EditorGUIUtility.isProSkin )
  87. {
  88. m_labelStyleBold.normal.textColor = m_labelStyle.normal.textColor = Color.black;
  89. m_nodeInfoLabelStyleBold.normal.textColor = m_labelStyle.normal.textColor = Color.black;
  90. m_nodeInfoLabelStyle.normal.textColor = m_labelStyle.normal.textColor = Color.black;
  91. }
  92. m_availableArea = new Rect( WindowPosX, WindowPosY, WindowSizeX - 2 * WindowPosX, WindowSizeY - 2 * WindowPosY );
  93. }
  94. void DrawPort( WirePortDataType type )
  95. {
  96. EditorGUILayout.BeginHorizontal();
  97. {
  98. GUI.color = UIUtils.GetColorForDataType( type, false );
  99. GUILayout.Box( string.Empty, m_portStyle, GUILayout.Width( UIUtils.PortsSize.x ), GUILayout.Height( UIUtils.PortsSize.y ) );
  100. GUI.color = Color.white;
  101. EditorGUILayout.LabelField( UIUtils.GetNameForDataType( type ), m_labelStyle );
  102. }
  103. EditorGUILayout.EndHorizontal();
  104. EditorGUILayout.Separator();
  105. }
  106. void OnGUI()
  107. {
  108. if ( !UIUtils.Initialized || UIUtils.CurrentWindow == null )
  109. {
  110. EditorGUILayout.LabelField( NoASEWindowWarning );
  111. return;
  112. }
  113. if ( m_init )
  114. {
  115. Init();
  116. }
  117. TitleLabelWidth = (int)(this.position.width * 0.42f);
  118. KeyCode key = Event.current.keyCode;
  119. if ( key == ShortcutsManager.ScrollUpKey )
  120. {
  121. m_currentScrollPos.y -= 10;
  122. if ( m_currentScrollPos.y < 0 )
  123. {
  124. m_currentScrollPos.y = 0;
  125. }
  126. Event.current.Use();
  127. }
  128. if ( key == ShortcutsManager.ScrollDownKey )
  129. {
  130. m_currentScrollPos.y += 10;
  131. Event.current.Use();
  132. }
  133. if ( Event.current.type == EventType.MouseDrag && Event.current.button > 0 )
  134. {
  135. m_currentScrollPos.x += Constants.MenuDragSpeed * Event.current.delta.x;
  136. if ( m_currentScrollPos.x < 0 )
  137. {
  138. m_currentScrollPos.x = 0;
  139. }
  140. m_currentScrollPos.y += Constants.MenuDragSpeed * Event.current.delta.y;
  141. if ( m_currentScrollPos.y < 0 )
  142. {
  143. m_currentScrollPos.y = 0;
  144. }
  145. }
  146. m_availableArea = new Rect( WindowPosX, WindowPosY, position.width - 2 * WindowPosX, position.height - 2 * WindowPosY );
  147. GUILayout.BeginArea( m_availableArea );
  148. {
  149. if ( GUILayout.Button( "Wiki Page" ) )
  150. {
  151. Application.OpenURL( Constants.HelpURL );
  152. }
  153. m_currentScrollPos = GUILayout.BeginScrollView( m_currentScrollPos );
  154. {
  155. EditorGUILayout.BeginVertical();
  156. {
  157. NodeUtils.DrawPropertyGroup( ref m_portAreaFoldout, PortLegendTitle, DrawPortInfo );
  158. float currLabelWidth = EditorGUIUtility.labelWidth;
  159. EditorGUIUtility.labelWidth = 1;
  160. NodeUtils.DrawPropertyGroup( ref m_editorShortcutAreaFoldout, EditorShortcutsTitle, DrawEditorShortcuts );
  161. NodeUtils.DrawPropertyGroup( ref m_menuShortcutAreaFoldout, MenuShortcutsTitle, DrawMenuShortcuts );
  162. NodeUtils.DrawPropertyGroup( ref m_nodesShortcutAreaFoldout, NodesShortcutsTitle, DrawNodesShortcuts );
  163. NodeUtils.DrawPropertyGroup( ref m_compatibleAssetsFoldout, CompatibleAssetsTitle, DrawCompatibleAssets );
  164. NodeUtils.DrawPropertyGroup( ref m_nodesDescriptionAreaFoldout, NodesDescTitle, DrawNodeDescriptions );
  165. EditorGUIUtility.labelWidth = currLabelWidth;
  166. }
  167. EditorGUILayout.EndVertical();
  168. }
  169. GUILayout.EndScrollView();
  170. }
  171. GUILayout.EndArea();
  172. }
  173. void DrawPortInfo()
  174. {
  175. Color originalColor = GUI.color;
  176. DrawPort( WirePortDataType.OBJECT );
  177. DrawPort( WirePortDataType.INT );
  178. DrawPort( WirePortDataType.FLOAT );
  179. DrawPort( WirePortDataType.FLOAT2 );
  180. DrawPort( WirePortDataType.FLOAT3 );
  181. DrawPort( WirePortDataType.FLOAT4 );
  182. DrawPort( WirePortDataType.COLOR );
  183. DrawPort( WirePortDataType.SAMPLER2D );
  184. DrawPort( WirePortDataType.FLOAT3x3 );
  185. DrawPort( WirePortDataType.FLOAT4x4 );
  186. EditorGUILayout.BeginHorizontal();
  187. {
  188. GUI.color = Constants.LockedPortColor;
  189. GUILayout.Box( string.Empty, m_portStyle, GUILayout.Width( UIUtils.PortsSize.x ), GUILayout.Height( UIUtils.PortsSize.y ) );
  190. GUI.color = Color.white;
  191. EditorGUILayout.LabelField( m_lockedStr, m_labelStyle );
  192. }
  193. EditorGUILayout.EndHorizontal();
  194. GUI.color = originalColor;
  195. }
  196. public void DrawEditorShortcuts()
  197. {
  198. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  199. if ( window != null )
  200. {
  201. if ( m_editorShortcuts == null )
  202. {
  203. m_editorShortcuts = window.ShortcutManagerInstance.AvailableEditorShortcutsList;
  204. }
  205. EditorGUI.indentLevel--;
  206. int count = m_editorShortcuts.Count;
  207. for ( int i = 0; i < count; i++ )
  208. {
  209. DrawItem( m_editorShortcuts[ i ].Name, m_editorShortcuts[ i ].Description );
  210. }
  211. DrawItem( "Q", "Alternative Pan modifier" );
  212. DrawItem( "Ctrl + F", "Find nodes" );
  213. DrawItem( "LMB Drag", "Box selection" );
  214. DrawItem( "MMB/RMB Drag", "Camera pan" );
  215. DrawItem( "Alt + MMB/RMB Drag", "Zoom graph" );
  216. DrawItem( "Shift/Ctrl + Node Select", "Add/Remove from selection" );
  217. DrawItem( "Shift + Node Drag", "Node move with offset" );
  218. DrawItem( "Ctrl + Node Drag", "Node move with snap" );
  219. DrawItem( "MMB/RMB + Drag Panel", "Scroll panel" );
  220. DrawItem( "Alt + LMB Drag", "Additive box selection" );
  221. DrawItem( "Alt + Shift + Drag", "Subtractive box selection" );
  222. DrawItem( "Alt + Node Drag", "Auto-(Dis)Connect node on existing wire connection" );
  223. EditorGUI.indentLevel++;
  224. }
  225. else
  226. {
  227. EditorGUILayout.LabelField( NoASEWindowWarning );
  228. }
  229. }
  230. public void DrawMenuShortcuts()
  231. {
  232. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  233. if ( window != null )
  234. {
  235. EditorGUI.indentLevel--;
  236. DrawItem( ShortcutsManager.ScrollUpKey.ToString(), "Scroll Up Menu" );
  237. DrawItem( ShortcutsManager.ScrollDownKey.ToString(), "Scroll Down Menu" );
  238. DrawItem( "RMB Drag", "Scroll Menu" );
  239. EditorGUI.indentLevel++;
  240. }
  241. else
  242. {
  243. EditorGUILayout.LabelField( NoASEWindowWarning );
  244. }
  245. }
  246. void DrawItem( string name, string description )
  247. {
  248. GUILayout.BeginHorizontal();
  249. GUILayout.Label( name, m_labelStyleBold , GUILayout.Width( TitleLabelWidth ) );
  250. GUILayout.Label( description, m_labelStyle );
  251. GUILayout.EndHorizontal();
  252. GUILayout.Space( PixelSeparator );
  253. }
  254. public void DrawNodesShortcuts()
  255. {
  256. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  257. if ( window != null )
  258. {
  259. if ( m_nodesShortcuts == null || m_nodesShortcuts.Count == 0 )
  260. {
  261. m_nodesShortcuts = window.ShortcutManagerInstance.AvailableNodesShortcutsList;
  262. }
  263. EditorGUI.indentLevel--;
  264. int count = m_nodesShortcuts.Count;
  265. for ( int i = 0; i < count; i++ )
  266. {
  267. DrawItem( m_nodesShortcuts[ i ].Name, m_nodesShortcuts[ i ].Description );
  268. }
  269. EditorGUI.indentLevel++;
  270. }
  271. else
  272. {
  273. EditorGUILayout.LabelField( NoASEWindowWarning );
  274. }
  275. }
  276. string CreateCompatibilityString( string source )
  277. {
  278. string[] split = source.Split( '.' );
  279. if ( split != null && split.Length > 1 )
  280. {
  281. return split[ 1 ];
  282. }
  283. else
  284. {
  285. return source;
  286. }
  287. }
  288. public void DrawCompatibleAssets()
  289. {
  290. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  291. if ( window != null )
  292. {
  293. if ( m_compatibleAssetsInfo == null )
  294. {
  295. m_compatibleAssetsInfo = new List<string[]>();
  296. List<ContextMenuItem> items = window.ContextMenuInstance.MenuItems;
  297. int count = items.Count;
  298. for ( int i = 0; i < count; i++ )
  299. {
  300. if ( items[ i ].NodeAttributes != null && items[ i ].NodeAttributes.CastType != null )
  301. {
  302. string types = string.Empty;
  303. if ( items[ i ].NodeAttributes.CastType.Length > 1 )
  304. {
  305. for ( int j = 0; j < items[ i ].NodeAttributes.CastType.Length; j++ )
  306. {
  307. types += CreateCompatibilityString( items[ i ].NodeAttributes.CastType[ j ].ToString() );
  308. if ( j < items[ i ].NodeAttributes.CastType.Length - 1 )
  309. {
  310. types += ", ";
  311. }
  312. }
  313. }
  314. else
  315. {
  316. types = CreateCompatibilityString( items[ i ].NodeAttributes.CastType[ 0 ].ToString() );
  317. }
  318. m_compatibleAssetsInfo.Add( new string[] { items[ i ].NodeAttributes.Name + ": ", types } );
  319. }
  320. }
  321. }
  322. EditorGUI.indentLevel--;
  323. int nodeCount = m_compatibleAssetsInfo.Count;
  324. for ( int j = 0; j < nodeCount; j++ )
  325. {
  326. DrawItem( m_compatibleAssetsInfo[ j ][ 0 ], m_compatibleAssetsInfo[ j ][ 1 ] );
  327. }
  328. EditorGUI.indentLevel++;
  329. }
  330. else
  331. {
  332. EditorGUILayout.LabelField( NoASEWindowWarning );
  333. }
  334. }
  335. public void DrawNodeDescriptions()
  336. {
  337. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  338. if ( window != null )
  339. {
  340. if ( m_nodeDescriptionsInfo == null )
  341. {
  342. //fetch node info
  343. m_nodeDescriptionsInfo = new List<NodeDescriptionInfo>();
  344. Dictionary<string, PaletteFilterData> nodeData = window.CurrentPaletteWindow.BuildFullList();
  345. var enumerator = nodeData.GetEnumerator();
  346. while ( enumerator.MoveNext() )
  347. {
  348. List<ContextMenuItem> nodes = enumerator.Current.Value.Contents;
  349. int count = nodes.Count;
  350. NodeDescriptionInfo currInfo = new NodeDescriptionInfo();
  351. currInfo.Contents = new string[ count, 2 ];
  352. currInfo.Category = enumerator.Current.Key;
  353. for ( int i = 0; i < count; i++ )
  354. {
  355. currInfo.Contents[ i, 0 ] = nodes[ i ].Name + ':';
  356. currInfo.Contents[ i, 1 ] = nodes[ i ].Description;
  357. }
  358. m_nodeDescriptionsInfo.Add( currInfo );
  359. }
  360. }
  361. //draw
  362. {
  363. GUILayout.Space( 5 );
  364. int count = m_nodeDescriptionsInfo.Count;
  365. EditorGUI.indentLevel--;
  366. for ( int i = 0; i < count; i++ )
  367. {
  368. m_nodeDescriptionsInfo[ i ].FoldoutValue = EditorGUILayout.Foldout( m_nodeDescriptionsInfo[ i ].FoldoutValue, m_nodeDescriptionsInfo[ i ].Category, m_nodeInfoFoldoutStyle );
  369. if ( m_nodeDescriptionsInfo[ i ].FoldoutValue )
  370. {
  371. EditorGUI.indentLevel++;
  372. int nodeCount = m_nodeDescriptionsInfo[ i ].Contents.GetLength( 0 );
  373. for ( int j = 0; j < nodeCount; j++ )
  374. {
  375. GUILayout.Label( m_nodeDescriptionsInfo[ i ].Contents[ j, 0 ], m_nodeInfoLabelStyleBold );
  376. GUILayout.Label( m_nodeDescriptionsInfo[ i ].Contents[ j, 1 ], m_nodeInfoLabelStyle );
  377. GUILayout.Space( PixelSeparator );
  378. }
  379. EditorGUI.indentLevel--;
  380. }
  381. GUILayout.Space( PixelSeparator );
  382. }
  383. EditorGUI.indentLevel++;
  384. }
  385. }
  386. else
  387. {
  388. EditorGUILayout.LabelField( NoASEWindowWarning );
  389. }
  390. }
  391. private void OnDestroy()
  392. {
  393. m_nodesShortcuts = null;
  394. m_editorShortcuts = null;
  395. m_portStyle = null;
  396. m_labelStyle = null;
  397. m_labelStyleBold = null;
  398. m_nodeInfoLabelStyle = null;
  399. m_nodeInfoLabelStyleBold = null;
  400. m_nodeInfoFoldoutStyle = null;
  401. m_init = false;
  402. if ( m_nodeDescriptionsInfo != null )
  403. {
  404. m_nodeDescriptionsInfo.Clear();
  405. m_nodeDescriptionsInfo = null;
  406. }
  407. if( m_compatibleAssetsInfo != null )
  408. {
  409. m_compatibleAssetsInfo.Clear();
  410. m_compatibleAssetsInfo = null;
  411. }
  412. }
  413. }
  414. }