GetLocalVarNode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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( "Get Local Var", "Miscellaneous", "Use a registered local variable", null, KeyCode.G )]
  11. public class GetLocalVarNode : ParentNode
  12. {
  13. [SerializeField]
  14. private int m_referenceId = -1;
  15. [SerializeField]
  16. private float m_referenceWidth = -1;
  17. [SerializeField]
  18. private int m_nodeId = -1;
  19. [SerializeField]
  20. private RegisterLocalVarNode m_currentSelected = null;
  21. [SerializeField]
  22. private string m_registerLocalVarName = string.Empty;
  23. private int m_cachedPropertyId = -1;
  24. private string m_previousLabel = string.Empty;
  25. private bool m_refSelect = false;
  26. private int m_prevReferenceId = -1;
  27. protected override void CommonInit( int uniqueId )
  28. {
  29. base.CommonInit( uniqueId );
  30. AddOutputPort( WirePortDataType.OBJECT, Constants.EmptyPortValue );
  31. // This is needed for infinite loop detection
  32. AddInputPort( WirePortDataType.OBJECT, false, Constants.EmptyPortValue );
  33. m_inputPorts[ 0 ].Visible = false;
  34. m_outputPorts[ 0 ].Locked = true;
  35. m_textLabelWidth = 80;
  36. m_autoWrapProperties = true;
  37. m_hasLeftDropdown = true;
  38. m_previewShaderGUID = "f21a6e44c7d7b8543afacd19751d24c6";
  39. }
  40. protected override void OnUniqueIDAssigned()
  41. {
  42. base.OnUniqueIDAssigned();
  43. if( UniqueId > -1 )
  44. m_containerGraph.LocalVarNodes.OnReorderEventComplete += OnReorderEventComplete;
  45. }
  46. private void OnReorderEventComplete()
  47. {
  48. if( m_currentSelected != null )
  49. {
  50. m_referenceId = m_containerGraph.LocalVarNodes.GetNodeRegisterIdx( m_currentSelected.UniqueId );
  51. }
  52. }
  53. public override void SetPreviewInputs()
  54. {
  55. base.SetPreviewInputs();
  56. if( m_currentSelected != null )
  57. {
  58. if( m_drawPreviewAsSphere != m_currentSelected.SpherePreview )
  59. {
  60. m_drawPreviewAsSphere = m_currentSelected.SpherePreview;
  61. OnNodeChange();
  62. }
  63. //CheckSpherePreview();
  64. if( m_cachedPropertyId == -1 )
  65. m_cachedPropertyId = Shader.PropertyToID( "_A" );
  66. PreviewMaterial.SetTexture( m_cachedPropertyId, m_currentSelected.OutputPorts[ 0 ].OutputPreviewTexture );
  67. }
  68. }
  69. public override void DrawProperties()
  70. {
  71. base.DrawProperties();
  72. EditorGUILayout.BeginHorizontal();
  73. EditorGUI.BeginChangeCheck();
  74. m_referenceId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceId, UIUtils.LocalVarNodeArr() );
  75. if( EditorGUI.EndChangeCheck() )
  76. {
  77. UpdateFromSelected();
  78. }
  79. if( GUILayout.Button( "\u25C4", "minibutton", GUILayout.Width( 17 ) ) && m_currentSelected )
  80. {
  81. UIUtils.FocusOnNode( m_currentSelected, 0, false );
  82. }
  83. EditorGUILayout.EndHorizontal();
  84. //EditorGUILayout.LabelField( ConnStatus.ToString() + " " + m_activeConnections );
  85. }
  86. public override void Destroy()
  87. {
  88. base.Destroy();
  89. CurrentSelected = null;
  90. if( UniqueId > -1 )
  91. m_containerGraph.LocalVarNodes.OnReorderEventComplete -= OnReorderEventComplete;
  92. }
  93. public override void Draw( DrawInfo drawInfo )
  94. {
  95. base.Draw( drawInfo );
  96. if( m_dropdownEditing )
  97. {
  98. EditorGUI.BeginChangeCheck();
  99. m_referenceId = EditorGUIPopup( m_dropdownRect, m_referenceId, UIUtils.LocalVarNodeArr(), UIUtils.PropertyPopUp );
  100. if( EditorGUI.EndChangeCheck() )
  101. {
  102. UpdateFromSelected();
  103. DropdownEditing = false;
  104. }
  105. }
  106. }
  107. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  108. {
  109. base.OnNodeLogicUpdate( drawInfo );
  110. UpdateLocalVar();
  111. }
  112. public override void OnNodeRepaint( DrawInfo drawInfo )
  113. {
  114. base.OnNodeRepaint( drawInfo );
  115. if( m_isVisible && m_refSelect && !m_selected )
  116. {
  117. GUI.color = Constants.SpecialGetLocalVarSelectionColor;
  118. GUI.Label( m_globalPosition, string.Empty, UIUtils.GetCustomStyle( CustomStyle.NodeWindowOn ) );
  119. GUI.color = m_colorBuffer;
  120. }
  121. }
  122. void CheckForLoops()
  123. {
  124. if( CurrentSelected != null && UIUtils.DetectNodeLoopsFrom( CurrentSelected, new Dictionary<int, int>() ) )
  125. {
  126. CurrentSelected = UIUtils.GetLocalVarNode( m_prevReferenceId );
  127. if( CurrentSelected == null || UIUtils.DetectNodeLoopsFrom( CurrentSelected, new Dictionary<int, int>() ) )
  128. {
  129. m_referenceId = -1;
  130. m_prevReferenceId = -1;
  131. CurrentSelected = null;
  132. m_outputPorts[ 0 ].Locked = true;
  133. SetAdditonalTitleText( "" );
  134. UIUtils.ShowMessage( "Infinite Loop detected, disabled selection" );
  135. }
  136. else
  137. {
  138. m_referenceId = m_prevReferenceId;
  139. UIUtils.ShowMessage( "Infinite Loop detected, reverted to previous selection" );
  140. }
  141. }
  142. }
  143. void UpdateFromSelected()
  144. {
  145. CurrentSelected = UIUtils.GetLocalVarNode( m_referenceId );
  146. CheckForLoops();
  147. if( m_currentSelected != null )
  148. {
  149. m_nodeId = m_currentSelected.UniqueId;
  150. m_outputPorts[ 0 ].Locked = false;
  151. m_outputPorts[ 0 ].ChangeType( m_currentSelected.OutputPorts[ 0 ].DataType, false );
  152. m_drawPreviewAsSphere = m_currentSelected.SpherePreview;
  153. CheckSpherePreview();
  154. m_previousLabel = m_currentSelected.DataToArray;
  155. SetAdditonalTitleText( string.Format( Constants.SubTitleVarNameFormatStr, m_currentSelected.DataToArray ) );
  156. m_referenceWidth = m_currentSelected.Position.width;
  157. }
  158. m_sizeIsDirty = true;
  159. m_isDirty = true;
  160. m_prevReferenceId = m_referenceId;
  161. }
  162. public void UpdateLocalVar()
  163. {
  164. m_refSelect = false;
  165. if( m_referenceId > -1 )
  166. {
  167. ParentNode newNode = UIUtils.GetLocalVarNode( m_referenceId );
  168. if( newNode != null )
  169. {
  170. if( newNode.UniqueId != m_nodeId )
  171. {
  172. CurrentSelected = null;
  173. int count = UIUtils.LocalVarNodeAmount();
  174. for( int i = 0; i < count; i++ )
  175. {
  176. ParentNode node = UIUtils.GetLocalVarNode( i );
  177. if( node.UniqueId == m_nodeId )
  178. {
  179. CurrentSelected = node as RegisterLocalVarNode;
  180. m_referenceId = i;
  181. break;
  182. }
  183. }
  184. }
  185. }
  186. if( m_currentSelected != null )
  187. {
  188. if( m_currentSelected.OutputPorts[ 0 ].DataType != m_outputPorts[ 0 ].DataType )
  189. {
  190. m_outputPorts[ 0 ].Locked = false;
  191. m_outputPorts[ 0 ].ChangeType( m_currentSelected.OutputPorts[ 0 ].DataType, false );
  192. }
  193. if( !m_previousLabel.Equals( m_currentSelected.DataToArray ) )
  194. {
  195. m_previousLabel = m_currentSelected.DataToArray;
  196. SetAdditonalTitleText( string.Format( Constants.SubTitleVarNameFormatStr, m_currentSelected.DataToArray ) );
  197. }
  198. if( m_referenceWidth != m_currentSelected.Position.width )
  199. {
  200. m_referenceWidth = m_currentSelected.Position.width;
  201. m_sizeIsDirty = true;
  202. }
  203. if( m_currentSelected.Selected )
  204. m_refSelect = true;
  205. }
  206. else
  207. {
  208. ResetReference();
  209. }
  210. }
  211. }
  212. public void ResetReference()
  213. {
  214. m_outputPorts[ 0 ].Locked = true;
  215. m_currentSelected = null;
  216. m_inputPorts[ 0 ].DummyClear();
  217. m_nodeId = -1;
  218. m_referenceId = -1;
  219. m_referenceWidth = -1;
  220. SetAdditonalTitleText( string.Empty );
  221. }
  222. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  223. {
  224. if( m_currentSelected != null )
  225. {
  226. return m_currentSelected.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  227. }
  228. else
  229. {
  230. UIUtils.ShowMessage( UniqueId, "Get Local Var node without reference. Attempting to access inexistant local variable.", MessageSeverity.Error );
  231. return m_outputPorts[ 0 ].ErrorValue;
  232. }
  233. }
  234. //public override void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
  235. //{
  236. // base.PropagateNodeData( nodeData, ref dataCollector );
  237. // if( m_currentSelected != null )
  238. // {
  239. // m_currentSelected.PropagateNodeData( nodeData, ref dataCollector );
  240. // }
  241. //}
  242. public override void ReadFromString( ref string[] nodeParams )
  243. {
  244. base.ReadFromString( ref nodeParams );
  245. if( UIUtils.CurrentShaderVersion() > 15 )
  246. {
  247. m_nodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  248. m_outputPorts[ 0 ].Locked = ( m_nodeId < 0 );
  249. if( UIUtils.CurrentShaderVersion() > 15500 )
  250. {
  251. m_registerLocalVarName = GetCurrentParam( ref nodeParams );
  252. }
  253. }
  254. else
  255. {
  256. m_referenceId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  257. m_outputPorts[ 0 ].Locked = ( m_referenceId < 0 );
  258. }
  259. }
  260. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  261. {
  262. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  263. if( m_currentSelected != null )
  264. {
  265. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentSelected.UniqueId );
  266. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentSelected.DataToArray );
  267. }
  268. else
  269. {
  270. IOUtils.AddFieldValueToString( ref nodeInfo, -1 );
  271. IOUtils.AddFieldValueToString( ref nodeInfo, string.Empty );
  272. }
  273. }
  274. public override void OnNodeDoubleClicked( Vector2 currentMousePos2D )
  275. {
  276. if( m_currentSelected != null )
  277. {
  278. UIUtils.FocusOnNode( m_currentSelected, 0, true );
  279. }
  280. }
  281. public override void RefreshExternalReferences()
  282. {
  283. base.RefreshExternalReferences();
  284. if( UIUtils.CurrentShaderVersion() > 15 )
  285. {
  286. CurrentSelected = UIUtils.GetNode( m_nodeId ) as RegisterLocalVarNode;
  287. m_referenceId = UIUtils.GetLocalVarNodeRegisterId( m_nodeId );
  288. if( CurrentSelected == null && UIUtils.CurrentShaderVersion() > 15500 && !string.IsNullOrEmpty( m_registerLocalVarName ) )
  289. {
  290. CurrentSelected = m_containerGraph.LocalVarNodes.GetNodeByDataToArray( m_registerLocalVarName );
  291. if( CurrentSelected != null )
  292. {
  293. m_nodeId = CurrentSelected.UniqueId;
  294. m_referenceId = UIUtils.GetLocalVarNodeRegisterId( m_nodeId );
  295. }
  296. }
  297. }
  298. else
  299. {
  300. CurrentSelected = UIUtils.GetLocalVarNode( m_referenceId );
  301. if( m_currentSelected != null )
  302. {
  303. m_nodeId = m_currentSelected.UniqueId;
  304. }
  305. }
  306. CheckForLoops();
  307. if( m_currentSelected != null )
  308. {
  309. m_outputPorts[ 0 ].Locked = false;
  310. m_outputPorts[ 0 ].ChangeType( m_currentSelected.OutputPorts[ 0 ].DataType, false );
  311. }
  312. else
  313. {
  314. m_outputPorts[ 0 ].Locked = true;
  315. }
  316. }
  317. public override void ActivateNode( int signalGenNodeId, int signalGenPortId, System.Type signalGenNodeType )
  318. {
  319. base.ActivateNode( signalGenNodeId, signalGenPortId, signalGenNodeType );
  320. if( m_activeConnections == 1 )
  321. {
  322. if( m_currentSelected != null )
  323. {
  324. m_currentSelected.ActivateNode( signalGenNodeId, signalGenPortId, signalGenNodeType );
  325. }
  326. }
  327. }
  328. public override void DeactivateNode( int deactivatedPort, bool forceComplete )
  329. {
  330. forceComplete = forceComplete || ( m_activeConnections == 1 );
  331. base.DeactivateNode( deactivatedPort, forceComplete );
  332. if( forceComplete && m_currentSelected != null )
  333. {
  334. m_currentSelected.DeactivateNode( deactivatedPort, false );
  335. }
  336. }
  337. public override void OnNodeSelected( bool value )
  338. {
  339. base.OnNodeSelected( value );
  340. if( m_currentSelected != null )
  341. {
  342. m_currentSelected.CheckReferenceSelection();
  343. }
  344. }
  345. public RegisterLocalVarNode CurrentSelected
  346. {
  347. get { return m_currentSelected; }
  348. set
  349. {
  350. // This is needed for infinite loop detection
  351. if( m_inputPorts != null )
  352. m_inputPorts[ 0 ].DummyClear();
  353. if( m_currentSelected != null )
  354. {
  355. m_currentSelected.UnregisterGetLocalVar( this );
  356. //if( m_currentSelected != value )
  357. m_currentSelected.DeactivateNode( 0, false );
  358. }
  359. if( value != null )
  360. {
  361. value.RegisterGetLocalVar( this );
  362. if( IsConnected && value != m_currentSelected )
  363. value.ActivateNode( UniqueId, 0, m_activeType );
  364. // This is needed for infinite loop detection
  365. m_inputPorts[ 0 ].DummyAdd( value.UniqueId, 0 ); ;
  366. }
  367. m_currentSelected = value;
  368. }
  369. }
  370. }
  371. }