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.

853 lines
24 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. [NodeAttributes( "Function Switch", "Functions", "Function Switch allows switching options at compile time for shader function", NodeAvailabilityFlags = (int)NodeAvailability.ShaderFunction )]
  11. public sealed class FunctionSwitch : ParentNode
  12. {
  13. private const string InputPortNameStr = "In ";
  14. private const string ToggleFalseStr = "False";
  15. private const string ToggleTrueStr = "True";
  16. private const string CurrSelectedStr = "Current";
  17. private const string MaxAmountStr = "Amount";
  18. private const int MaxAllowedAmount = 9;
  19. private const int MinComboSize = 50;
  20. private const int MaxComboSize = 105;
  21. [SerializeField]
  22. private string m_optionLabel = "Option";
  23. [SerializeField]
  24. private string[] AvailableInputsLabels = { "In 0", "In 1" };
  25. [SerializeField]
  26. private int[] AvailableInputsValues = { 0, 1 };
  27. [SerializeField]
  28. private int m_previousSelectedInput = 0;
  29. [SerializeField]
  30. private int m_currentSelectedInput = 0;
  31. [SerializeField]
  32. private int m_maxAmountInputs = 2;
  33. [SerializeField]
  34. private bool m_toggleMode = false;
  35. [SerializeField]
  36. private string[] m_optionNames = { "In 0", "In 1", "In 2", "In 3", "In 4", "In 5", "In 6", "In 7", "In 8" };
  37. [SerializeField]
  38. private int m_orderIndex = -1;
  39. [SerializeField]
  40. private TexReferenceType m_referenceType = TexReferenceType.Object;
  41. [SerializeField]
  42. private FunctionSwitch m_functionSwitchReference = null;
  43. [SerializeField]
  44. private int m_referenceUniqueId = -1;
  45. [SerializeField]
  46. private bool m_validReference = false;
  47. private bool m_asDrawn = false;
  48. private GUIContent m_checkContent;
  49. private GUIContent m_popContent;
  50. private const double MaxTimestamp = 1;
  51. private bool m_nameModified = false;
  52. private double m_lastTimeNameModified = 0;
  53. private Rect m_varRect;
  54. private Rect m_imgRect;
  55. private bool m_editing;
  56. private int m_cachedPropertyId = -1;
  57. [SerializeField]
  58. private int m_refMaxInputs = -1;
  59. [SerializeField]
  60. private string m_refOptionLabel = string.Empty;
  61. [SerializeField]
  62. private int m_refSelectedInput = -1;
  63. protected override void CommonInit( int uniqueId )
  64. {
  65. base.CommonInit( uniqueId );
  66. for( int i = 0; i < MaxAllowedAmount; i++ )
  67. {
  68. AddInputPort( WirePortDataType.FLOAT, false, InputPortNameStr + i );
  69. m_inputPorts[ i ].Visible = ( i < 2 );
  70. }
  71. AddOutputPort( WirePortDataType.FLOAT, " " );
  72. m_checkContent = new GUIContent();
  73. m_checkContent.image = UIUtils.CheckmarkIcon;
  74. m_popContent = new GUIContent();
  75. m_popContent.image = UIUtils.PopupIcon;
  76. m_textLabelWidth = 100;
  77. m_autoWrapProperties = true;
  78. m_insideSize.Set( 80, 25 );
  79. m_previewShaderGUID = "a58e46feaa5e3d14383bfeac24d008bc";
  80. }
  81. public void SetCurrentSelectedInput( int newValue, int prevValue )
  82. {
  83. m_previousSelectedInput = prevValue;
  84. if( m_validReference )
  85. m_currentSelectedInput = Mathf.Clamp( newValue, 0, m_refMaxInputs - 1 );
  86. else
  87. m_currentSelectedInput = Mathf.Clamp( newValue, 0, m_maxAmountInputs - 1 );
  88. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ m_currentSelectedInput ].DataType, false );
  89. ChangeSignalPropagation();
  90. }
  91. public int GetCurrentSelectedInput()
  92. {
  93. return m_currentSelectedInput;
  94. }
  95. public override void SetPreviewInputs()
  96. {
  97. base.SetPreviewInputs();
  98. if( m_cachedPropertyId == -1 )
  99. m_cachedPropertyId = Shader.PropertyToID( "_Current" );
  100. PreviewMaterial.SetInt( m_cachedPropertyId, m_currentSelectedInput );
  101. }
  102. protected override void OnUniqueIDAssigned()
  103. {
  104. base.OnUniqueIDAssigned();
  105. if( m_referenceType == TexReferenceType.Object )
  106. {
  107. UIUtils.RegisterFunctionSwitchNode( this );
  108. }
  109. else
  110. {
  111. UIUtils.RegisterFunctionSwitchNode( this );
  112. UIUtils.RegisterFunctionSwitchCopyNode( this );
  113. }
  114. }
  115. public override void Destroy()
  116. {
  117. base.Destroy();
  118. m_functionSwitchReference = null;
  119. m_referenceUniqueId = -1;
  120. if( m_referenceType == TexReferenceType.Object )
  121. {
  122. UIUtils.UnregisterFunctionSwitchNode( this );
  123. }
  124. else
  125. {
  126. UIUtils.UnregisterFunctionSwitchNode( this );
  127. UIUtils.UnregisterFunctionSwitchCopyNode( this );
  128. }
  129. }
  130. public override void OnConnectedOutputNodeChanges( int portId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  131. {
  132. base.OnConnectedOutputNodeChanges( portId, otherNodeId, otherPortId, name, type );
  133. m_inputPorts[ portId ].MatchPortToConnection();
  134. if( portId == m_currentSelectedInput )
  135. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ portId ].DataType, false );
  136. }
  137. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  138. {
  139. InputPort port = GetInputPortByUniqueId( portId );
  140. int arrayPos = m_inputPorts.IndexOf( port );
  141. if( activateNode && m_connStatus == NodeConnectionStatus.Connected && arrayPos == m_currentSelectedInput )
  142. {
  143. port.GetOutputNode().ActivateNode( m_activeNode, m_activePort, m_activeType );
  144. }
  145. OnNodeChange();
  146. SetSaveIsDirty();
  147. m_inputPorts[ portId ].MatchPortToConnection();
  148. if( arrayPos == m_currentSelectedInput )
  149. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ portId ].DataType, false );
  150. }
  151. public override void ActivateNode( int signalGenNodeId, int signalGenPortId, Type signalGenNodeType )
  152. {
  153. if( m_selfPowered )
  154. return;
  155. ConnStatus = m_restrictions.GetRestiction( signalGenNodeType, signalGenPortId ) ? NodeConnectionStatus.Error : NodeConnectionStatus.Connected;
  156. m_activeConnections += 1;
  157. m_activeType = signalGenNodeType;
  158. m_activeNode = signalGenNodeId;
  159. m_activePort = signalGenPortId;
  160. if( m_activeConnections == 1 )
  161. if( m_inputPorts[ m_currentSelectedInput ].IsConnected )
  162. m_inputPorts[ m_currentSelectedInput ].GetOutputNode().ActivateNode( signalGenNodeId, signalGenPortId, signalGenNodeType );
  163. SetSaveIsDirty();
  164. }
  165. public override void DeactivateInputPortNode( int deactivatedPort, bool forceComplete )
  166. {
  167. InputPort port = GetInputPortByUniqueId( deactivatedPort );
  168. if( deactivatedPort == m_currentSelectedInput )
  169. port.GetOutputNode().DeactivateNode( deactivatedPort, false );
  170. }
  171. public override void DeactivateNode( int deactivatedPort, bool forceComplete )
  172. {
  173. if( m_selfPowered )
  174. return;
  175. SetSaveIsDirty();
  176. m_activeConnections -= 1;
  177. if( ( forceComplete || m_activeConnections <= 0 ) )
  178. {
  179. m_activeConnections = 0;
  180. ConnStatus = NodeConnectionStatus.Not_Connected;
  181. for( int i = 0; i < m_inputPorts.Count; i++ )
  182. {
  183. if( m_inputPorts[ i ].IsConnected && i == m_currentSelectedInput )
  184. {
  185. ParentNode node = m_inputPorts[ i ].GetOutputNode();
  186. if( node != null )
  187. node.DeactivateNode( deactivatedPort == -1 ? m_inputPorts[ i ].PortId : deactivatedPort, false );
  188. }
  189. }
  190. }
  191. }
  192. public void ChangeSignalPropagation()
  193. {
  194. if( m_previousSelectedInput != m_currentSelectedInput && ConnStatus == NodeConnectionStatus.Connected )
  195. {
  196. if( m_inputPorts[ m_previousSelectedInput ].IsConnected )
  197. m_inputPorts[ m_previousSelectedInput ].GetOutputNode().DeactivateNode( m_inputPorts[ m_previousSelectedInput ].PortId, false );
  198. if( m_inputPorts[ m_currentSelectedInput ].IsConnected )
  199. m_inputPorts[ m_currentSelectedInput ].GetOutputNode().ActivateNode( UniqueId, m_inputPorts[ m_currentSelectedInput ].PortId, m_activeType );
  200. }
  201. }
  202. public bool DrawOption( ParentNode owner, bool forceDraw = false )
  203. {
  204. if( !IsConnected && !forceDraw )
  205. {
  206. //EditorGUILayout.LabelField( "Not Connected" );
  207. return false;
  208. }
  209. if( m_asDrawn ) //used to prevent the same property to be drawn more than once
  210. return false;
  211. if( m_validReference )
  212. {
  213. return m_functionSwitchReference.DrawOption( owner, true );
  214. }
  215. int prev = m_currentSelectedInput;
  216. m_asDrawn = true;
  217. if( m_toggleMode )
  218. {
  219. m_currentSelectedInput = owner.EditorGUILayoutToggle( m_optionLabel, ( m_currentSelectedInput != 0 ? true : false ) ) ? 1 : 0;
  220. if( m_currentSelectedInput != prev )
  221. {
  222. SetCurrentSelectedInput( m_currentSelectedInput, prev );
  223. return true;
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. }
  230. else
  231. {
  232. m_currentSelectedInput = owner.EditorGUILayoutIntPopup( m_optionLabel, m_currentSelectedInput, AvailableInputsLabels, AvailableInputsValues );
  233. if( m_currentSelectedInput != prev )
  234. {
  235. SetCurrentSelectedInput( m_currentSelectedInput, prev );
  236. return true;
  237. }
  238. else
  239. {
  240. return false;
  241. }
  242. }
  243. }
  244. public void CheckReference()
  245. {
  246. if( m_referenceType != TexReferenceType.Instance )
  247. {
  248. m_validReference = false;
  249. return;
  250. }
  251. if( m_functionSwitchReference == null )
  252. {
  253. m_validReference = false;
  254. ResetToSelf();
  255. return;
  256. }
  257. if( m_referenceUniqueId != m_functionSwitchReference.UniqueId )
  258. {
  259. UpdateFromSelected();
  260. }
  261. if( m_refSelectedInput != m_functionSwitchReference.GetCurrentSelectedInput() || m_refMaxInputs != m_functionSwitchReference.MaxAmountInputs || m_refOptionLabel != m_functionSwitchReference.OptionLabel )
  262. {
  263. UpdateFromSelected();
  264. }
  265. m_validReference = true;
  266. }
  267. void ResetToSelf()
  268. {
  269. m_functionSwitchReference = null;
  270. m_validReference = false;
  271. m_referenceUniqueId = -1;
  272. m_refMaxInputs = -1;
  273. m_refOptionLabel = string.Empty;
  274. m_refSelectedInput = -1;
  275. for( int i = 0; i < MaxAllowedAmount; i++ )
  276. {
  277. m_inputPorts[ i ].Visible = ( i < m_maxAmountInputs );
  278. m_inputPorts[ i ].Name = m_optionNames[ i ];
  279. }
  280. if( m_currentSelectedInput >= m_maxAmountInputs )
  281. {
  282. m_currentSelectedInput = m_maxAmountInputs - 1;
  283. }
  284. UpdateLabels();
  285. m_sizeIsDirty = true;
  286. }
  287. void UpdateFromSelected()
  288. {
  289. if( m_referenceUniqueId < 0 )
  290. return;
  291. m_functionSwitchReference = UIUtils.GetNode( m_referenceUniqueId ) as FunctionSwitch;
  292. if( m_functionSwitchReference != null )
  293. {
  294. m_validReference = true;
  295. for( int i = 0; i < MaxAllowedAmount; i++ )
  296. {
  297. m_inputPorts[ i ].Visible = ( i < m_functionSwitchReference.MaxAmountInputs );
  298. m_inputPorts[ i ].Name = m_functionSwitchReference.InputPorts[ i ].Name;
  299. }
  300. UpdateLabels();
  301. m_refMaxInputs = m_functionSwitchReference.m_maxAmountInputs;
  302. m_refOptionLabel = m_functionSwitchReference.OptionLabel;
  303. m_refSelectedInput = m_functionSwitchReference.GetCurrentSelectedInput();
  304. SetCurrentSelectedInput( m_functionSwitchReference.GetCurrentSelectedInput(), m_currentSelectedInput );
  305. }
  306. m_sizeIsDirty = true;
  307. m_isDirty = true;
  308. }
  309. public override void DrawProperties()
  310. {
  311. base.DrawProperties();
  312. EditorGUI.BeginChangeCheck();
  313. m_referenceType = (TexReferenceType)EditorGUILayoutPopup( Constants.ReferenceTypeStr, (int)m_referenceType, Constants.ReferenceArrayLabels );
  314. if( EditorGUI.EndChangeCheck() )
  315. {
  316. if( m_referenceType == TexReferenceType.Object )
  317. {
  318. UIUtils.UnregisterFunctionSwitchCopyNode( this );
  319. //UIUtils.RegisterFunctionSwitchNode( this );
  320. ResetToSelf();
  321. }
  322. else
  323. {
  324. //UIUtils.UnregisterFunctionSwitchNode( this );
  325. UIUtils.RegisterFunctionSwitchCopyNode( this );
  326. }
  327. }
  328. if( m_referenceType == TexReferenceType.Instance )
  329. {
  330. EditorGUI.BeginChangeCheck();
  331. string[] arr = new string[ UIUtils.FunctionSwitchList().Count ];
  332. int[] ids = new int[ UIUtils.FunctionSwitchList().Count ];
  333. for( int i = 0; i < arr.Length; i++ )
  334. {
  335. arr[ i ] = i + " - " + UIUtils.FunctionSwitchList()[ i ].OptionLabel;
  336. ids[ i ] = UIUtils.FunctionSwitchList()[ i ].UniqueId;
  337. }
  338. m_referenceUniqueId = EditorGUILayout.IntPopup( Constants.AvailableReferenceStr, m_referenceUniqueId, arr, ids );
  339. if( EditorGUI.EndChangeCheck() )
  340. {
  341. UpdateFromSelected();
  342. }
  343. return;
  344. }
  345. EditorGUI.BeginChangeCheck();
  346. m_optionLabel = EditorGUILayoutTextField( "Option Label", m_optionLabel );
  347. if( EditorGUI.EndChangeCheck() )
  348. {
  349. m_optionLabel = UIUtils.RemoveInvalidEnumCharacters( m_optionLabel );
  350. if( string.IsNullOrEmpty( m_optionLabel ) )
  351. {
  352. m_optionLabel = "Option";
  353. }
  354. UIUtils.UpdateFunctionSwitchData( UniqueId, m_optionLabel );
  355. }
  356. EditorGUI.BeginChangeCheck();
  357. m_toggleMode = EditorGUILayoutToggle( "Toggle Mode", m_toggleMode );
  358. if( EditorGUI.EndChangeCheck() )
  359. {
  360. if( m_toggleMode )
  361. {
  362. m_inputPorts[ 0 ].Name = ToggleFalseStr;
  363. m_inputPorts[ 1 ].Name = ToggleTrueStr;
  364. for( int i = 0; i < MaxAllowedAmount; i++ )
  365. {
  366. m_inputPorts[ i ].Visible = ( i < 2 );
  367. }
  368. if( m_currentSelectedInput >= 2 )
  369. {
  370. m_currentSelectedInput = 1;
  371. }
  372. UpdateLabels();
  373. m_sizeIsDirty = true;
  374. }
  375. else
  376. {
  377. m_inputPorts[ 0 ].Name = m_optionNames[ 0 ];
  378. m_inputPorts[ 1 ].Name = m_optionNames[ 1 ];
  379. for( int i = 0; i < MaxAllowedAmount; i++ )
  380. {
  381. m_inputPorts[ i ].Visible = ( i < m_maxAmountInputs );
  382. }
  383. if( m_currentSelectedInput >= m_maxAmountInputs )
  384. {
  385. m_currentSelectedInput = m_maxAmountInputs - 1;
  386. }
  387. UpdateLabels();
  388. m_sizeIsDirty = true;
  389. }
  390. }
  391. if( !m_toggleMode )
  392. {
  393. EditorGUI.BeginChangeCheck();
  394. m_maxAmountInputs = EditorGUILayoutIntSlider( MaxAmountStr, m_maxAmountInputs, 2, MaxAllowedAmount );
  395. if( EditorGUI.EndChangeCheck() )
  396. {
  397. for( int i = 0; i < MaxAllowedAmount; i++ )
  398. {
  399. m_inputPorts[ i ].Visible = ( i < m_maxAmountInputs );
  400. }
  401. if( m_currentSelectedInput >= m_maxAmountInputs )
  402. {
  403. m_currentSelectedInput = m_maxAmountInputs - 1;
  404. }
  405. UpdateLabels();
  406. m_sizeIsDirty = true;
  407. }
  408. EditorGUI.indentLevel++;
  409. for( int i = 0; i < m_maxAmountInputs; i++ )
  410. {
  411. EditorGUI.BeginChangeCheck();
  412. m_inputPorts[ i ].Name = EditorGUILayoutTextField( "Item " + i, m_inputPorts[ i ].Name );
  413. if( EditorGUI.EndChangeCheck() )
  414. {
  415. m_nameModified = true;
  416. m_lastTimeNameModified = EditorApplication.timeSinceStartup;
  417. m_inputPorts[ i ].Name = UIUtils.RemoveInvalidEnumCharacters( m_inputPorts[ i ].Name );
  418. m_optionNames[ i ] = m_inputPorts[ i ].Name;
  419. if( string.IsNullOrEmpty( m_inputPorts[ i ].Name ) )
  420. {
  421. m_inputPorts[ i ].Name = InputPortNameStr + i;
  422. }
  423. m_sizeIsDirty = true;
  424. }
  425. }
  426. EditorGUI.indentLevel--;
  427. if( m_nameModified )
  428. {
  429. UpdateLabels();
  430. }
  431. }
  432. if( m_toggleMode )
  433. {
  434. EditorGUI.BeginChangeCheck();
  435. int prevVal = m_currentSelectedInput;
  436. m_currentSelectedInput = EditorGUILayoutToggle( CurrSelectedStr, ( m_currentSelectedInput != 0 ? true : false ) ) ? 1 : 0;
  437. if( EditorGUI.EndChangeCheck() )
  438. SetCurrentSelectedInput( m_currentSelectedInput, prevVal );
  439. }
  440. else
  441. {
  442. EditorGUI.BeginChangeCheck();
  443. int prevVal = m_currentSelectedInput;
  444. m_currentSelectedInput = EditorGUILayoutIntPopup( CurrSelectedStr, m_currentSelectedInput, AvailableInputsLabels, AvailableInputsValues );
  445. if( EditorGUI.EndChangeCheck() )
  446. {
  447. SetCurrentSelectedInput( m_currentSelectedInput, prevVal );
  448. }
  449. }
  450. }
  451. public override void RefreshExternalReferences()
  452. {
  453. base.RefreshExternalReferences();
  454. if( UIUtils.CurrentShaderVersion() > 14205 )
  455. {
  456. if( m_referenceType == TexReferenceType.Instance )
  457. {
  458. m_functionSwitchReference = UIUtils.GetNode( m_referenceUniqueId ) as FunctionSwitch;
  459. UpdateFromSelected();
  460. }
  461. }
  462. SetCurrentSelectedInput( m_currentSelectedInput, m_previousSelectedInput );
  463. }
  464. public void UpdateLabels()
  465. {
  466. int maxinputs = m_maxAmountInputs;
  467. if( m_validReference )
  468. maxinputs = m_functionSwitchReference.MaxAmountInputs;
  469. AvailableInputsLabels = new string[ maxinputs ];
  470. AvailableInputsValues = new int[ maxinputs ];
  471. for( int i = 0; i < maxinputs; i++ )
  472. {
  473. AvailableInputsLabels[ i ] = m_optionNames[ i ];
  474. AvailableInputsValues[ i ] = i;
  475. }
  476. }
  477. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  478. {
  479. base.OnNodeLogicUpdate( drawInfo );
  480. CheckReference();
  481. }
  482. public override void OnNodeLayout( DrawInfo drawInfo )
  483. {
  484. float finalSize = 0;
  485. if( !m_toggleMode )
  486. {
  487. GUIContent dropdown = new GUIContent( m_inputPorts[ m_currentSelectedInput ].Name );
  488. int cacheSize = UIUtils.GraphDropDown.fontSize;
  489. UIUtils.GraphDropDown.fontSize = 10;
  490. Vector2 calcSize = UIUtils.GraphDropDown.CalcSize( dropdown );
  491. UIUtils.GraphDropDown.fontSize = cacheSize;
  492. finalSize = Mathf.Clamp( calcSize.x, MinComboSize, MaxComboSize );
  493. if( m_insideSize.x != finalSize )
  494. {
  495. m_insideSize.Set( finalSize, 25 );
  496. m_sizeIsDirty = true;
  497. }
  498. }
  499. base.OnNodeLayout( drawInfo );
  500. bool toggleMode = m_toggleMode;
  501. if( m_validReference )
  502. {
  503. toggleMode = m_functionSwitchReference.m_toggleMode;
  504. }
  505. if( toggleMode )
  506. {
  507. m_varRect = m_remainingBox;
  508. m_varRect.size = Vector2.one * 22 * drawInfo.InvertedZoom;
  509. m_varRect.center = m_remainingBox.center;
  510. if( m_showPreview )
  511. m_varRect.y = m_remainingBox.y;
  512. }
  513. else
  514. {
  515. m_varRect = m_remainingBox;
  516. m_varRect.width = finalSize * drawInfo.InvertedZoom;
  517. m_varRect.height = 16 * drawInfo.InvertedZoom;
  518. m_varRect.x = m_remainingBox.xMax - m_varRect.width;
  519. m_varRect.y += 1 * drawInfo.InvertedZoom;
  520. m_imgRect = m_varRect;
  521. m_imgRect.x = m_varRect.xMax - 16 * drawInfo.InvertedZoom;
  522. m_imgRect.width = 16 * drawInfo.InvertedZoom;
  523. m_imgRect.height = m_imgRect.width;
  524. }
  525. }
  526. public override void DrawGUIControls( DrawInfo drawInfo )
  527. {
  528. if( m_validReference )
  529. {
  530. base.DrawGUIControls( drawInfo );
  531. }
  532. else
  533. {
  534. base.DrawGUIControls( drawInfo );
  535. if( drawInfo.CurrentEventType != EventType.MouseDown )
  536. return;
  537. if( m_varRect.Contains( drawInfo.MousePosition ) )
  538. {
  539. m_editing = true;
  540. }
  541. else if( m_editing )
  542. {
  543. m_editing = false;
  544. }
  545. }
  546. }
  547. public override void Draw( DrawInfo drawInfo )
  548. {
  549. base.Draw( drawInfo );
  550. if( m_nameModified )
  551. {
  552. if( ( EditorApplication.timeSinceStartup - m_lastTimeNameModified ) > MaxTimestamp )
  553. {
  554. m_nameModified = false;
  555. }
  556. }
  557. if( m_validReference )
  558. {
  559. SetAdditonalTitleTextOnCallback( m_functionSwitchReference.OptionLabel, ( instance, newSubTitle ) => instance.AdditonalTitleContent.text = string.Format( Constants.SubTitleVarNameFormatStr, newSubTitle ) );
  560. }
  561. else
  562. {
  563. SetAdditonalTitleTextOnCallback( m_optionLabel, ( instance, newSubTitle ) => instance.AdditonalTitleContent.text = string.Format( Constants.SubTitleValueFormatStr, newSubTitle ) );
  564. if( m_editing )
  565. {
  566. if( m_toggleMode )
  567. {
  568. if( GUI.Button( m_varRect, GUIContent.none, UIUtils.GraphButton ) )
  569. {
  570. int prevVal = m_currentSelectedInput;
  571. m_currentSelectedInput = m_currentSelectedInput == 1 ? 0 : 1;
  572. if( m_currentSelectedInput != prevVal )
  573. SetCurrentSelectedInput( m_currentSelectedInput, prevVal );
  574. m_editing = false;
  575. }
  576. if( m_currentSelectedInput == 1 )
  577. {
  578. GUI.Label( m_varRect, m_checkContent, UIUtils.GraphButtonIcon );
  579. }
  580. }
  581. else
  582. {
  583. EditorGUI.BeginChangeCheck();
  584. int prevVal = m_currentSelectedInput;
  585. m_currentSelectedInput = EditorGUIIntPopup( m_varRect, m_currentSelectedInput, AvailableInputsLabels, AvailableInputsValues, UIUtils.GraphDropDown );
  586. if( EditorGUI.EndChangeCheck() )
  587. {
  588. SetCurrentSelectedInput( m_currentSelectedInput, prevVal );
  589. m_editing = false;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. public override void OnNodeRepaint( DrawInfo drawInfo )
  596. {
  597. base.OnNodeRepaint( drawInfo );
  598. if( !m_isVisible )
  599. return;
  600. if( ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD2 )
  601. {
  602. if( m_validReference )
  603. {
  604. bool cacheState = GUI.enabled;
  605. GUI.enabled = false;
  606. if( m_functionSwitchReference.m_toggleMode )
  607. {
  608. GUI.Label( m_varRect, GUIContent.none, UIUtils.GraphButton );
  609. if( m_functionSwitchReference.GetCurrentSelectedInput() == 1 )
  610. {
  611. GUI.Label( m_varRect, m_checkContent, UIUtils.GraphButtonIcon );
  612. }
  613. }
  614. else
  615. {
  616. GUI.Label( m_varRect, m_functionSwitchReference.AvailableInputsLabels[ m_currentSelectedInput ], UIUtils.GraphDropDown );
  617. }
  618. GUI.enabled = cacheState;
  619. }
  620. else
  621. {
  622. if( !m_editing )
  623. {
  624. if( m_toggleMode )
  625. {
  626. GUI.Label( m_varRect, GUIContent.none, UIUtils.GraphButton );
  627. if( m_currentSelectedInput == 1 )
  628. {
  629. GUI.Label( m_varRect, m_checkContent, UIUtils.GraphButtonIcon );
  630. }
  631. }
  632. else
  633. {
  634. GUI.Label( m_varRect, AvailableInputsLabels[ m_currentSelectedInput ], UIUtils.GraphDropDown );
  635. GUI.Label( m_imgRect, m_popContent, UIUtils.GraphButtonIcon );
  636. }
  637. }
  638. }
  639. }
  640. }
  641. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  642. {
  643. return m_inputPorts[ m_currentSelectedInput ].GeneratePortInstructions( ref dataCollector );
  644. }
  645. public override void ReadFromString( ref string[] nodeParams )
  646. {
  647. base.ReadFromString( ref nodeParams );
  648. m_optionLabel = GetCurrentParam( ref nodeParams );
  649. m_toggleMode = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  650. m_currentSelectedInput = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  651. m_previousSelectedInput = m_currentSelectedInput;
  652. m_maxAmountInputs = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  653. m_orderIndex = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  654. for( int i = 0; i < MaxAllowedAmount; i++ )
  655. {
  656. m_inputPorts[ i ].Visible = ( i < m_maxAmountInputs );
  657. }
  658. if( m_currentSelectedInput >= m_maxAmountInputs )
  659. {
  660. m_currentSelectedInput = m_maxAmountInputs - 1;
  661. }
  662. for( int i = 0; i < m_maxAmountInputs; i++ )
  663. {
  664. m_optionNames[ i ] = GetCurrentParam( ref nodeParams );
  665. m_inputPorts[ i ].Name = m_optionNames[ i ];
  666. }
  667. if( m_toggleMode )
  668. {
  669. m_inputPorts[ 0 ].Name = ToggleFalseStr;
  670. m_inputPorts[ 1 ].Name = ToggleTrueStr;
  671. }
  672. UpdateLabels();
  673. m_sizeIsDirty = true;
  674. UIUtils.UpdateFunctionSwitchData( UniqueId, m_optionLabel );
  675. UIUtils.UpdateFunctionSwitchCopyData( UniqueId, m_optionLabel );
  676. if( UIUtils.CurrentShaderVersion() > 14205 )
  677. {
  678. m_referenceType = (TexReferenceType)Enum.Parse( typeof( TexReferenceType ), GetCurrentParam( ref nodeParams ) );
  679. m_referenceUniqueId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  680. if( m_referenceType == TexReferenceType.Instance )
  681. {
  682. //UIUtils.UnregisterFunctionSwitchNode( this );
  683. UIUtils.RegisterFunctionSwitchNode( this );
  684. UIUtils.RegisterFunctionSwitchCopyNode( this );
  685. }
  686. else
  687. {
  688. //UIUtils.UnregisterFunctionSwitchCopyNode( this );
  689. UIUtils.RegisterFunctionSwitchNode( this );
  690. }
  691. }
  692. }
  693. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  694. {
  695. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  696. IOUtils.AddFieldValueToString( ref nodeInfo, m_optionLabel );
  697. IOUtils.AddFieldValueToString( ref nodeInfo, m_toggleMode );
  698. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentSelectedInput );
  699. IOUtils.AddFieldValueToString( ref nodeInfo, m_maxAmountInputs );
  700. IOUtils.AddFieldValueToString( ref nodeInfo, m_orderIndex );
  701. for( int i = 0; i < m_maxAmountInputs; i++ )
  702. {
  703. IOUtils.AddFieldValueToString( ref nodeInfo, m_optionNames[ i ] );
  704. }
  705. IOUtils.AddFieldValueToString( ref nodeInfo, m_referenceType );
  706. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_functionSwitchReference != null ? m_functionSwitchReference.UniqueId : -1 ) );
  707. }
  708. public int OrderIndex
  709. {
  710. get { return m_orderIndex; }
  711. set { m_orderIndex = value; }
  712. }
  713. public string OptionLabel
  714. {
  715. get { return m_optionLabel; }
  716. set { m_optionLabel = value; }
  717. }
  718. public bool AsDrawn { get { return m_asDrawn; } set { m_asDrawn = value; } }
  719. public override string DataToArray { get { return m_optionLabel; } }
  720. public int MaxAmountInputs
  721. {
  722. get { return m_maxAmountInputs; }
  723. }
  724. }
  725. }