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.

466 lines
14 KiB

  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. }
  90. m_availableArea = new Rect( WindowPosX, WindowPosY, WindowSizeX - 2 * WindowPosX, WindowSizeY - 2 * WindowPosY );
  91. }
  92. void DrawPort( WirePortDataType type )
  93. {
  94. EditorGUILayout.BeginHorizontal();
  95. {
  96. GUI.color = UIUtils.GetColorForDataType( type, false );
  97. GUILayout.Box( string.Empty, m_portStyle, GUILayout.Width( UIUtils.PortsSize.x ), GUILayout.Height( UIUtils.PortsSize.y ) );
  98. GUI.color = Color.white;
  99. EditorGUILayout.LabelField( UIUtils.GetNameForDataType( type ), m_labelStyle );
  100. }
  101. EditorGUILayout.EndHorizontal();
  102. EditorGUILayout.Separator();
  103. }
  104. void OnGUI()
  105. {
  106. if ( !UIUtils.Initialized || UIUtils.CurrentWindow == null )
  107. {
  108. EditorGUILayout.LabelField( NoASEWindowWarning );
  109. return;
  110. }
  111. if ( m_init )
  112. {
  113. Init();
  114. }
  115. TitleLabelWidth = (int)(this.position.width * 0.42f);
  116. KeyCode key = Event.current.keyCode;
  117. if ( key == ShortcutsManager.ScrollUpKey )
  118. {
  119. m_currentScrollPos.y -= 10;
  120. if ( m_currentScrollPos.y < 0 )
  121. {
  122. m_currentScrollPos.y = 0;
  123. }
  124. Event.current.Use();
  125. }
  126. if ( key == ShortcutsManager.ScrollDownKey )
  127. {
  128. m_currentScrollPos.y += 10;
  129. Event.current.Use();
  130. }
  131. if ( Event.current.type == EventType.MouseDrag && Event.current.button > 0 )
  132. {
  133. m_currentScrollPos.x += Constants.MenuDragSpeed * Event.current.delta.x;
  134. if ( m_currentScrollPos.x < 0 )
  135. {
  136. m_currentScrollPos.x = 0;
  137. }
  138. m_currentScrollPos.y += Constants.MenuDragSpeed * Event.current.delta.y;
  139. if ( m_currentScrollPos.y < 0 )
  140. {
  141. m_currentScrollPos.y = 0;
  142. }
  143. }
  144. m_availableArea = new Rect( WindowPosX, WindowPosY, position.width - 2 * WindowPosX, position.height - 2 * WindowPosY );
  145. GUILayout.BeginArea( m_availableArea );
  146. {
  147. if ( GUILayout.Button( "Wiki Page" ) )
  148. {
  149. Application.OpenURL( Constants.HelpURL );
  150. }
  151. m_currentScrollPos = GUILayout.BeginScrollView( m_currentScrollPos );
  152. {
  153. EditorGUILayout.BeginVertical();
  154. {
  155. NodeUtils.DrawPropertyGroup( ref m_portAreaFoldout, PortLegendTitle, DrawPortInfo );
  156. float currLabelWidth = EditorGUIUtility.labelWidth;
  157. EditorGUIUtility.labelWidth = 1;
  158. NodeUtils.DrawPropertyGroup( ref m_editorShortcutAreaFoldout, EditorShortcutsTitle, DrawEditorShortcuts );
  159. NodeUtils.DrawPropertyGroup( ref m_menuShortcutAreaFoldout, MenuShortcutsTitle, DrawMenuShortcuts );
  160. NodeUtils.DrawPropertyGroup( ref m_nodesShortcutAreaFoldout, NodesShortcutsTitle, DrawNodesShortcuts );
  161. NodeUtils.DrawPropertyGroup( ref m_compatibleAssetsFoldout, CompatibleAssetsTitle, DrawCompatibleAssets );
  162. NodeUtils.DrawPropertyGroup( ref m_nodesDescriptionAreaFoldout, NodesDescTitle, DrawNodeDescriptions );
  163. EditorGUIUtility.labelWidth = currLabelWidth;
  164. }
  165. EditorGUILayout.EndVertical();
  166. }
  167. GUILayout.EndScrollView();
  168. }
  169. GUILayout.EndArea();
  170. }
  171. void DrawPortInfo()
  172. {
  173. Color originalColor = GUI.color;
  174. DrawPort( WirePortDataType.OBJECT );
  175. DrawPort( WirePortDataType.INT );
  176. DrawPort( WirePortDataType.FLOAT );
  177. DrawPort( WirePortDataType.FLOAT2 );
  178. DrawPort( WirePortDataType.FLOAT3 );
  179. DrawPort( WirePortDataType.FLOAT4 );
  180. DrawPort( WirePortDataType.COLOR );
  181. DrawPort( WirePortDataType.SAMPLER2D );
  182. DrawPort( WirePortDataType.FLOAT3x3 );
  183. DrawPort( WirePortDataType.FLOAT4x4 );
  184. EditorGUILayout.BeginHorizontal();
  185. {
  186. GUI.color = Constants.LockedPortColor;
  187. GUILayout.Box( string.Empty, m_portStyle, GUILayout.Width( UIUtils.PortsSize.x ), GUILayout.Height( UIUtils.PortsSize.y ) );
  188. GUI.color = Color.white;
  189. EditorGUILayout.LabelField( m_lockedStr, m_labelStyle );
  190. }
  191. EditorGUILayout.EndHorizontal();
  192. GUI.color = originalColor;
  193. }
  194. public void DrawEditorShortcuts()
  195. {
  196. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  197. if ( window != null )
  198. {
  199. if ( m_editorShortcuts == null )
  200. {
  201. m_editorShortcuts = window.ShortcutManagerInstance.AvailableEditorShortcutsList;
  202. }
  203. EditorGUI.indentLevel--;
  204. int count = m_editorShortcuts.Count;
  205. for ( int i = 0; i < count; i++ )
  206. {
  207. DrawItem( m_editorShortcuts[ i ].Name, m_editorShortcuts[ i ].Description );
  208. }
  209. DrawItem( "Ctrl + F", "Find nodes" );
  210. DrawItem( "LMB Drag", "Box selection" );
  211. DrawItem( "MMB/RMB Drag", "Camera pan" );
  212. DrawItem( "Alt + MMB/RMB Drag", "Zoom graph" );
  213. DrawItem( "Shift/Ctrl + Node Select", "Add/Remove from selection" );
  214. DrawItem( "Shift + Node Drag", "Node move with offset" );
  215. DrawItem( "Ctrl + Node Drag", "Node move with snap" );
  216. DrawItem( "MMB/RMB + Drag Panel", "Scroll panel" );
  217. DrawItem( "Alt + LMB Drag", "Additive box selection" );
  218. DrawItem( "Alt + Shift + Drag", "Subtractive box selection" );
  219. DrawItem( "Alt + Node Drag", "Auto-(Dis)Connect node on existing wire connection" );
  220. EditorGUI.indentLevel++;
  221. }
  222. else
  223. {
  224. EditorGUILayout.LabelField( NoASEWindowWarning );
  225. }
  226. }
  227. public void DrawMenuShortcuts()
  228. {
  229. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  230. if ( window != null )
  231. {
  232. EditorGUI.indentLevel--;
  233. DrawItem( ShortcutsManager.ScrollUpKey.ToString(), "Scroll Up Menu" );
  234. DrawItem( ShortcutsManager.ScrollDownKey.ToString(), "Scroll Down Menu" );
  235. DrawItem( "RMB Drag", "Scroll Menu" );
  236. EditorGUI.indentLevel++;
  237. }
  238. else
  239. {
  240. EditorGUILayout.LabelField( NoASEWindowWarning );
  241. }
  242. }
  243. void DrawItem( string name, string description )
  244. {
  245. GUILayout.BeginHorizontal();
  246. GUILayout.Label( name, m_labelStyleBold , GUILayout.Width( TitleLabelWidth ) );
  247. GUILayout.Label( description, m_labelStyle );
  248. GUILayout.EndHorizontal();
  249. GUILayout.Space( PixelSeparator );
  250. }
  251. public void DrawNodesShortcuts()
  252. {
  253. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  254. if ( window != null )
  255. {
  256. if ( m_nodesShortcuts == null || m_nodesShortcuts.Count == 0 )
  257. {
  258. m_nodesShortcuts = window.ShortcutManagerInstance.AvailableNodesShortcutsList;
  259. }
  260. EditorGUI.indentLevel--;
  261. int count = m_nodesShortcuts.Count;
  262. for ( int i = 0; i < count; i++ )
  263. {
  264. DrawItem( m_nodesShortcuts[ i ].Name, m_nodesShortcuts[ i ].Description );
  265. }
  266. EditorGUI.indentLevel++;
  267. }
  268. else
  269. {
  270. EditorGUILayout.LabelField( NoASEWindowWarning );
  271. }
  272. }
  273. string CreateCompatibilityString( string source )
  274. {
  275. string[] split = source.Split( '.' );
  276. if ( split != null && split.Length > 1 )
  277. {
  278. return split[ 1 ];
  279. }
  280. else
  281. {
  282. return source;
  283. }
  284. }
  285. public void DrawCompatibleAssets()
  286. {
  287. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  288. if ( window != null )
  289. {
  290. if ( m_compatibleAssetsInfo == null )
  291. {
  292. m_compatibleAssetsInfo = new List<string[]>();
  293. List<ContextMenuItem> items = window.ContextMenuInstance.MenuItems;
  294. int count = items.Count;
  295. for ( int i = 0; i < count; i++ )
  296. {
  297. if ( items[ i ].NodeAttributes != null && items[ i ].NodeAttributes.CastType != null )
  298. {
  299. string types = string.Empty;
  300. if ( items[ i ].NodeAttributes.CastType.Length > 1 )
  301. {
  302. for ( int j = 0; j < items[ i ].NodeAttributes.CastType.Length; j++ )
  303. {
  304. types += CreateCompatibilityString( items[ i ].NodeAttributes.CastType[ j ].ToString() );
  305. if ( j < items[ i ].NodeAttributes.CastType.Length - 1 )
  306. {
  307. types += ", ";
  308. }
  309. }
  310. }
  311. else
  312. {
  313. types = CreateCompatibilityString( items[ i ].NodeAttributes.CastType[ 0 ].ToString() );
  314. }
  315. m_compatibleAssetsInfo.Add( new string[] { items[ i ].NodeAttributes.Name + ": ", types } );
  316. }
  317. }
  318. }
  319. EditorGUI.indentLevel--;
  320. int nodeCount = m_compatibleAssetsInfo.Count;
  321. for ( int j = 0; j < nodeCount; j++ )
  322. {
  323. DrawItem( m_compatibleAssetsInfo[ j ][ 0 ], m_compatibleAssetsInfo[ j ][ 1 ] );
  324. }
  325. EditorGUI.indentLevel++;
  326. }
  327. else
  328. {
  329. EditorGUILayout.LabelField( NoASEWindowWarning );
  330. }
  331. }
  332. public void DrawNodeDescriptions()
  333. {
  334. AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
  335. if ( window != null )
  336. {
  337. if ( m_nodeDescriptionsInfo == null )
  338. {
  339. //fetch node info
  340. m_nodeDescriptionsInfo = new List<NodeDescriptionInfo>();
  341. Dictionary<string, PaletteFilterData> nodeData = window.CurrentPaletteWindow.BuildFullList();
  342. var enumerator = nodeData.GetEnumerator();
  343. while ( enumerator.MoveNext() )
  344. {
  345. List<ContextMenuItem> nodes = enumerator.Current.Value.Contents;
  346. int count = nodes.Count;
  347. NodeDescriptionInfo currInfo = new NodeDescriptionInfo();
  348. currInfo.Contents = new string[ count, 2 ];
  349. currInfo.Category = enumerator.Current.Key;
  350. for ( int i = 0; i < count; i++ )
  351. {
  352. currInfo.Contents[ i, 0 ] = nodes[ i ].Name + ':';
  353. currInfo.Contents[ i, 1 ] = nodes[ i ].Description;
  354. }
  355. m_nodeDescriptionsInfo.Add( currInfo );
  356. }
  357. }
  358. //draw
  359. {
  360. GUILayout.Space( 5 );
  361. int count = m_nodeDescriptionsInfo.Count;
  362. EditorGUI.indentLevel--;
  363. for ( int i = 0; i < count; i++ )
  364. {
  365. m_nodeDescriptionsInfo[ i ].FoldoutValue = EditorGUILayout.Foldout( m_nodeDescriptionsInfo[ i ].FoldoutValue, m_nodeDescriptionsInfo[ i ].Category, m_nodeInfoFoldoutStyle );
  366. if ( m_nodeDescriptionsInfo[ i ].FoldoutValue )
  367. {
  368. EditorGUI.indentLevel++;
  369. int nodeCount = m_nodeDescriptionsInfo[ i ].Contents.GetLength( 0 );
  370. for ( int j = 0; j < nodeCount; j++ )
  371. {
  372. GUILayout.Label( m_nodeDescriptionsInfo[ i ].Contents[ j, 0 ], m_nodeInfoLabelStyleBold );
  373. GUILayout.Label( m_nodeDescriptionsInfo[ i ].Contents[ j, 1 ], m_nodeInfoLabelStyle );
  374. GUILayout.Space( PixelSeparator );
  375. }
  376. EditorGUI.indentLevel--;
  377. }
  378. GUILayout.Space( PixelSeparator );
  379. }
  380. EditorGUI.indentLevel++;
  381. }
  382. }
  383. else
  384. {
  385. EditorGUILayout.LabelField( NoASEWindowWarning );
  386. }
  387. }
  388. private void OnDestroy()
  389. {
  390. m_nodesShortcuts = null;
  391. m_editorShortcuts = null;
  392. m_portStyle = null;
  393. m_labelStyle = null;
  394. m_labelStyleBold = null;
  395. m_nodeInfoLabelStyle = null;
  396. m_nodeInfoLabelStyleBold = null;
  397. m_nodeInfoFoldoutStyle = null;
  398. m_init = false;
  399. if ( m_nodeDescriptionsInfo != null )
  400. {
  401. m_nodeDescriptionsInfo.Clear();
  402. m_nodeDescriptionsInfo = null;
  403. }
  404. if( m_compatibleAssetsInfo != null )
  405. {
  406. m_compatibleAssetsInfo.Clear();
  407. m_compatibleAssetsInfo = null;
  408. }
  409. }
  410. }
  411. }