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.

229 lines
6.7 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( "Debug Switch", "Logical Operators", "Hard Switch between any of its input ports" )]
  10. public class SwitchNode : ParentNode
  11. {
  12. private const string Info = "This is a Debug node which only generates the source for the selected port. This means that no properties are generated for other ports and information might be lost.";
  13. private const string InputPortName = "In ";
  14. private const string CurrSelectedStr = "Current";
  15. private const string MaxAmountStr = "Max Amount";
  16. private const int MaxAllowedAmount = 8;
  17. [SerializeField]
  18. private string[] m_availableInputsLabels = { "In 0", "In 1" };
  19. [SerializeField]
  20. private int[] m_availableInputsValues = { 0, 1 };
  21. [SerializeField]
  22. private int m_currentSelectedInput = 0;
  23. [SerializeField]
  24. private int m_maxAmountInputs = 2;
  25. private int m_cachedPropertyId = -1;
  26. private GUIContent m_popContent;
  27. private Rect m_varRect;
  28. private Rect m_imgRect;
  29. private bool m_editing;
  30. protected override void CommonInit( int uniqueId )
  31. {
  32. base.CommonInit( uniqueId );
  33. for( int i = 0; i < MaxAllowedAmount; i++ )
  34. {
  35. AddInputPort( WirePortDataType.FLOAT, false, InputPortName + i );
  36. m_inputPorts[ i ].Visible = ( i < 2 );
  37. }
  38. AddOutputPort( WirePortDataType.FLOAT, " " );
  39. m_popContent = new GUIContent();
  40. m_popContent.image = UIUtils.PopupIcon;
  41. m_insideSize.Set( 50, 25 );
  42. m_textLabelWidth = 100;
  43. m_autoWrapProperties = false;
  44. m_previewShaderGUID = "a58e46feaa5e3d14383bfeac24d008bc";
  45. }
  46. public override void SetPreviewInputs()
  47. {
  48. base.SetPreviewInputs();
  49. if( m_cachedPropertyId == -1 )
  50. m_cachedPropertyId = Shader.PropertyToID( "_Current" );
  51. PreviewMaterial.SetInt( m_cachedPropertyId, m_currentSelectedInput );
  52. }
  53. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  54. {
  55. m_inputPorts[ inputPortId ].MatchPortToConnection();
  56. if( inputPortId == m_currentSelectedInput )
  57. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ inputPortId ].DataType, false );
  58. }
  59. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  60. {
  61. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  62. m_inputPorts[ portId ].MatchPortToConnection();
  63. if( portId == m_currentSelectedInput )
  64. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ portId ].DataType, false );
  65. }
  66. public void UpdateLabels()
  67. {
  68. m_availableInputsLabels = new string[ m_maxAmountInputs ];
  69. m_availableInputsValues = new int[ m_maxAmountInputs ];
  70. for( int i = 0; i < m_maxAmountInputs; i++ )
  71. {
  72. m_availableInputsLabels[ i ] = InputPortName + i;
  73. m_availableInputsValues[ i ] = i;
  74. }
  75. }
  76. //void UpdateOutput()
  77. //{
  78. // m_outputPorts[ 0 ].ChangeProperties( m_inputPorts[ m_currentSelectedInput ].Name, m_inputPorts[ m_currentSelectedInput ].DataType, false );
  79. // m_sizeIsDirty = true;
  80. //}
  81. public override void OnNodeLayout( DrawInfo drawInfo )
  82. {
  83. base.OnNodeLayout( drawInfo );
  84. m_varRect = m_remainingBox;
  85. m_varRect.width = 50 * drawInfo.InvertedZoom;
  86. m_varRect.height = 16 * drawInfo.InvertedZoom;
  87. m_varRect.x = m_remainingBox.xMax - m_varRect.width;
  88. //m_varRect.x += m_remainingBox.width * 0.5f - m_varRect.width * 0.5f;
  89. m_varRect.y += 1 * drawInfo.InvertedZoom;
  90. m_imgRect = m_varRect;
  91. m_imgRect.x = m_varRect.xMax - 16 * drawInfo.InvertedZoom;
  92. m_imgRect.width = 16 * drawInfo.InvertedZoom;
  93. m_imgRect.height = m_imgRect.width;
  94. }
  95. public override void DrawGUIControls( DrawInfo drawInfo )
  96. {
  97. base.DrawGUIControls( drawInfo );
  98. if( drawInfo.CurrentEventType != EventType.MouseDown )
  99. return;
  100. if( m_varRect.Contains( drawInfo.MousePosition ) )
  101. {
  102. m_editing = true;
  103. }
  104. else if( m_editing )
  105. {
  106. m_editing = false;
  107. }
  108. }
  109. public override void Draw( DrawInfo drawInfo )
  110. {
  111. base.Draw( drawInfo );
  112. if( m_editing )
  113. {
  114. EditorGUI.BeginChangeCheck();
  115. m_currentSelectedInput = EditorGUIIntPopup( m_varRect, m_currentSelectedInput, m_availableInputsLabels, m_availableInputsValues, UIUtils.GraphDropDown );
  116. if( EditorGUI.EndChangeCheck() )
  117. {
  118. m_editing = false;
  119. }
  120. }
  121. }
  122. public override void OnNodeRepaint( DrawInfo drawInfo )
  123. {
  124. base.OnNodeRepaint( drawInfo );
  125. if( !m_isVisible )
  126. return;
  127. if( !m_editing && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  128. {
  129. GUI.Label( m_varRect, m_availableInputsLabels[ m_currentSelectedInput ], UIUtils.GraphDropDown );
  130. GUI.Label( m_imgRect, m_popContent, UIUtils.GraphButtonIcon );
  131. }
  132. }
  133. public override void DrawProperties()
  134. {
  135. base.DrawProperties();
  136. NodeUtils.DrawPropertyGroup( ref m_propertiesFoldout, Constants.ParameterLabelStr, DrawDebugOptions );
  137. EditorGUILayout.HelpBox( Info, MessageType.Warning );
  138. }
  139. void DrawDebugOptions()
  140. {
  141. EditorGUI.BeginChangeCheck();
  142. m_maxAmountInputs = EditorGUILayoutIntSlider( MaxAmountStr, m_maxAmountInputs, 2, MaxAllowedAmount );
  143. if( EditorGUI.EndChangeCheck() )
  144. {
  145. for( int i = 0; i < MaxAllowedAmount; i++ )
  146. {
  147. m_inputPorts[ i ].Visible = ( i < m_maxAmountInputs );
  148. }
  149. if( m_currentSelectedInput >= m_maxAmountInputs )
  150. {
  151. m_currentSelectedInput = m_maxAmountInputs - 1;
  152. }
  153. UpdateLabels();
  154. m_sizeIsDirty = true;
  155. }
  156. m_currentSelectedInput = EditorGUILayoutIntPopup( CurrSelectedStr, m_currentSelectedInput, m_availableInputsLabels, m_availableInputsValues );
  157. }
  158. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  159. {
  160. return m_inputPorts[ m_currentSelectedInput ].GeneratePortInstructions( ref dataCollector );
  161. }
  162. public override void ReadFromString( ref string[] nodeParams )
  163. {
  164. base.ReadFromString( ref nodeParams );
  165. m_currentSelectedInput = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  166. m_maxAmountInputs = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  167. for( int i = 0; i < MaxAllowedAmount; i++ )
  168. {
  169. m_inputPorts[ i ].Visible = ( i < m_maxAmountInputs );
  170. }
  171. if( m_currentSelectedInput >= m_maxAmountInputs )
  172. {
  173. m_currentSelectedInput = m_maxAmountInputs - 1;
  174. }
  175. UpdateLabels();
  176. m_sizeIsDirty = true;
  177. }
  178. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  179. {
  180. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  181. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentSelectedInput );
  182. IOUtils.AddFieldValueToString( ref nodeInfo, m_maxAmountInputs );
  183. }
  184. }
  185. }