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.

862 lines
28 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.Collections.Generic;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. public class TemplateOptionsUIHelper
  11. {
  12. private const string CustomOptionsLabel = " Custom Options";
  13. private bool m_isSubShader = false;
  14. [SerializeField]
  15. private bool m_passCustomOptionsFoldout = true;
  16. [SerializeField]
  17. private string m_passCustomOptionsLabel = CustomOptionsLabel;
  18. [SerializeField]
  19. private int m_passCustomOptionsSizeCheck = 0;
  20. [SerializeField]
  21. private List<TemplateOptionUIItem> m_passCustomOptionsUI = new List<TemplateOptionUIItem>();
  22. [NonSerialized]
  23. private Dictionary<string, TemplateOptionUIItem> m_passCustomOptionsUIDict = new Dictionary<string, TemplateOptionUIItem>();
  24. [NonSerialized]
  25. private TemplateMultiPassMasterNode m_owner;
  26. [NonSerialized]
  27. private string[] m_readOptionNames;
  28. [NonSerialized]
  29. private string[] m_readOptionSelections;
  30. [SerializeField]
  31. private List<TemplateOptionPortItem> m_passCustomOptionsPorts = new List<TemplateOptionPortItem>();
  32. public TemplateOptionsUIHelper( bool isSubShader )
  33. {
  34. m_isSubShader = isSubShader;
  35. }
  36. public void CopyOptionsValuesFrom( TemplateOptionsUIHelper origin )
  37. {
  38. for( int i = 0; i < origin.PassCustomOptionsUI.Count; i++ )
  39. {
  40. m_passCustomOptionsUI[ i ].CopyValuesFrom( origin.PassCustomOptionsUI[ i ] );
  41. }
  42. }
  43. public void Destroy()
  44. {
  45. for( int i = 0; i < m_passCustomOptionsUI.Count; i++ )
  46. {
  47. m_passCustomOptionsUI[ i ].Destroy();
  48. }
  49. m_passCustomOptionsUI.Clear();
  50. m_passCustomOptionsUI = null;
  51. m_passCustomOptionsUIDict.Clear();
  52. m_passCustomOptionsUIDict = null;
  53. m_passCustomOptionsPorts.Clear();
  54. m_passCustomOptionsPorts = null;
  55. }
  56. public void DrawCustomOptions( TemplateMultiPassMasterNode owner )
  57. {
  58. m_owner = owner;
  59. if( m_passCustomOptionsUI.Count > 0 )
  60. {
  61. NodeUtils.DrawNestedPropertyGroup( ref m_passCustomOptionsFoldout, m_passCustomOptionsLabel, DrawCustomOptionsBlock );
  62. }
  63. }
  64. public void DrawCustomOptionsBlock()
  65. {
  66. float currWidth = EditorGUIUtility.labelWidth;
  67. #if UNITY_2019_3_OR_NEWER
  68. float size = Mathf.Max( UIUtils.CurrentWindow.ParametersWindow.TransformedArea.width * 0.385f, 0 );
  69. #else
  70. float size = Mathf.Max( UIUtils.CurrentWindow.ParametersWindow.TransformedArea.width * 0.34f, 0 );
  71. #endif
  72. EditorGUIUtility.labelWidth = size;
  73. for( int i = 0; i < m_passCustomOptionsUI.Count; i++ )
  74. {
  75. m_passCustomOptionsUI[ i ].Draw( m_owner );
  76. }
  77. EditorGUILayout.Space();
  78. EditorGUIUtility.labelWidth = currWidth;
  79. }
  80. public void OnCustomOptionSelected( bool isRefreshing, bool invertAction, TemplateMultiPassMasterNode owner, TemplateOptionUIItem uiItem, params TemplateActionItem[] validActions )
  81. {
  82. uiItem.CheckOnExecute = false;
  83. for( int i = 0; i < validActions.Length; i++ )
  84. {
  85. AseOptionsActionType actionType = validActions[ i ].ActionType;
  86. if( invertAction )
  87. {
  88. if( !TemplateOptionsToolsHelper.InvertAction( validActions[ i ].ActionType, ref actionType ) )
  89. {
  90. continue;
  91. }
  92. }
  93. switch( actionType )
  94. {
  95. case AseOptionsActionType.ShowOption:
  96. {
  97. TemplateOptionUIItem item = m_passCustomOptionsUI.Find( x => ( x.Options.Name.Equals( validActions[ i ].ActionData ) ) );
  98. if( item != null )
  99. {
  100. if( isRefreshing )
  101. {
  102. string optionId = validActions[ i ].PassName + validActions[ i ].ActionData + "Option";
  103. owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionId, true );
  104. }
  105. // this prevents options from showing up when loading by checking if they were hidden by another option
  106. // it works on the assumption that an option that may possible hide this one is checked first
  107. if( !isRefreshing )
  108. item.IsVisible = true;
  109. else if( item.WasVisible )
  110. item.IsVisible = true;
  111. if( !invertAction && validActions[ i ].ActionDataIdx > -1 )
  112. item.CurrentOption = validActions[ i ].ActionDataIdx;
  113. item.CheckEnDisable();
  114. }
  115. else
  116. {
  117. Debug.LogFormat( "Could not find Option {0} for action {1}", validActions[ i ].ActionData, validActions[ i ].ActionType );
  118. }
  119. }
  120. break;
  121. case AseOptionsActionType.HideOption:
  122. {
  123. TemplateOptionUIItem item = m_passCustomOptionsUI.Find( x => ( x.Options.Name.Equals( validActions[ i ].ActionData ) ) );
  124. if( item != null )
  125. {
  126. bool flag = false;
  127. if( isRefreshing )
  128. {
  129. string optionId = validActions[ i ].PassName + validActions[ i ].ActionData + "Option";
  130. flag = owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionId, false );
  131. }
  132. item.IsVisible = false || flag;
  133. if( !invertAction && validActions[ i ].ActionDataIdx > -1 )
  134. item.CurrentOption = validActions[ i ].ActionDataIdx;
  135. item.CheckEnDisable();
  136. }
  137. else
  138. {
  139. Debug.LogFormat( "Could not find Option {0} for action {1}", validActions[ i ].ActionData, validActions[ i ].ActionType );
  140. }
  141. }
  142. break;
  143. case AseOptionsActionType.SetOption:
  144. {
  145. if( !uiItem.IsVisible )
  146. break;
  147. TemplateOptionUIItem item = m_passCustomOptionsUI.Find( x => ( x.Options.Name.Equals( validActions[ i ].ActionData ) ) );
  148. if( item != null )
  149. {
  150. item.CurrentOption = validActions[ i ].ActionDataIdx;
  151. item.Refresh();
  152. }
  153. else
  154. {
  155. Debug.LogFormat( "Could not find Option {0} for action {1}", validActions[ i ].ActionData, validActions[ i ].ActionType );
  156. }
  157. }
  158. break;
  159. case AseOptionsActionType.HidePort:
  160. {
  161. TemplateMultiPassMasterNode passMasterNode = owner;
  162. if( !string.IsNullOrEmpty( validActions[ i ].PassName ) )
  163. {
  164. passMasterNode = owner.ContainerGraph.GetMasterNodeOfPass( validActions[ i ].PassName,owner.LODIndex );
  165. }
  166. if( passMasterNode != null )
  167. {
  168. InputPort port = validActions[ i ].ActionDataIdx > -1 ?
  169. passMasterNode.GetInputPortByUniqueId( validActions[ i ].ActionDataIdx ) :
  170. passMasterNode.InputPorts.Find( x => x.Name.Equals( validActions[ i ].ActionData ) );
  171. if( port != null )
  172. {
  173. if( isRefreshing )
  174. {
  175. string optionId = validActions[ i ].PassName + port.Name;
  176. owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionId, port.IsConnected );
  177. port.Visible = port.IsConnected;
  178. }
  179. else
  180. {
  181. port.Visible = false;
  182. }
  183. passMasterNode.SizeIsDirty = true;
  184. }
  185. else
  186. {
  187. Debug.LogFormat( "Could not find port {0},{1} for action {2}", validActions[ i ].ActionDataIdx, validActions[ i ].ActionData, validActions[ i ].ActionType );
  188. }
  189. }
  190. else
  191. {
  192. Debug.LogFormat( "Could not find pass {0} for action {1} on {2}", validActions[ i ].PassName, validActions[ i ].ActionType, validActions[ i ].ActionData );
  193. }
  194. }
  195. break;
  196. case AseOptionsActionType.ShowPort:
  197. {
  198. if( !uiItem.IsVisible )
  199. break;
  200. TemplateMultiPassMasterNode passMasterNode = owner;
  201. if( !string.IsNullOrEmpty( validActions[ i ].PassName ) )
  202. {
  203. passMasterNode = owner.ContainerGraph.GetMasterNodeOfPass( validActions[ i ].PassName, owner.LODIndex );
  204. }
  205. if( passMasterNode != null )
  206. {
  207. InputPort port = validActions[ i ].ActionDataIdx > -1 ?
  208. passMasterNode.GetInputPortByUniqueId( validActions[ i ].ActionDataIdx ) :
  209. passMasterNode.InputPorts.Find( x => x.Name.Equals( validActions[ i ].ActionData ) );
  210. if( port != null )
  211. {
  212. if( isRefreshing )
  213. {
  214. string optionId = validActions[ i ].PassName + port.Name;
  215. owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionId, true );
  216. }
  217. port.Visible = true;
  218. passMasterNode.SizeIsDirty = true;
  219. }
  220. else
  221. {
  222. Debug.LogFormat( "Could not find port {0},{1} for action {2}", validActions[ i ].ActionDataIdx, validActions[ i ].ActionData, validActions[ i ].ActionType );
  223. }
  224. }
  225. else
  226. {
  227. Debug.LogFormat( "Could not find pass {0} for action {1} on {2}", validActions[ i ].PassName, validActions[ i ].ActionType, validActions[ i ].ActionData );
  228. }
  229. }
  230. break;
  231. case AseOptionsActionType.SetPortName:
  232. {
  233. if( !uiItem.IsVisible )
  234. break;
  235. TemplateMultiPassMasterNode passMasterNode = owner;
  236. if( !string.IsNullOrEmpty( validActions[ i ].PassName ) )
  237. {
  238. passMasterNode = owner.ContainerGraph.GetMasterNodeOfPass( validActions[ i ].PassName, owner.LODIndex );
  239. }
  240. if( passMasterNode != null )
  241. {
  242. InputPort port = passMasterNode.GetInputPortByUniqueId( validActions[ i ].ActionDataIdx );
  243. if( port != null )
  244. {
  245. port.Name = validActions[ i ].ActionData;
  246. passMasterNode.SizeIsDirty = true;
  247. }
  248. else
  249. {
  250. Debug.LogFormat( "Could not find port {0},{1} for action {2}", validActions[ i ].ActionDataIdx, validActions[ i ].ActionData, validActions[ i ].ActionType );
  251. }
  252. }
  253. else
  254. {
  255. Debug.LogFormat( "Could not find pass {0} for action {1} on {2}", validActions[ i ].PassName, validActions[ i ].ActionType, validActions[ i ].ActionData );
  256. }
  257. }
  258. break;
  259. case AseOptionsActionType.SetDefine:
  260. {
  261. if( !uiItem.IsVisible )
  262. {
  263. uiItem.CheckOnExecute = true;
  264. break;
  265. }
  266. //Debug.Log( "DEFINE " + validActions[ i ].ActionData );
  267. if( validActions[ i ].AllPasses )
  268. {
  269. string actionData = validActions[ i ].ActionData;
  270. string defineValue = string.Empty;
  271. if( actionData.StartsWith( "pragma" ) )
  272. {
  273. defineValue = "#" + actionData;
  274. }
  275. else
  276. {
  277. defineValue = "#define " + validActions[ i ].ActionData;
  278. }
  279. if( isRefreshing )
  280. {
  281. owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( defineValue, true );
  282. }
  283. List<TemplateMultiPassMasterNode> nodes = owner.ContainerGraph.GetMultiPassMasterNodes( owner.LODIndex );
  284. int count = nodes.Count;
  285. for( int nodeIdx = 0; nodeIdx < count; nodeIdx++ )
  286. {
  287. nodes[ nodeIdx ].OptionsDefineContainer.AddDefine( defineValue, false );
  288. }
  289. }
  290. else if( !string.IsNullOrEmpty( validActions[ i ].PassName ) )
  291. {
  292. TemplateMultiPassMasterNode passMasterNode = owner.ContainerGraph.GetMasterNodeOfPass( validActions[ i ].PassName, owner.LODIndex );
  293. if( passMasterNode != null )
  294. {
  295. string actionData = validActions[ i ].ActionData;
  296. string defineValue = string.Empty;
  297. if( actionData.StartsWith( "pragma" ) )
  298. {
  299. defineValue = "#" + actionData;
  300. }
  301. else
  302. {
  303. defineValue = "#define " + validActions[ i ].ActionData;
  304. }
  305. if( isRefreshing )
  306. {
  307. string optionsId = validActions[ i ].PassName + defineValue;
  308. owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionsId, true );
  309. }
  310. passMasterNode.OptionsDefineContainer.AddDefine( defineValue, false );
  311. }
  312. else
  313. {
  314. Debug.LogFormat( "Could not find pass {0} for action {1} on {2}", validActions[ i ].PassName, validActions[ i ].ActionType, validActions[ i ].ActionData );
  315. }
  316. }
  317. else
  318. {
  319. uiItem.CheckOnExecute = true;
  320. }
  321. }
  322. break;
  323. case AseOptionsActionType.RemoveDefine:
  324. {
  325. //Debug.Log( "UNDEFINE " + validActions[ i ].ActionData );
  326. if( validActions[ i ].AllPasses )
  327. {
  328. string actionData = validActions[ i ].ActionData;
  329. string defineValue = string.Empty;
  330. if( actionData.StartsWith( "pragma" ) )
  331. {
  332. defineValue = "#" + actionData;
  333. }
  334. else
  335. {
  336. defineValue = "#define " + validActions[ i ].ActionData;
  337. }
  338. bool flag = false;
  339. if( isRefreshing )
  340. {
  341. flag = owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( defineValue, false );
  342. }
  343. if( !flag )
  344. {
  345. List<TemplateMultiPassMasterNode> nodes = owner.ContainerGraph.GetMultiPassMasterNodes( owner.LODIndex );
  346. int count = nodes.Count;
  347. for( int nodeIdx = 0; nodeIdx < count; nodeIdx++ )
  348. {
  349. nodes[ nodeIdx ].OptionsDefineContainer.RemoveDefine( defineValue );
  350. }
  351. }
  352. }
  353. else if( !string.IsNullOrEmpty( validActions[ i ].PassName ) )
  354. {
  355. TemplateMultiPassMasterNode passMasterNode = owner.ContainerGraph.GetMasterNodeOfPass( validActions[ i ].PassName, owner.LODIndex );
  356. if( passMasterNode != null )
  357. {
  358. string actionData = validActions[ i ].ActionData;
  359. string defineValue = string.Empty;
  360. if( actionData.StartsWith( "pragma" ) )
  361. {
  362. defineValue = "#" + actionData;
  363. }
  364. else
  365. {
  366. defineValue = "#define " + validActions[ i ].ActionData;
  367. }
  368. bool flag = false;
  369. if( isRefreshing )
  370. {
  371. string optionId = validActions[ i ].PassName + defineValue;
  372. flag = owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionId, false );
  373. }
  374. if( !flag )
  375. {
  376. passMasterNode.OptionsDefineContainer.RemoveDefine( defineValue );
  377. }
  378. }
  379. else
  380. {
  381. Debug.LogFormat( "Could not find pass {0} for action {1} on {2}", validActions[ i ].PassName, validActions[ i ].ActionType, validActions[ i ].ActionData );
  382. }
  383. }
  384. else
  385. {
  386. uiItem.CheckOnExecute = false;
  387. }
  388. }
  389. break;
  390. case AseOptionsActionType.SetUndefine:
  391. {
  392. if( !uiItem.IsVisible )
  393. {
  394. uiItem.CheckOnExecute = true;
  395. break;
  396. }
  397. if( validActions[ i ].AllPasses )
  398. {
  399. string defineValue = "#undef " + validActions[ i ].ActionData;
  400. if( isRefreshing )
  401. {
  402. owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( defineValue, true );
  403. }
  404. List<TemplateMultiPassMasterNode> nodes = owner.ContainerGraph.GetMultiPassMasterNodes(owner.LODIndex);
  405. int count = nodes.Count;
  406. for( int nodeIdx = 0; nodeIdx < count; nodeIdx++ )
  407. {
  408. nodes[ nodeIdx ].OptionsDefineContainer.AddDefine( defineValue, false );
  409. }
  410. }
  411. else if( !string.IsNullOrEmpty( validActions[ i ].PassName ) )
  412. {
  413. TemplateMultiPassMasterNode passMasterNode = owner.ContainerGraph.GetMasterNodeOfPass( validActions[ i ].PassName, owner.LODIndex );
  414. if( passMasterNode != null )
  415. {
  416. string defineValue = "#undef " + validActions[ i ].ActionData;
  417. if( isRefreshing )
  418. {
  419. string optionsId = validActions[ i ].PassName + defineValue;
  420. owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionsId, true );
  421. }
  422. passMasterNode.OptionsDefineContainer.AddDefine( defineValue, false );
  423. }
  424. else
  425. {
  426. Debug.LogFormat( "Could not find pass {0} for action {1} on {2}", validActions[ i ].PassName, validActions[ i ].ActionType, validActions[ i ].ActionData );
  427. }
  428. }
  429. else
  430. {
  431. uiItem.CheckOnExecute = true;
  432. }
  433. }
  434. break;
  435. case AseOptionsActionType.RemoveUndefine:
  436. {
  437. if( validActions[ i ].AllPasses )
  438. {
  439. string defineValue = "#undef " + validActions[ i ].ActionData;
  440. bool flag = false;
  441. if( isRefreshing )
  442. {
  443. flag = owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( defineValue, false );
  444. }
  445. if( !flag )
  446. {
  447. List<TemplateMultiPassMasterNode> nodes = owner.ContainerGraph.GetMultiPassMasterNodes( owner.LODIndex );
  448. int count = nodes.Count;
  449. for( int nodeIdx = 0; nodeIdx < count; nodeIdx++ )
  450. {
  451. nodes[ nodeIdx ].OptionsDefineContainer.RemoveDefine( defineValue );
  452. }
  453. }
  454. }
  455. else if( !string.IsNullOrEmpty( validActions[ i ].PassName ) )
  456. {
  457. TemplateMultiPassMasterNode passMasterNode = owner.ContainerGraph.GetMasterNodeOfPass( validActions[ i ].PassName, owner.LODIndex );
  458. if( passMasterNode != null )
  459. {
  460. bool flag = false;
  461. string defineValue = "#undef " + validActions[ i ].ActionData;
  462. if( isRefreshing )
  463. {
  464. string optionId = validActions[ i ].PassName + defineValue;
  465. flag = owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionId, false );
  466. }
  467. if( !flag )
  468. {
  469. passMasterNode.OptionsDefineContainer.RemoveDefine( defineValue );
  470. }
  471. }
  472. else
  473. {
  474. Debug.LogFormat( "Could not find pass {0} for action {1} on {2}", validActions[ i ].PassName, validActions[ i ].ActionType, validActions[ i ].ActionData );
  475. }
  476. }
  477. else
  478. {
  479. uiItem.CheckOnExecute = false;
  480. }
  481. }
  482. break;
  483. case AseOptionsActionType.ExcludePass:
  484. {
  485. string optionId = validActions[ i ].ActionData + "Pass";
  486. bool flag = isRefreshing ? owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionId, false ) : false;
  487. if( !flag )
  488. owner.SetPassVisible( validActions[ i ].ActionData, false );
  489. }
  490. break;
  491. case AseOptionsActionType.IncludePass:
  492. {
  493. if( !uiItem.IsVisible )
  494. break;
  495. string optionId = validActions[ i ].ActionData + "Pass";
  496. owner.ContainerGraph.ParentWindow.TemplatesManagerInstance.SetOptionsValue( optionId, true );
  497. owner.SetPassVisible( validActions[ i ].ActionData, true );
  498. }
  499. break;
  500. case AseOptionsActionType.SetPropertyOnPass:
  501. {
  502. //Debug.Log( "PASSPROP " + validActions[ i ].ActionData );
  503. //Refresh happens on hotcode reload and shader load and in those situation
  504. // The property own serialization handles its setup
  505. if( isRefreshing )
  506. continue;
  507. if( !string.IsNullOrEmpty( validActions[ i ].PassName ) )
  508. {
  509. TemplateMultiPassMasterNode passMasterNode = owner.ContainerGraph.GetMasterNodeOfPass( validActions[ i ].PassName, owner.LODIndex );
  510. if( passMasterNode != null )
  511. {
  512. passMasterNode.SetPropertyActionFromItem( passMasterNode.PassModule, validActions[ i ] );
  513. }
  514. else
  515. {
  516. Debug.LogFormat( "Could not find pass {0} for action {1} on {2}", validActions[ i ].PassName, validActions[ i ].ActionType, validActions[ i ].ActionData );
  517. }
  518. }
  519. else
  520. {
  521. owner.SetPropertyActionFromItem( owner.PassModule, validActions[ i ] );
  522. }
  523. }
  524. break;
  525. case AseOptionsActionType.SetPropertyOnSubShader:
  526. {
  527. //Refresh happens on hotcode reload and shader load and in those situation
  528. // The property own serialization handles its setup
  529. if( isRefreshing )
  530. continue;
  531. owner.SetPropertyActionFromItem( owner.SubShaderModule, validActions[ i ] );
  532. }
  533. break;
  534. case AseOptionsActionType.SetShaderProperty:
  535. {
  536. //This action is only check when shader is compiled over
  537. //the TemplateMultiPassMasterNode via the on CheckPropertyChangesOnOptions() method
  538. }
  539. break;
  540. case AseOptionsActionType.SetMaterialProperty:
  541. {
  542. if( isRefreshing )
  543. continue;
  544. if( !uiItem.IsVisible )
  545. break;
  546. if( owner.ContainerGraph.CurrentMaterial != null )
  547. {
  548. string prop = validActions[ i ].ActionData;
  549. if( owner.ContainerGraph.CurrentMaterial.HasProperty( prop ) )
  550. {
  551. if( uiItem.Options.UIWidget == AseOptionsUIWidget.Float || uiItem.Options.UIWidget == AseOptionsUIWidget.FloatRange )
  552. owner.ContainerGraph.CurrentMaterial.SetFloat( prop, uiItem.CurrentFieldValue );
  553. else
  554. owner.ContainerGraph.CurrentMaterial.SetInt( prop, (int)uiItem.CurrentFieldValue );
  555. if( ASEMaterialInspector.Instance != null )
  556. ASEMaterialInspector.Instance.Repaint();
  557. }
  558. }
  559. }
  560. break;
  561. }
  562. }
  563. }
  564. public void SetupCustomOptionsFromTemplate( TemplateMultiPassMasterNode owner, bool newTemplate )
  565. {
  566. TemplateOptionsContainer customOptionsContainer = m_isSubShader ? owner.SubShader.CustomOptionsContainer : owner.Pass.CustomOptionsContainer;
  567. if( !newTemplate && customOptionsContainer.Body.Length == m_passCustomOptionsSizeCheck )
  568. {
  569. for( int i = 0; i < m_passCustomOptionsUI.Count; i++ )
  570. {
  571. if( m_passCustomOptionsUI[ i ].EmptyEvent )
  572. {
  573. if( m_isSubShader )
  574. {
  575. m_passCustomOptionsUI[ i ].OnActionPerformedEvt += owner.OnCustomSubShaderOptionSelected;
  576. }
  577. else
  578. {
  579. m_passCustomOptionsUI[ i ].OnActionPerformedEvt += owner.OnCustomPassOptionSelected;
  580. }
  581. }
  582. }
  583. return;
  584. }
  585. m_passCustomOptionsLabel = string.IsNullOrEmpty( customOptionsContainer.Name ) ? CustomOptionsLabel : " " + customOptionsContainer.Name;
  586. for( int i = 0; i < m_passCustomOptionsUI.Count; i++ )
  587. {
  588. m_passCustomOptionsUI[ i ].Destroy();
  589. }
  590. m_passCustomOptionsUI.Clear();
  591. m_passCustomOptionsUIDict.Clear();
  592. m_passCustomOptionsPorts.Clear();
  593. if( customOptionsContainer.Enabled )
  594. {
  595. m_passCustomOptionsSizeCheck = customOptionsContainer.Body.Length;
  596. for( int i = 0; i < customOptionsContainer.Options.Length; i++ )
  597. {
  598. switch( customOptionsContainer.Options[ i ].Type )
  599. {
  600. case AseOptionsType.Option:
  601. {
  602. TemplateOptionUIItem item = new TemplateOptionUIItem( customOptionsContainer.Options[ i ] );
  603. if( m_isSubShader )
  604. {
  605. item.OnActionPerformedEvt += owner.OnCustomSubShaderOptionSelected;
  606. }
  607. else
  608. {
  609. item.OnActionPerformedEvt += owner.OnCustomPassOptionSelected;
  610. }
  611. m_passCustomOptionsUI.Add( item );
  612. m_passCustomOptionsUIDict.Add( customOptionsContainer.Options[ i ].Id, item );
  613. }
  614. break;
  615. case AseOptionsType.Port:
  616. {
  617. TemplateOptionPortItem item = new TemplateOptionPortItem( owner, customOptionsContainer.Options[ i ] );
  618. m_passCustomOptionsPorts.Add( item );
  619. //if( m_isSubShader )
  620. //{
  621. // if( string.IsNullOrEmpty( customOptionsContainer.Options[ i ].Id ) )
  622. // {
  623. // //No pass name selected. inject on all passes
  624. // TemplateOptionPortItem item = new TemplateOptionPortItem( owner, customOptionsContainer.Options[ i ] );
  625. // m_passCustomOptionsPorts.Add( item );
  626. // }
  627. // else if( customOptionsContainer.Options[ i ].Id.Equals( owner.PassName ) )
  628. // {
  629. // TemplateOptionPortItem item = new TemplateOptionPortItem( owner, customOptionsContainer.Options[ i ] );
  630. // m_passCustomOptionsPorts.Add( item );
  631. // }
  632. //}
  633. //else
  634. //{
  635. // TemplateOptionPortItem item = new TemplateOptionPortItem( owner, customOptionsContainer.Options[ i ] );
  636. // m_passCustomOptionsPorts.Add( item );
  637. //}
  638. }
  639. break;
  640. case AseOptionsType.Field:
  641. {
  642. TemplateOptionUIItem item = new TemplateOptionUIItem( customOptionsContainer.Options[ i ] );
  643. if( m_isSubShader )
  644. {
  645. item.OnActionPerformedEvt += owner.OnCustomSubShaderOptionSelected;
  646. }
  647. else
  648. {
  649. item.OnActionPerformedEvt += owner.OnCustomPassOptionSelected;
  650. }
  651. m_passCustomOptionsUI.Add( item );
  652. m_passCustomOptionsUIDict.Add( customOptionsContainer.Options[ i ].Id, item );
  653. }
  654. break;
  655. }
  656. }
  657. }
  658. else
  659. {
  660. m_passCustomOptionsSizeCheck = 0;
  661. }
  662. }
  663. public void SetCustomOptionsInfo( TemplateMultiPassMasterNode masterNode, ref MasterNodeDataCollector dataCollector )
  664. {
  665. if( masterNode == null )
  666. return;
  667. for( int i = 0; i < m_passCustomOptionsUI.Count; i++ )
  668. {
  669. m_passCustomOptionsUI[ i ].FillDataCollector( ref dataCollector );
  670. }
  671. for( int i = 0; i < m_passCustomOptionsPorts.Count; i++ )
  672. {
  673. m_passCustomOptionsPorts[ i ].FillDataCollector( masterNode, ref dataCollector );
  674. }
  675. }
  676. public void CheckImediateActionsForPort( TemplateMultiPassMasterNode masterNode , int portId )
  677. {
  678. for( int i = 0; i < m_passCustomOptionsPorts.Count; i++ )
  679. {
  680. m_passCustomOptionsPorts[ i ].CheckImediateActionsForPort( masterNode, portId );
  681. }
  682. }
  683. public void SetSubShaderCustomOptionsPortsInfo( TemplateMultiPassMasterNode masterNode, ref MasterNodeDataCollector dataCollector )
  684. {
  685. if( masterNode == null )
  686. return;
  687. //for( int i = 0; i < m_passCustomOptionsPorts.Count; i++ )
  688. //{
  689. // if( string.IsNullOrEmpty( m_passCustomOptionsPorts[ i ].Options.Id ) ||
  690. // masterNode.PassUniqueName.Equals( m_passCustomOptionsPorts[ i ].Options.Id ) )
  691. // {
  692. // m_passCustomOptionsPorts[ i ].FillDataCollector( masterNode, ref dataCollector );
  693. // }
  694. //}
  695. for( int i = 0; i < m_passCustomOptionsPorts.Count; i++ )
  696. {
  697. m_passCustomOptionsPorts[ i ].SubShaderFillDataCollector( masterNode, ref dataCollector );
  698. }
  699. }
  700. public void RefreshCustomOptionsDict()
  701. {
  702. if( m_passCustomOptionsUIDict.Count != m_passCustomOptionsUI.Count )
  703. {
  704. m_passCustomOptionsUIDict.Clear();
  705. int count = m_passCustomOptionsUI.Count;
  706. for( int i = 0; i < count; i++ )
  707. {
  708. m_passCustomOptionsUIDict.Add( m_passCustomOptionsUI[ i ].Options.Id, m_passCustomOptionsUI[ i ] );
  709. }
  710. }
  711. }
  712. public void ReadFromString( ref uint index, ref string[] nodeParams )
  713. {
  714. RefreshCustomOptionsDict();
  715. int savedOptions = Convert.ToInt32( nodeParams[ index++ ] );
  716. m_readOptionNames = new string[ savedOptions ];
  717. m_readOptionSelections = new string[ savedOptions ];
  718. for( int i = 0; i < savedOptions; i++ )
  719. {
  720. string optionName = nodeParams[ index++ ];
  721. string optionSelection = nodeParams[ index++ ];
  722. m_readOptionNames[ i ] = optionName;
  723. m_readOptionSelections[ i ] = optionSelection;
  724. }
  725. }
  726. public void SetReadOptions()
  727. {
  728. if( m_readOptionNames != null && m_readOptionSelections != null )
  729. {
  730. for( int i = 0; i < m_readOptionNames.Length; i++ )
  731. {
  732. if( m_passCustomOptionsUIDict.ContainsKey( m_readOptionNames[ i ] ) )
  733. {
  734. if( m_passCustomOptionsUIDict[ m_readOptionNames[ i ] ].Options.Type == AseOptionsType.Field )
  735. {
  736. m_passCustomOptionsUIDict[ m_readOptionNames[ i ] ].FieldValue.ReadFromSingle( m_readOptionSelections[ i ] );
  737. foreach( var item in m_passCustomOptionsUIDict[ m_readOptionNames[ i ] ].Options.ActionsPerOption.Rows )
  738. {
  739. if( item.Columns.Length>0 && item.Columns[ 0 ].ActionType == AseOptionsActionType.SetMaterialProperty )
  740. {
  741. if( UIUtils.CurrentWindow.CurrentGraph.CurrentMaterial != null )
  742. {
  743. if( UIUtils.CurrentWindow.CurrentGraph.CurrentMaterial.HasProperty( item.Columns[ 0 ].ActionData ) )
  744. {
  745. m_passCustomOptionsUIDict[ m_readOptionNames[ i ] ].CurrentFieldValue = UIUtils.CurrentWindow.CurrentGraph.CurrentMaterial.GetFloat( item.Columns[ 0 ].ActionData );
  746. }
  747. }
  748. }
  749. }
  750. }
  751. else
  752. m_passCustomOptionsUIDict[ m_readOptionNames[ i ] ].CurrentOptionIdx = Convert.ToInt32( m_readOptionSelections[ i ] );
  753. }
  754. }
  755. }
  756. }
  757. public void Refresh()
  758. {
  759. int count = m_passCustomOptionsUI.Count;
  760. for( int i = 0; i < count; i++ )
  761. {
  762. m_passCustomOptionsUI[ i ].Refresh();
  763. }
  764. }
  765. public void CheckDisable()
  766. {
  767. int count = m_passCustomOptionsUI.Count;
  768. for( int i = 0; i < count; i++ )
  769. {
  770. m_passCustomOptionsUI[ i ].CheckEnDisable();
  771. }
  772. }
  773. public void WriteToString( ref string nodeInfo )
  774. {
  775. int optionsCount = m_passCustomOptionsUI.Count;
  776. IOUtils.AddFieldValueToString( ref nodeInfo, optionsCount );
  777. for( int i = 0; i < optionsCount; i++ )
  778. {
  779. IOUtils.AddFieldValueToString( ref nodeInfo, m_passCustomOptionsUI[ i ].Options.Id );
  780. if( m_passCustomOptionsUI[ i ].Options.Type == AseOptionsType.Field )
  781. IOUtils.AddFieldValueToString( ref nodeInfo, m_passCustomOptionsUI[ i ].FieldValue.WriteToSingle() );
  782. else
  783. IOUtils.AddFieldValueToString( ref nodeInfo, m_passCustomOptionsUI[ i ].CurrentOption );
  784. }
  785. }
  786. public List<TemplateOptionUIItem> PassCustomOptionsUI { get { return m_passCustomOptionsUI; } }
  787. }
  788. }