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.

533 lines
18 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.Collections.Generic;
  6. using UnityEditorInternal;
  7. namespace AmplifyShaderEditor
  8. {
  9. public sealed class NodeParametersWindow : MenuParent
  10. {
  11. private int m_lastSelectedNode = -1;
  12. private const string TitleStr = "Node Properties";
  13. private GUIStyle m_nodePropertiesStyle;
  14. private GUIContent m_dummyContent = new GUIContent();
  15. private GUIStyle m_propertyAdjustment;
  16. private ReorderableList m_functionInputsReordableList = null;
  17. private int m_functionInputsLastCount = 0;
  18. private ReorderableList m_functionSwitchesReordableList = null;
  19. private int m_functionSwitchesLastCount = 0;
  20. private ReorderableList m_functionOutputsReordableList = null;
  21. private int m_functionOutputsLastCount = 0;
  22. private ReorderableList m_propertyReordableList = null;
  23. private int m_lastCount = 0;
  24. private bool m_forceUpdate = false;
  25. [SerializeField]
  26. private List<PropertyNode> m_propertyReordableNodes = new List<PropertyNode>();
  27. // width and height are between [0,1] and represent a percentage of the total screen area
  28. public NodeParametersWindow( AmplifyShaderEditorWindow parentWindow ) : base( parentWindow, 0, 0, 265, 0, string.Empty, MenuAnchor.TOP_LEFT, MenuAutoSize.MATCH_VERTICAL )
  29. {
  30. SetMinimizedArea( -225, 0, 275, 0 );
  31. }
  32. public void OnShaderFunctionLoad()
  33. {
  34. m_functionInputsReordableList = null;
  35. m_functionSwitchesReordableList = null;
  36. m_functionOutputsReordableList = null;
  37. }
  38. public bool Draw( Rect parentPosition, ParentNode selectedNode, Vector2 mousePosition, int mouseButtonId, bool hasKeyboardFocus )
  39. {
  40. bool changeCheck = false;
  41. base.Draw( parentPosition, mousePosition, mouseButtonId, hasKeyboardFocus );
  42. if ( m_nodePropertiesStyle == null )
  43. {
  44. m_nodePropertiesStyle = UIUtils.GetCustomStyle( CustomStyle.NodePropertiesTitle );
  45. m_nodePropertiesStyle.normal.textColor = m_nodePropertiesStyle.active.textColor = EditorGUIUtility.isProSkin ? new Color( 1f, 1f, 1f ) : new Color( 0f, 0f, 0f );
  46. }
  47. if ( m_isMaximized )
  48. {
  49. KeyCode key = Event.current.keyCode;
  50. if ( m_isMouseInside || hasKeyboardFocus )
  51. {
  52. if ( key == ShortcutsManager.ScrollUpKey )
  53. {
  54. m_currentScrollPos.y -= 10;
  55. if ( m_currentScrollPos.y < 0 )
  56. {
  57. m_currentScrollPos.y = 0;
  58. }
  59. Event.current.Use();
  60. }
  61. if ( key == ShortcutsManager.ScrollDownKey )
  62. {
  63. m_currentScrollPos.y += 10;
  64. Event.current.Use();
  65. }
  66. }
  67. if( m_forceUpdate )
  68. {
  69. if( m_propertyReordableList != null )
  70. m_propertyReordableList.ReleaseKeyboardFocus();
  71. m_propertyReordableList = null;
  72. if ( m_functionInputsReordableList != null )
  73. m_functionInputsReordableList.ReleaseKeyboardFocus();
  74. m_functionInputsReordableList = null;
  75. if( m_functionSwitchesReordableList != null )
  76. m_functionSwitchesReordableList.ReleaseKeyboardFocus();
  77. m_functionSwitchesReordableList = null;
  78. if ( m_functionOutputsReordableList != null )
  79. m_functionOutputsReordableList.ReleaseKeyboardFocus();
  80. m_functionOutputsReordableList = null;
  81. m_forceUpdate = false;
  82. }
  83. GUILayout.BeginArea( m_transformedArea, m_content, m_style );
  84. {
  85. //Draw selected node parameters
  86. if ( selectedNode != null )
  87. {
  88. // this hack is need because without it the several FloatFields/Textfields/... would show wrong values ( different from the ones they were assigned to show )
  89. if ( m_lastSelectedNode != selectedNode.UniqueId )
  90. {
  91. m_lastSelectedNode = selectedNode.UniqueId;
  92. GUI.FocusControl( "" );
  93. }
  94. EditorGUILayout.BeginVertical();
  95. {
  96. EditorGUILayout.Separator();
  97. if ( selectedNode.UniqueId == ParentWindow.CurrentGraph.CurrentMasterNodeId )
  98. {
  99. m_dummyContent.text = "Output Node";
  100. }
  101. else
  102. {
  103. if ( selectedNode.Attributes != null )
  104. {
  105. m_dummyContent.text = selectedNode.Attributes.Name;
  106. }
  107. else if ( selectedNode is CommentaryNode )
  108. {
  109. m_dummyContent.text = "Commentary";
  110. }
  111. else
  112. {
  113. m_dummyContent.text = TitleStr;
  114. }
  115. }
  116. EditorGUILayout.LabelField( m_dummyContent, m_nodePropertiesStyle );
  117. EditorGUILayout.Separator();
  118. //UIUtils.RecordObject( selectedNode , "Changing properties on node " + selectedNode.UniqueId);
  119. m_currentScrollPos = EditorGUILayout.BeginScrollView( m_currentScrollPos, GUILayout.Width( 0 ), GUILayout.Height( 0 ) );
  120. float labelWidth = EditorGUIUtility.labelWidth;
  121. if ( selectedNode.TextLabelWidth > 0 )
  122. EditorGUIUtility.labelWidth = selectedNode.TextLabelWidth;
  123. changeCheck = selectedNode.SafeDrawProperties();
  124. EditorGUIUtility.labelWidth = labelWidth;
  125. EditorGUILayout.EndScrollView();
  126. }
  127. EditorGUILayout.EndVertical();
  128. if ( changeCheck )
  129. {
  130. if ( selectedNode.ConnStatus == NodeConnectionStatus.Connected )
  131. ParentWindow.SetSaveIsDirty();
  132. }
  133. }
  134. else
  135. {
  136. //Draw Graph Params
  137. EditorGUILayout.BeginVertical();
  138. {
  139. EditorGUILayout.Separator();
  140. EditorGUILayout.LabelField( "Graph Properties", m_nodePropertiesStyle );
  141. EditorGUILayout.Separator();
  142. m_currentScrollPos = EditorGUILayout.BeginScrollView( m_currentScrollPos, GUILayout.Width( 0 ), GUILayout.Height( 0 ) );
  143. float labelWidth = EditorGUIUtility.labelWidth;
  144. EditorGUIUtility.labelWidth = 90;
  145. bool generalIsVisible = m_parentWindow.InnerWindowVariables.ExpandedGeneralShaderOptions;
  146. NodeUtils.DrawPropertyGroup( ref generalIsVisible, " General", DrawGeneralFunction );
  147. m_parentWindow.InnerWindowVariables.ExpandedGeneralShaderOptions = generalIsVisible;
  148. AmplifyShaderFunction function = ParentWindow.CurrentGraph.CurrentShaderFunction;
  149. if( function != null )
  150. {
  151. //function.AdditionalIncludes.Draw( ParentWindow.CurrentGraph.CurrentOutputNode );
  152. //function.AdditionalPragmas.Draw( ParentWindow.CurrentGraph.CurrentOutputNode );
  153. function.AdditionalDirectives.Draw( ParentWindow.CurrentGraph.CurrentOutputNode );
  154. }
  155. bool inputIsVisible = m_parentWindow.InnerWindowVariables.ExpandedFunctionInputs;
  156. NodeUtils.DrawPropertyGroup( ref inputIsVisible, " Function Inputs", DrawFunctionInputs );
  157. m_parentWindow.InnerWindowVariables.ExpandedFunctionInputs = inputIsVisible;
  158. bool swicthIsVisible = m_parentWindow.InnerWindowVariables.ExpandedFunctionSwitches;
  159. NodeUtils.DrawPropertyGroup( ref swicthIsVisible, " Function Switches", DrawFunctionSwitches );
  160. m_parentWindow.InnerWindowVariables.ExpandedFunctionSwitches = swicthIsVisible;
  161. bool outputIsVisible = m_parentWindow.InnerWindowVariables.ExpandedFunctionOutputs;
  162. NodeUtils.DrawPropertyGroup( ref outputIsVisible, " Function Outputs", DrawFunctionOutputs );
  163. m_parentWindow.InnerWindowVariables.ExpandedFunctionOutputs = outputIsVisible;
  164. bool properties = ParentWindow.InnerWindowVariables.ExpandedProperties;
  165. NodeUtils.DrawPropertyGroup( ref properties, " Material Properties", DrawFunctionProperties );
  166. ParentWindow.InnerWindowVariables.ExpandedProperties = properties;
  167. EditorGUIUtility.labelWidth = labelWidth;
  168. EditorGUILayout.EndScrollView();
  169. }
  170. EditorGUILayout.EndVertical();
  171. }
  172. }
  173. // Close window area
  174. GUILayout.EndArea();
  175. }
  176. PostDraw();
  177. return changeCheck;
  178. }
  179. public void DrawGeneralFunction()
  180. {
  181. AmplifyShaderFunction function = ParentWindow.CurrentGraph.CurrentShaderFunction;
  182. if ( function == null )
  183. return;
  184. float cacheWidth = EditorGUIUtility.labelWidth;
  185. EditorGUIUtility.labelWidth = 115;
  186. SerializedObject serializedObject = new UnityEditor.SerializedObject( function );
  187. if ( serializedObject != null )
  188. {
  189. SerializedProperty temo = serializedObject.FindProperty( "m_description" );
  190. EditorGUILayout.PropertyField( temo, new GUIContent( " Description" ) );
  191. SerializedProperty cat = serializedObject.FindProperty( "m_nodeCategory" );
  192. SerializedProperty ppos = serializedObject.FindProperty( "m_previewPosition" );
  193. EditorGUILayout.PropertyField( ppos, new GUIContent( "Preview Position" ) );
  194. cat.intValue = ParentWindow.CurrentGraph.CurrentOutputNode.EditorGUILayoutPopup( "Category", cat.intValue, UIUtils.CategoryPresets );
  195. if( cat.enumValueIndex == 0 )
  196. {
  197. SerializedProperty custCat = serializedObject.FindProperty( "m_customNodeCategory" );
  198. EditorGUILayout.PropertyField( custCat, new GUIContent( "Custom" ) );
  199. }
  200. serializedObject.ApplyModifiedProperties();
  201. }
  202. EditorGUIUtility.labelWidth = cacheWidth;
  203. }
  204. public void DrawFunctionInputs()
  205. {
  206. List<FunctionInput> functionInputNodes = UIUtils.FunctionInputList();
  207. if ( m_functionInputsReordableList == null || functionInputNodes.Count != m_functionInputsLastCount )
  208. {
  209. functionInputNodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  210. m_functionInputsReordableList = new ReorderableList( functionInputNodes, typeof( FunctionInput ), true, false, false, false );
  211. m_functionInputsReordableList.headerHeight = 0;
  212. m_functionInputsReordableList.footerHeight = 0;
  213. m_functionInputsReordableList.showDefaultBackground = false;
  214. m_functionInputsReordableList.drawElementCallback = ( Rect rect, int index, bool isActive, bool isFocused ) =>
  215. {
  216. EditorGUI.LabelField( rect, functionInputNodes[ index ].InputName );
  217. };
  218. m_functionInputsReordableList.onChangedCallback = ( list ) =>
  219. {
  220. //for ( int i = 0; i < functionInputNodes.Count; i++ )
  221. //{
  222. // functionInputNodes[ i ].OrderIndex = i;
  223. //}
  224. ForceInputReorder( ref functionInputNodes );
  225. };
  226. m_functionInputsLastCount = m_functionInputsReordableList.count;
  227. }
  228. if ( m_functionInputsReordableList != null )
  229. {
  230. if ( m_propertyAdjustment == null )
  231. {
  232. m_propertyAdjustment = new GUIStyle();
  233. m_propertyAdjustment.padding.left = 17;
  234. }
  235. EditorGUILayout.BeginVertical( m_propertyAdjustment );
  236. m_functionInputsReordableList.DoLayoutList();
  237. EditorGUILayout.EndVertical();
  238. }
  239. }
  240. public void ForceInputReorder( ref List<FunctionInput> functionInputNodes )
  241. {
  242. for( int i = 0; i < functionInputNodes.Count; i++ )
  243. {
  244. functionInputNodes[ i ].OrderIndex = i;
  245. }
  246. }
  247. public void DrawFunctionSwitches()
  248. {
  249. List<FunctionSwitch> functionSwitchNodes = UIUtils.FunctionSwitchList();
  250. if( m_functionSwitchesReordableList == null || functionSwitchNodes.Count != m_functionSwitchesLastCount )
  251. {
  252. functionSwitchNodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  253. UIUtils.UpdateFunctionSwitchArr();
  254. m_functionSwitchesReordableList = new ReorderableList( functionSwitchNodes, typeof( FunctionSwitch ), true, false, false, false );
  255. m_functionSwitchesReordableList.headerHeight = 0;
  256. m_functionSwitchesReordableList.footerHeight = 0;
  257. m_functionSwitchesReordableList.showDefaultBackground = false;
  258. m_functionSwitchesReordableList.drawElementCallback = ( Rect rect, int index, bool isActive, bool isFocused ) =>
  259. {
  260. EditorGUI.LabelField( rect, functionSwitchNodes[ index ].OptionLabel );
  261. };
  262. m_functionSwitchesReordableList.onChangedCallback = ( list ) =>
  263. {
  264. ForceSwitchesReorder(ref functionSwitchNodes );
  265. };
  266. m_functionSwitchesLastCount = m_functionSwitchesReordableList.count;
  267. }
  268. if( m_functionSwitchesReordableList != null )
  269. {
  270. if( m_propertyAdjustment == null )
  271. {
  272. m_propertyAdjustment = new GUIStyle();
  273. m_propertyAdjustment.padding.left = 17;
  274. }
  275. EditorGUILayout.BeginVertical( m_propertyAdjustment );
  276. m_functionSwitchesReordableList.DoLayoutList();
  277. EditorGUILayout.EndVertical();
  278. }
  279. }
  280. public void ForceSwitchesReorder( ref List<FunctionSwitch> functionSwitchNodes )
  281. {
  282. for( int i = 0; i < functionSwitchNodes.Count; i++ )
  283. {
  284. functionSwitchNodes[ i ].OrderIndex = i;
  285. }
  286. UIUtils.UpdateFunctionSwitchArr();
  287. }
  288. public void DrawFunctionOutputs()
  289. {
  290. List<FunctionOutput> functionOutputNodes = UIUtils.FunctionOutputList();
  291. if ( m_functionOutputsReordableList == null || functionOutputNodes.Count != m_functionOutputsLastCount )
  292. {
  293. functionOutputNodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  294. m_functionOutputsReordableList = new ReorderableList( functionOutputNodes, typeof( FunctionOutput ), true, false, false, false );
  295. m_functionOutputsReordableList.headerHeight = 0;
  296. m_functionOutputsReordableList.footerHeight = 0;
  297. m_functionOutputsReordableList.showDefaultBackground = false;
  298. m_functionOutputsReordableList.drawElementCallback = ( Rect rect, int index, bool isActive, bool isFocused ) =>
  299. {
  300. EditorGUI.LabelField( rect, functionOutputNodes[ index ].OutputName );
  301. };
  302. m_functionOutputsReordableList.onChangedCallback = ( list ) =>
  303. {
  304. for ( int i = 0; i < functionOutputNodes.Count; i++ )
  305. {
  306. functionOutputNodes[ i ].OrderIndex = i;
  307. }
  308. };
  309. m_functionOutputsLastCount = m_functionOutputsReordableList.count;
  310. }
  311. if ( m_functionOutputsReordableList != null )
  312. {
  313. if ( m_propertyAdjustment == null )
  314. {
  315. m_propertyAdjustment = new GUIStyle();
  316. m_propertyAdjustment.padding.left = 17;
  317. }
  318. EditorGUILayout.BeginVertical( m_propertyAdjustment );
  319. m_functionOutputsReordableList.DoLayoutList();
  320. EditorGUILayout.EndVertical();
  321. }
  322. }
  323. public void DrawFunctionProperties()
  324. {
  325. List<PropertyNode> nodes = UIUtils.PropertyNodesList();
  326. if ( m_propertyReordableList == null || nodes.Count != m_lastCount )
  327. {
  328. m_propertyReordableNodes.Clear();
  329. for ( int i = 0; i < nodes.Count; i++ )
  330. {
  331. ReordenatorNode rnode = nodes[ i ] as ReordenatorNode;
  332. if ( ( rnode == null || !rnode.IsInside ) && ( !m_propertyReordableNodes.Exists( x => x.PropertyName.Equals( nodes[ i ].PropertyName ) ) ) )
  333. m_propertyReordableNodes.Add( nodes[ i ] );
  334. }
  335. m_propertyReordableNodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  336. m_propertyReordableList = new ReorderableList( m_propertyReordableNodes, typeof( PropertyNode ), true, false, false, false );
  337. m_propertyReordableList.headerHeight = 0;
  338. m_propertyReordableList.footerHeight = 0;
  339. m_propertyReordableList.showDefaultBackground = false;
  340. m_propertyReordableList.drawElementCallback = ( Rect rect, int index, bool isActive, bool isFocused ) =>
  341. {
  342. EditorGUI.LabelField( rect, /*m_propertyReordableNodes[ index ].OrderIndex + " " + */m_propertyReordableNodes[ index ].PropertyInspectorName );
  343. };
  344. m_propertyReordableList.onChangedCallback = ( list ) =>
  345. {
  346. ReorderList( ref nodes );
  347. };
  348. ReorderList( ref nodes );
  349. m_lastCount = m_propertyReordableList.count;
  350. }
  351. if ( m_propertyReordableList != null )
  352. {
  353. if ( m_propertyAdjustment == null )
  354. {
  355. m_propertyAdjustment = new GUIStyle();
  356. m_propertyAdjustment.padding.left = 17;
  357. }
  358. EditorGUILayout.BeginVertical( m_propertyAdjustment );
  359. m_propertyReordableList.DoLayoutList();
  360. EditorGUILayout.EndVertical();
  361. }
  362. }
  363. public void ForceReordering()
  364. {
  365. List<PropertyNode> nodes = UIUtils.PropertyNodesList();
  366. ReorderList( ref nodes );
  367. List<FunctionInput> functionInputNodes = UIUtils.FunctionInputList();
  368. ForceInputReorder( ref functionInputNodes );
  369. List<FunctionSwitch> functionSwitchNodes = UIUtils.FunctionSwitchList();
  370. ForceSwitchesReorder( ref functionSwitchNodes );
  371. //RecursiveLog();
  372. }
  373. private void RecursiveLog()
  374. {
  375. List<PropertyNode> nodes = UIUtils.PropertyNodesList();
  376. nodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  377. for( int i = 0; i < nodes.Count; i++ )
  378. {
  379. if( ( nodes[ i ] is ReordenatorNode ) )
  380. ( nodes[ i ] as ReordenatorNode ).RecursiveLog();
  381. else
  382. Debug.Log( nodes[ i ].OrderIndex + " " + nodes[ i ].PropertyName );
  383. }
  384. }
  385. private void ReorderList( ref List<PropertyNode> nodes )
  386. {
  387. // clear lock list before reordering because of multiple sf being used
  388. for( int i = 0; i < nodes.Count; i++ )
  389. {
  390. ReordenatorNode rnode = nodes[ i ] as ReordenatorNode;
  391. if ( rnode != null )
  392. rnode.RecursiveClear();
  393. }
  394. int propoffset = 0;
  395. int count = 0;
  396. for ( int i = 0; i < m_propertyReordableNodes.Count; i++ )
  397. {
  398. ReordenatorNode renode = m_propertyReordableNodes[ i ] as ReordenatorNode;
  399. if ( renode != null )
  400. {
  401. if ( !renode.IsInside )
  402. {
  403. m_propertyReordableNodes[ i ].OrderIndex = count + propoffset;
  404. if ( renode.PropertyListCount > 0 )
  405. {
  406. propoffset += renode.RecursiveCount();
  407. // the same reordenator can exist multiple times, apply ordering to all of them
  408. for( int j = 0; j < nodes.Count; j++ )
  409. {
  410. ReordenatorNode pnode = ( nodes[ j ] as ReordenatorNode );
  411. if ( pnode != null && pnode.PropertyName.Equals( renode.PropertyName ) )
  412. {
  413. pnode.OrderIndex = renode.RawOrderIndex;
  414. pnode.RecursiveSetOrderOffset( renode.RawOrderIndex, true );
  415. }
  416. }
  417. }
  418. else
  419. {
  420. count++;
  421. }
  422. }
  423. else
  424. {
  425. m_propertyReordableNodes[ i ].OrderIndex = 0;
  426. }
  427. }
  428. else
  429. {
  430. m_propertyReordableNodes[ i ].OrderIndex = count + propoffset;
  431. count++;
  432. }
  433. }
  434. }
  435. public override void Destroy()
  436. {
  437. base.Destroy();
  438. m_functionInputsReordableList = null;
  439. m_functionOutputsReordableList = null;
  440. m_propertyReordableList = null;
  441. }
  442. public bool ForceUpdate
  443. {
  444. get { return m_forceUpdate; }
  445. set { m_forceUpdate = value; }
  446. }
  447. }
  448. }