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.

557 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, 285, 0, string.Empty, MenuAnchor.TOP_LEFT, MenuAutoSize.MATCH_VERTICAL )
  29. {
  30. SetMinimizedArea( -225, 0, 260, 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. //else
  124. EditorGUIUtility.labelWidth = TransformedArea.width * 0.42f;
  125. changeCheck = selectedNode.SafeDrawProperties();
  126. EditorGUIUtility.labelWidth = labelWidth;
  127. EditorGUILayout.EndScrollView();
  128. }
  129. EditorGUILayout.EndVertical();
  130. if ( changeCheck )
  131. {
  132. if ( selectedNode.ConnStatus == NodeConnectionStatus.Connected )
  133. ParentWindow.SetSaveIsDirty();
  134. }
  135. }
  136. else
  137. {
  138. //Draw Graph Params
  139. EditorGUILayout.BeginVertical();
  140. {
  141. EditorGUILayout.Separator();
  142. EditorGUILayout.LabelField( "Graph Properties", m_nodePropertiesStyle );
  143. EditorGUILayout.Separator();
  144. m_currentScrollPos = EditorGUILayout.BeginScrollView( m_currentScrollPos, GUILayout.Width( 0 ), GUILayout.Height( 0 ) );
  145. float labelWidth = EditorGUIUtility.labelWidth;
  146. EditorGUIUtility.labelWidth = 90;
  147. bool generalIsVisible = m_parentWindow.InnerWindowVariables.ExpandedGeneralShaderOptions;
  148. NodeUtils.DrawPropertyGroup( ref generalIsVisible, " General", DrawGeneralFunction );
  149. m_parentWindow.InnerWindowVariables.ExpandedGeneralShaderOptions = generalIsVisible;
  150. AmplifyShaderFunction function = ParentWindow.CurrentGraph.CurrentShaderFunction;
  151. if( function != null )
  152. {
  153. //function.AdditionalIncludes.Draw( ParentWindow.CurrentGraph.CurrentOutputNode );
  154. //function.AdditionalPragmas.Draw( ParentWindow.CurrentGraph.CurrentOutputNode );
  155. function.AdditionalDirectives.Draw( ParentWindow.CurrentGraph.CurrentOutputNode );
  156. }
  157. bool inputIsVisible = m_parentWindow.InnerWindowVariables.ExpandedFunctionInputs;
  158. NodeUtils.DrawPropertyGroup( ref inputIsVisible, " Function Inputs", DrawFunctionInputs );
  159. m_parentWindow.InnerWindowVariables.ExpandedFunctionInputs = inputIsVisible;
  160. bool swicthIsVisible = m_parentWindow.InnerWindowVariables.ExpandedFunctionSwitches;
  161. NodeUtils.DrawPropertyGroup( ref swicthIsVisible, " Function Switches", DrawFunctionSwitches );
  162. m_parentWindow.InnerWindowVariables.ExpandedFunctionSwitches = swicthIsVisible;
  163. bool outputIsVisible = m_parentWindow.InnerWindowVariables.ExpandedFunctionOutputs;
  164. NodeUtils.DrawPropertyGroup( ref outputIsVisible, " Function Outputs", DrawFunctionOutputs );
  165. m_parentWindow.InnerWindowVariables.ExpandedFunctionOutputs = outputIsVisible;
  166. bool properties = ParentWindow.InnerWindowVariables.ExpandedProperties;
  167. NodeUtils.DrawPropertyGroup( ref properties, " Material Properties", DrawFunctionProperties );
  168. ParentWindow.InnerWindowVariables.ExpandedProperties = properties;
  169. EditorGUIUtility.labelWidth = labelWidth;
  170. EditorGUILayout.EndScrollView();
  171. }
  172. EditorGUILayout.EndVertical();
  173. }
  174. }
  175. // Close window area
  176. GUILayout.EndArea();
  177. }
  178. PostDraw();
  179. return changeCheck;
  180. }
  181. public void DrawGeneralFunction()
  182. {
  183. AmplifyShaderFunction function = ParentWindow.CurrentGraph.CurrentShaderFunction;
  184. if ( function == null )
  185. return;
  186. float cacheWidth = EditorGUIUtility.labelWidth;
  187. EditorGUIUtility.labelWidth = 115;
  188. SerializedObject serializedObject = new UnityEditor.SerializedObject( function );
  189. if ( serializedObject != null )
  190. {
  191. SerializedProperty temo = serializedObject.FindProperty( "m_description" );
  192. EditorGUILayout.PropertyField( temo, new GUIContent( " Description" ) );
  193. SerializedProperty cat = serializedObject.FindProperty( "m_nodeCategory" );
  194. SerializedProperty ppos = serializedObject.FindProperty( "m_previewPosition" );
  195. EditorGUILayout.PropertyField( ppos, new GUIContent( "Preview Position" ) );
  196. cat.intValue = ParentWindow.CurrentGraph.CurrentOutputNode.EditorGUILayoutPopup( "Category", cat.intValue, UIUtils.CategoryPresets );
  197. if( cat.enumValueIndex == 0 )
  198. {
  199. SerializedProperty custCat = serializedObject.FindProperty( "m_customNodeCategory" );
  200. EditorGUILayout.PropertyField( custCat, new GUIContent( "Custom" ) );
  201. }
  202. SerializedProperty hidden = serializedObject.FindProperty( "m_hidden" );
  203. EditorGUILayout.PropertyField( hidden, new GUIContent( "Hidden" ) );
  204. serializedObject.ApplyModifiedProperties();
  205. }
  206. EditorGUIUtility.labelWidth = cacheWidth;
  207. }
  208. public void DrawFunctionInputs()
  209. {
  210. List<FunctionInput> functionInputNodes = UIUtils.FunctionInputList();
  211. if ( m_functionInputsReordableList == null || functionInputNodes.Count != m_functionInputsLastCount )
  212. {
  213. functionInputNodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  214. m_functionInputsReordableList = new ReorderableList( functionInputNodes, typeof( FunctionInput ), true, false, false, false );
  215. m_functionInputsReordableList.headerHeight = 0;
  216. m_functionInputsReordableList.footerHeight = 0;
  217. m_functionInputsReordableList.showDefaultBackground = false;
  218. m_functionInputsReordableList.drawElementCallback = ( Rect rect, int index, bool isActive, bool isFocused ) =>
  219. {
  220. EditorGUI.LabelField( rect, functionInputNodes[ index ].InputName );
  221. };
  222. m_functionInputsReordableList.onChangedCallback = ( list ) =>
  223. {
  224. //for ( int i = 0; i < functionInputNodes.Count; i++ )
  225. //{
  226. // functionInputNodes[ i ].OrderIndex = i;
  227. //}
  228. ForceInputReorder( ref functionInputNodes );
  229. };
  230. m_functionInputsLastCount = m_functionInputsReordableList.count;
  231. }
  232. if ( m_functionInputsReordableList != null )
  233. {
  234. if ( m_propertyAdjustment == null )
  235. {
  236. m_propertyAdjustment = new GUIStyle();
  237. m_propertyAdjustment.padding.left = 17;
  238. }
  239. EditorGUILayout.BeginVertical( m_propertyAdjustment );
  240. m_functionInputsReordableList.DoLayoutList();
  241. EditorGUILayout.EndVertical();
  242. }
  243. }
  244. public void ForceInputReorder( ref List<FunctionInput> functionInputNodes )
  245. {
  246. for( int i = 0; i < functionInputNodes.Count; i++ )
  247. {
  248. functionInputNodes[ i ].OrderIndex = i;
  249. }
  250. }
  251. public void DrawFunctionSwitches()
  252. {
  253. List<FunctionSwitch> functionSwitchNodes = UIUtils.FunctionSwitchList();
  254. if( m_functionSwitchesReordableList == null || functionSwitchNodes.Count != m_functionSwitchesLastCount )
  255. {
  256. functionSwitchNodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  257. UIUtils.UpdateFunctionSwitchArr();
  258. m_functionSwitchesReordableList = new ReorderableList( functionSwitchNodes, typeof( FunctionSwitch ), true, false, false, false );
  259. m_functionSwitchesReordableList.headerHeight = 0;
  260. m_functionSwitchesReordableList.footerHeight = 0;
  261. m_functionSwitchesReordableList.showDefaultBackground = false;
  262. m_functionSwitchesReordableList.drawElementCallback = ( Rect rect, int index, bool isActive, bool isFocused ) =>
  263. {
  264. EditorGUI.LabelField( rect, functionSwitchNodes[ index ].OptionLabel );
  265. };
  266. m_functionSwitchesReordableList.onChangedCallback = ( list ) =>
  267. {
  268. ForceSwitchesReorder(ref functionSwitchNodes );
  269. };
  270. m_functionSwitchesLastCount = m_functionSwitchesReordableList.count;
  271. }
  272. if( m_functionSwitchesReordableList != null )
  273. {
  274. if( m_propertyAdjustment == null )
  275. {
  276. m_propertyAdjustment = new GUIStyle();
  277. m_propertyAdjustment.padding.left = 17;
  278. }
  279. EditorGUILayout.BeginVertical( m_propertyAdjustment );
  280. m_functionSwitchesReordableList.DoLayoutList();
  281. EditorGUILayout.EndVertical();
  282. }
  283. }
  284. public void ForceSwitchesReorder( ref List<FunctionSwitch> functionSwitchNodes )
  285. {
  286. for( int i = 0; i < functionSwitchNodes.Count; i++ )
  287. {
  288. functionSwitchNodes[ i ].OrderIndex = i;
  289. }
  290. UIUtils.UpdateFunctionSwitchArr();
  291. }
  292. public void DrawFunctionOutputs()
  293. {
  294. List<FunctionOutput> functionOutputNodes = UIUtils.FunctionOutputList();
  295. if ( m_functionOutputsReordableList == null || functionOutputNodes.Count != m_functionOutputsLastCount )
  296. {
  297. functionOutputNodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  298. m_functionOutputsReordableList = new ReorderableList( functionOutputNodes, typeof( FunctionOutput ), true, false, false, false );
  299. m_functionOutputsReordableList.headerHeight = 0;
  300. m_functionOutputsReordableList.footerHeight = 0;
  301. m_functionOutputsReordableList.showDefaultBackground = false;
  302. m_functionOutputsReordableList.drawElementCallback = ( Rect rect, int index, bool isActive, bool isFocused ) =>
  303. {
  304. EditorGUI.LabelField( rect, functionOutputNodes[ index ].OutputName );
  305. };
  306. m_functionOutputsReordableList.onChangedCallback = ( list ) =>
  307. {
  308. for ( int i = 0; i < functionOutputNodes.Count; i++ )
  309. {
  310. functionOutputNodes[ i ].OrderIndex = i;
  311. }
  312. };
  313. m_functionOutputsLastCount = m_functionOutputsReordableList.count;
  314. }
  315. if ( m_functionOutputsReordableList != null )
  316. {
  317. if ( m_propertyAdjustment == null )
  318. {
  319. m_propertyAdjustment = new GUIStyle();
  320. m_propertyAdjustment.padding.left = 17;
  321. }
  322. EditorGUILayout.BeginVertical( m_propertyAdjustment );
  323. m_functionOutputsReordableList.DoLayoutList();
  324. EditorGUILayout.EndVertical();
  325. }
  326. }
  327. private void RefreshVisibleList( ref List<PropertyNode> allNodes )
  328. {
  329. // temp reference for lambda expression
  330. List<PropertyNode> nodes = allNodes;
  331. m_propertyReordableNodes.Clear();
  332. for( int i = 0; i < nodes.Count; i++ )
  333. {
  334. ReordenatorNode rnode = nodes[ i ] as ReordenatorNode;
  335. if( ( rnode == null || !rnode.IsInside ) && ( !m_propertyReordableNodes.Exists( x => x.PropertyName.Equals( nodes[ i ].PropertyName ) ) ) )
  336. m_propertyReordableNodes.Add( nodes[ i ] );
  337. }
  338. m_propertyReordableNodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  339. }
  340. public void DrawFunctionProperties()
  341. {
  342. List<PropertyNode> nodes = UIUtils.PropertyNodesList();
  343. if( nodes.Count != m_lastCount )
  344. {
  345. RefreshVisibleList( ref nodes );
  346. m_lastCount = nodes.Count;
  347. }
  348. if( m_propertyReordableList == null )
  349. {
  350. m_propertyReordableList = new ReorderableList( m_propertyReordableNodes, typeof( PropertyNode ), true, false, false, false )
  351. {
  352. headerHeight = 0,
  353. footerHeight = 0,
  354. showDefaultBackground = false,
  355. drawElementCallback = ( Rect rect, int index, bool isActive, bool isFocused ) =>
  356. {
  357. var first = rect;
  358. first.width *= 0.60f;
  359. EditorGUI.LabelField( first, m_propertyReordableNodes[ index ].PropertyInspectorName );
  360. var second = rect;
  361. second.width *= 0.4f;
  362. second.x += first.width;
  363. if( GUI.Button( second, m_propertyReordableNodes[ index ].PropertyName, new GUIStyle( "AssetLabel Partial" ) ) )
  364. {
  365. UIUtils.FocusOnNode( m_propertyReordableNodes[ index ], 1, false );
  366. }
  367. },
  368. onReorderCallback = ( list ) =>
  369. {
  370. ReorderList( ref nodes );
  371. }
  372. };
  373. ReorderList( ref nodes );
  374. }
  375. if( m_propertyReordableList != null )
  376. {
  377. if( m_propertyAdjustment == null )
  378. {
  379. m_propertyAdjustment = new GUIStyle();
  380. m_propertyAdjustment.padding.left = 17;
  381. }
  382. EditorGUILayout.BeginVertical( m_propertyAdjustment );
  383. m_propertyReordableList.DoLayoutList();
  384. EditorGUILayout.EndVertical();
  385. }
  386. }
  387. public void ForceReordering()
  388. {
  389. List<PropertyNode> nodes = UIUtils.PropertyNodesList();
  390. ReorderList( ref nodes );
  391. List<FunctionInput> functionInputNodes = UIUtils.FunctionInputList();
  392. ForceInputReorder( ref functionInputNodes );
  393. List<FunctionSwitch> functionSwitchNodes = UIUtils.FunctionSwitchList();
  394. ForceSwitchesReorder( ref functionSwitchNodes );
  395. //RecursiveLog();
  396. }
  397. private void RecursiveLog()
  398. {
  399. List<PropertyNode> nodes = UIUtils.PropertyNodesList();
  400. nodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  401. for( int i = 0; i < nodes.Count; i++ )
  402. {
  403. if( ( nodes[ i ] is ReordenatorNode ) )
  404. ( nodes[ i ] as ReordenatorNode ).RecursiveLog();
  405. else
  406. Debug.Log( nodes[ i ].OrderIndex + " " + nodes[ i ].PropertyName );
  407. }
  408. }
  409. private void ReorderList( ref List<PropertyNode> nodes )
  410. {
  411. // clear lock list before reordering because of multiple sf being used
  412. for( int i = 0; i < nodes.Count; i++ )
  413. {
  414. ReordenatorNode rnode = nodes[ i ] as ReordenatorNode;
  415. if ( rnode != null )
  416. rnode.RecursiveClear();
  417. }
  418. int propoffset = 0;
  419. int count = 0;
  420. for ( int i = 0; i < m_propertyReordableNodes.Count; i++ )
  421. {
  422. ReordenatorNode renode = m_propertyReordableNodes[ i ] as ReordenatorNode;
  423. if ( renode != null )
  424. {
  425. if ( !renode.IsInside )
  426. {
  427. m_propertyReordableNodes[ i ].OrderIndex = count + propoffset;
  428. if ( renode.PropertyListCount > 0 )
  429. {
  430. propoffset += renode.RecursiveCount();
  431. // the same reordenator can exist multiple times, apply ordering to all of them
  432. for( int j = 0; j < nodes.Count; j++ )
  433. {
  434. ReordenatorNode pnode = ( nodes[ j ] as ReordenatorNode );
  435. if ( pnode != null && pnode.PropertyName.Equals( renode.PropertyName ) )
  436. {
  437. pnode.OrderIndex = renode.RawOrderIndex;
  438. pnode.RecursiveSetOrderOffset( renode.RawOrderIndex, true );
  439. }
  440. }
  441. }
  442. else
  443. {
  444. count++;
  445. }
  446. }
  447. else
  448. {
  449. m_propertyReordableNodes[ i ].OrderIndex = 0;
  450. }
  451. }
  452. else
  453. {
  454. m_propertyReordableNodes[ i ].OrderIndex = count + propoffset;
  455. count++;
  456. }
  457. }
  458. }
  459. public override void Destroy()
  460. {
  461. base.Destroy();
  462. m_functionInputsReordableList = null;
  463. m_functionOutputsReordableList = null;
  464. m_propertyReordableList = null;
  465. }
  466. public bool ForceUpdate
  467. {
  468. get { return m_forceUpdate; }
  469. set { m_forceUpdate = value; }
  470. }
  471. }
  472. }