InlineProperty.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. [SerializeField]
  16. private string m_nodePropertyName = string.Empty;
  17. [SerializeField]
  18. private bool m_inlineButtonVisible = true;
  19. public InlineProperty()
  20. {
  21. InlinePropertyTable.Register( this );
  22. }
  23. public InlineProperty( float val ) : base()
  24. {
  25. m_value = val;
  26. }
  27. public InlineProperty( int val ) : base()
  28. {
  29. m_value = val;
  30. }
  31. public void ResetProperty()
  32. {
  33. m_nodeId = -1;
  34. m_active = false;
  35. }
  36. public void CopyFrom( InlineProperty other )
  37. {
  38. m_value = other.m_value;
  39. m_active = other.m_active;
  40. m_nodeId = other.m_nodeId;
  41. }
  42. public void SetInlineByName( string propertyName )
  43. {
  44. m_nodeId = UIUtils.GetFloatIntNodeIdByName( propertyName );
  45. m_nodePropertyName = propertyName;
  46. m_active = !string.IsNullOrEmpty( propertyName );
  47. }
  48. public void CheckInlineButton()
  49. {
  50. if( m_inlineButtonVisible )
  51. {
  52. if( GUILayout.Button( UIUtils.FloatIntIconON , UIUtils.FloatIntPickerONOFF , GUILayout.Width( 15 ) , GUILayout.Height( 15 ) ) )
  53. m_active = !m_active;
  54. }
  55. }
  56. public void IntField( ref UndoParentNode owner , string content )
  57. {
  58. if( !m_active )
  59. {
  60. EditorGUILayout.BeginHorizontal();
  61. m_value = owner.EditorGUILayoutIntField( content , (int)m_value );
  62. CheckInlineButton();
  63. EditorGUILayout.EndHorizontal();
  64. }
  65. else
  66. {
  67. DrawPicker( ref owner , content );
  68. }
  69. }
  70. public void IntSlider( ref UndoParentNode owner , GUIContent content , int min , int max )
  71. {
  72. if( !m_active )
  73. {
  74. EditorGUILayout.BeginHorizontal();
  75. m_value = owner.EditorGUILayoutIntSlider( content , (int)m_value , min , max );
  76. CheckInlineButton();
  77. EditorGUILayout.EndHorizontal();
  78. }
  79. else
  80. {
  81. DrawPicker( ref owner , content );
  82. }
  83. }
  84. public void IntSlider( ref UndoParentNode owner , string content , int min , int max )
  85. {
  86. if( !m_active )
  87. {
  88. EditorGUILayout.BeginHorizontal();
  89. m_value = owner.EditorGUILayoutIntSlider( content , (int)m_value , min , max );
  90. CheckInlineButton();
  91. EditorGUILayout.EndHorizontal();
  92. }
  93. else
  94. {
  95. DrawPicker( ref owner , content );
  96. }
  97. }
  98. public void EnumTypePopup( ref UndoParentNode owner , string content , string[] displayOptions )
  99. {
  100. if( !m_active )
  101. {
  102. EditorGUILayout.BeginHorizontal();
  103. m_value = owner.EditorGUILayoutPopup( content , (int)m_value , displayOptions );
  104. CheckInlineButton();
  105. EditorGUILayout.EndHorizontal();
  106. }
  107. else
  108. {
  109. DrawPicker( ref owner , content );
  110. }
  111. }
  112. public void FloatField( ref UndoParentNode owner , string content )
  113. {
  114. if( !m_active )
  115. {
  116. EditorGUILayout.BeginHorizontal();
  117. m_value = owner.EditorGUILayoutFloatField( content , m_value );
  118. CheckInlineButton();
  119. EditorGUILayout.EndHorizontal();
  120. }
  121. else
  122. {
  123. DrawPicker( ref owner , content );
  124. }
  125. }
  126. public void SliderField( ref UndoParentNode owner , string content , float min , float max )
  127. {
  128. if( !m_active )
  129. {
  130. EditorGUILayout.BeginHorizontal();
  131. m_value = owner.EditorGUILayoutSlider( content , m_value , min , max );
  132. CheckInlineButton();
  133. EditorGUILayout.EndHorizontal();
  134. }
  135. else
  136. {
  137. DrawPicker( ref owner , content );
  138. }
  139. }
  140. public void RangedFloatField( ref UndoParentNode owner , string content , float min , float max )
  141. {
  142. if( !m_active )
  143. {
  144. EditorGUILayout.BeginHorizontal();
  145. m_value = owner.EditorGUILayoutRangedFloatField( content , m_value , min , max );
  146. CheckInlineButton();
  147. EditorGUILayout.EndHorizontal();
  148. }
  149. else
  150. {
  151. DrawPicker( ref owner , content );
  152. }
  153. }
  154. public void CustomDrawer( ref UndoParentNode owner , DrawPropertySection Drawer , string content )
  155. {
  156. if( !m_active )
  157. {
  158. EditorGUILayout.BeginHorizontal();
  159. Drawer( owner );
  160. CheckInlineButton();
  161. EditorGUILayout.EndHorizontal();
  162. }
  163. else
  164. {
  165. DrawPicker( ref owner , content );
  166. }
  167. }
  168. public delegate void DrawPropertySection( UndoParentNode owner );
  169. private void DrawPicker( ref UndoParentNode owner , GUIContent content )
  170. {
  171. DrawPicker( ref owner , content.text );
  172. }
  173. private void DrawPicker( ref UndoParentNode owner , string content )
  174. {
  175. EditorGUILayout.BeginHorizontal();
  176. string[] intArraysNames = owner.ContainerGraph.ParentWindow.CurrentGraph.FloatIntNodes.NodesArr;
  177. int[] intIds = owner.ContainerGraph.ParentWindow.CurrentGraph.FloatIntNodes.NodeIds;
  178. int prevNodeId = m_nodeId;
  179. m_nodeId = owner.EditorGUILayoutIntPopup( content , m_nodeId , intArraysNames , intIds );
  180. if ( m_nodeId != prevNodeId )
  181. {
  182. m_nodePropertyName = UIUtils.GetFloatIntNameByNodeId( m_nodeId, m_nodePropertyName );
  183. }
  184. if ( GUILayout.Button( UIUtils.FloatIntIconOFF , UIUtils.FloatIntPickerONOFF , GUILayout.Width( 15 ) , GUILayout.Height( 15 ) ) )
  185. m_active = !m_active;
  186. EditorGUILayout.EndHorizontal();
  187. }
  188. public string GetValueOrProperty( bool parentesis = true )
  189. {
  190. if( m_active )
  191. {
  192. PropertyNode node = GetPropertyNode();
  193. if( node != null )
  194. {
  195. return parentesis ? "[" + node.PropertyName + "]" : node.PropertyName;
  196. }
  197. else if ( !string.IsNullOrEmpty( m_nodePropertyName ) )
  198. {
  199. return parentesis ? "[" + m_nodePropertyName + "]" : m_nodePropertyName;
  200. }
  201. else
  202. {
  203. m_active = false;
  204. m_nodeId = -1;
  205. return m_value.ToString();
  206. }
  207. }
  208. else
  209. {
  210. return m_value.ToString();
  211. }
  212. }
  213. public string GetValueOrProperty( string defaultValue , bool parentesis = true )
  214. {
  215. if( m_active )
  216. {
  217. PropertyNode node = GetPropertyNode();
  218. if( node != null )
  219. {
  220. return parentesis ? "[" + node.PropertyName + "]" : node.PropertyName;
  221. }
  222. else if ( !string.IsNullOrEmpty( m_nodePropertyName ) )
  223. {
  224. return parentesis ? "[" + m_nodePropertyName + "]" : m_nodePropertyName;
  225. }
  226. else if( !string.IsNullOrEmpty( defaultValue ) )
  227. {
  228. m_active = false;
  229. m_nodeId = -1;
  230. return defaultValue;
  231. }
  232. else
  233. {
  234. m_active = false;
  235. m_nodeId = -1;
  236. return m_value.ToString();
  237. }
  238. }
  239. else
  240. {
  241. return defaultValue;
  242. }
  243. }
  244. public void TryResolveDependency()
  245. {
  246. if ( m_active && !string.IsNullOrEmpty( m_nodePropertyName ) )
  247. {
  248. m_nodeId = UIUtils.GetFloatIntNodeIdByName( m_nodePropertyName );
  249. }
  250. }
  251. private void TryReadUniqueId( string param )
  252. {
  253. if ( Preferences.User.ForceTemplateInlineProperties && !string.IsNullOrEmpty( m_nodePropertyName ) )
  254. {
  255. // @diogo: exception path => ignore param and revert to template default
  256. m_nodeId = UIUtils.GetFloatIntNodeIdByName( m_nodePropertyName );
  257. // @diogo: by defaulting to template we are signaling the inline property is active
  258. m_active = true;
  259. }
  260. else
  261. {
  262. // @diogo: normal path
  263. if ( int.TryParse( param, out int nodeId ) )
  264. {
  265. m_nodeId = Convert.ToInt32( param );
  266. m_nodePropertyName = UIUtils.GetFloatIntNameByNodeId( m_nodeId, m_nodePropertyName );
  267. }
  268. else
  269. {
  270. m_nodePropertyName = param;
  271. m_nodeId = UIUtils.GetFloatIntNodeIdByName( m_nodePropertyName );
  272. }
  273. }
  274. }
  275. public void ReadFromString( ref uint index , ref string[] nodeParams , bool isInt = true )
  276. {
  277. m_value = isInt ? Convert.ToInt32( nodeParams[ index++ ] ) : Convert.ToSingle( nodeParams[ index++ ] );
  278. m_active = Convert.ToBoolean( nodeParams[ index++ ] );
  279. TryReadUniqueId( nodeParams[ index++ ] );
  280. }
  281. public void ReadFromSingle( string singleLine )
  282. {
  283. string[] data = singleLine.Split( IOUtils.VECTOR_SEPARATOR );
  284. m_value = Convert.ToSingle( data[ 0 ] , System.Globalization.CultureInfo.InvariantCulture );
  285. m_active = Convert.ToBoolean( data[ 1 ] );
  286. TryReadUniqueId( data[ 2 ] );
  287. }
  288. public void WriteToString( ref string nodeInfo )
  289. {
  290. IOUtils.AddFieldValueToString( ref nodeInfo , m_value );
  291. IOUtils.AddFieldValueToString( ref nodeInfo , m_active );
  292. IOUtils.AddFieldValueToString( ref nodeInfo, m_nodePropertyName );
  293. }
  294. public string WriteToSingle()
  295. {
  296. return m_value.ToString( System.Globalization.CultureInfo.InvariantCulture ) + IOUtils.VECTOR_SEPARATOR + m_active + IOUtils.VECTOR_SEPARATOR + m_nodePropertyName;
  297. }
  298. public void SetInlineNodeValue()
  299. {
  300. if( IsValid )
  301. {
  302. RangedFloatNode fnode = UIUtils.GetNode( m_nodeId ) as RangedFloatNode;
  303. if( fnode != null )
  304. {
  305. fnode.Value = m_value;
  306. fnode.SetMaterialValueFromInline( m_value );
  307. }
  308. else
  309. {
  310. IntNode inode = UIUtils.GetNode( m_nodeId ) as IntNode;
  311. inode.Value = (int)m_value;
  312. inode.SetMaterialValueFromInline( (int)m_value );
  313. }
  314. }
  315. }
  316. public bool IsValid { get { return m_active; } }
  317. public PropertyNode GetPropertyNode()
  318. {
  319. if( m_nodeId >= 0 )
  320. return UIUtils.GetNode( m_nodeId ) as PropertyNode;
  321. if( m_nodeId < -1 )
  322. {
  323. if( !string.IsNullOrEmpty( m_nodePropertyName ) )
  324. return UIUtils.GetInternalTemplateNode( m_nodePropertyName );
  325. return UIUtils.GetInternalTemplateNode( m_nodeId );
  326. }
  327. return null;
  328. }
  329. public void HideInlineButton()
  330. {
  331. m_inlineButtonVisible = false;
  332. }
  333. public int IntValue { get { return (int)m_value; } set { m_value = value; } }
  334. public float FloatValue { get { return m_value; } set { m_value = value; } }
  335. public bool Active { get { return m_active; } set { m_active = value; } }
  336. public int NodeId { get { return m_nodeId; } set { m_nodeId = value; } }
  337. }
  338. }