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.

233 lines
5.7 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. namespace AmplifyShaderEditor
  5. {
  6. [System.Serializable]
  7. public class InlineProperty
  8. {
  9. [SerializeField]
  10. private float m_value = 0;
  11. [SerializeField]
  12. private bool m_active = false;
  13. [SerializeField]
  14. private int m_nodeId = -1;
  15. public InlineProperty() { }
  16. public InlineProperty( float val )
  17. {
  18. m_value = val;
  19. }
  20. public InlineProperty( int val )
  21. {
  22. m_value = val;
  23. }
  24. public void ResetProperty()
  25. {
  26. m_nodeId = -1;
  27. m_active = false;
  28. }
  29. public void CopyFrom( InlineProperty other )
  30. {
  31. m_value = other.m_value;
  32. m_active = other.m_active;
  33. m_nodeId = other.m_nodeId;
  34. }
  35. public void SetInlineByName( string propertyName )
  36. {
  37. m_nodeId = UIUtils.GetNodeIdByName( propertyName );
  38. m_active = m_nodeId != -1;
  39. }
  40. public void IntSlider( ref UndoParentNode owner, GUIContent content, int min, int max )
  41. {
  42. if( !m_active )
  43. {
  44. EditorGUILayout.BeginHorizontal();
  45. m_value = owner.EditorGUILayoutIntSlider( content, (int)m_value, min, max );
  46. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  47. m_active = !m_active;
  48. EditorGUILayout.EndHorizontal();
  49. }
  50. else
  51. {
  52. DrawPicker( ref owner, content );
  53. }
  54. }
  55. public void EnumTypePopup( ref UndoParentNode owner, string content, string[] displayOptions )
  56. {
  57. if( !m_active )
  58. {
  59. EditorGUILayout.BeginHorizontal();
  60. m_value = owner.EditorGUILayoutPopup( content, (int)m_value, displayOptions );
  61. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  62. m_active = !m_active;
  63. EditorGUILayout.EndHorizontal();
  64. }
  65. else
  66. {
  67. DrawPicker( ref owner, content );
  68. }
  69. }
  70. public void FloatField( ref UndoParentNode owner, string content )
  71. {
  72. if( !m_active )
  73. {
  74. EditorGUILayout.BeginHorizontal();
  75. m_value = owner.EditorGUILayoutFloatField( content, m_value );
  76. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  77. m_active = !m_active;
  78. EditorGUILayout.EndHorizontal();
  79. }
  80. else
  81. {
  82. DrawPicker( ref owner, content );
  83. }
  84. }
  85. public void CustomDrawer( ref UndoParentNode owner, DrawPropertySection Drawer, string content )
  86. {
  87. if( !m_active )
  88. {
  89. EditorGUILayout.BeginHorizontal();
  90. Drawer( owner );
  91. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  92. m_active = !m_active;
  93. EditorGUILayout.EndHorizontal();
  94. }
  95. else
  96. {
  97. DrawPicker( ref owner, content );
  98. }
  99. }
  100. public delegate void DrawPropertySection( UndoParentNode owner );
  101. private void DrawPicker( ref UndoParentNode owner, GUIContent content )
  102. {
  103. DrawPicker( ref owner, content.text );
  104. }
  105. private void DrawPicker( ref UndoParentNode owner, string content )
  106. {
  107. EditorGUILayout.BeginHorizontal();
  108. m_nodeId = owner.EditorGUILayoutIntPopup( content, m_nodeId, UIUtils.FloatIntNodeArr(), UIUtils.FloatIntNodeIds() );
  109. if( GUILayout.Button( UIUtils.FloatIntIconOFF, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  110. m_active = !m_active;
  111. EditorGUILayout.EndHorizontal();
  112. }
  113. public string GetValueOrProperty( bool parentesis = true )
  114. {
  115. if( m_active )
  116. {
  117. PropertyNode node = GetPropertyNode();
  118. if( node != null )
  119. {
  120. return parentesis?"[" + node.PropertyName + "]": node.PropertyName;
  121. }
  122. else
  123. {
  124. m_active = false;
  125. m_nodeId = -1;
  126. return m_value.ToString();
  127. }
  128. }
  129. else
  130. {
  131. return m_value.ToString();
  132. }
  133. }
  134. public string GetValueOrProperty( string defaultValue, bool parentesis = true )
  135. {
  136. if( m_active )
  137. {
  138. PropertyNode node = GetPropertyNode();
  139. if( node != null )
  140. {
  141. return parentesis?"[" + node.PropertyName + "]": node.PropertyName;
  142. }
  143. else if( !string.IsNullOrEmpty( defaultValue ) )
  144. {
  145. m_active = false;
  146. m_nodeId = -1;
  147. return defaultValue;
  148. }
  149. else
  150. {
  151. m_active = false;
  152. m_nodeId = -1;
  153. return m_value.ToString();
  154. }
  155. }
  156. else
  157. {
  158. return defaultValue;
  159. }
  160. }
  161. public void ReadFromString( ref uint index, ref string[] nodeParams )
  162. {
  163. m_value = Convert.ToInt32( nodeParams[ index++ ] );
  164. m_active = Convert.ToBoolean( nodeParams[ index++ ] );
  165. m_nodeId = Convert.ToInt32( nodeParams[ index++ ] );
  166. }
  167. public void WriteToString( ref string nodeInfo )
  168. {
  169. IOUtils.AddFieldValueToString( ref nodeInfo, m_value );
  170. IOUtils.AddFieldValueToString( ref nodeInfo, m_active );
  171. IOUtils.AddFieldValueToString( ref nodeInfo, m_nodeId );
  172. }
  173. public void SetInlineNodeValue()
  174. {
  175. if( IsValid )
  176. {
  177. RangedFloatNode fnode = UIUtils.GetNode( m_nodeId ) as RangedFloatNode;
  178. if( fnode != null )
  179. {
  180. fnode.Value = m_value;
  181. fnode.SetMaterialValueFromInline( m_value );
  182. }
  183. else
  184. {
  185. IntNode inode = UIUtils.GetNode( m_nodeId ) as IntNode;
  186. inode.Value = (int)m_value;
  187. inode.SetMaterialValueFromInline( (int)m_value );
  188. }
  189. }
  190. }
  191. public bool IsValid { get { return m_active && m_nodeId != -1; } }
  192. public PropertyNode GetPropertyNode()
  193. {
  194. if( m_nodeId >= 0 )
  195. return UIUtils.GetNode( m_nodeId ) as PropertyNode;
  196. if( m_nodeId < -1 )
  197. return UIUtils.GetInternalTemplateNode( m_nodeId );
  198. return null;
  199. }
  200. public int IntValue { get { return (int)m_value; } set { m_value = value; } }
  201. public float FloatValue { get { return m_value; } set { m_value = value; } }
  202. public bool Active { get { return m_active; } set { m_active = value; } }
  203. public int NodeId { get { return m_nodeId; } set{ m_nodeId = value; } }
  204. }
  205. }