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.

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