Assignment for RMIT Mixed Reality in 2020
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.

320 lines
8.4 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. [SerializeField]
  16. private string m_nodePropertyName = string.Empty;
  17. public InlineProperty() { }
  18. public InlineProperty( float val )
  19. {
  20. m_value = val;
  21. }
  22. public InlineProperty( int val )
  23. {
  24. m_value = val;
  25. }
  26. public void ResetProperty()
  27. {
  28. m_nodeId = -1;
  29. m_active = false;
  30. }
  31. public void CopyFrom( InlineProperty other )
  32. {
  33. m_value = other.m_value;
  34. m_active = other.m_active;
  35. m_nodeId = other.m_nodeId;
  36. }
  37. public void SetInlineByName( string propertyName )
  38. {
  39. m_nodeId = UIUtils.GetNodeIdByName( propertyName );
  40. m_nodePropertyName = propertyName;
  41. m_active = m_nodeId != -1;
  42. }
  43. public void IntField( ref UndoParentNode owner, string content )
  44. {
  45. if( !m_active )
  46. {
  47. EditorGUILayout.BeginHorizontal();
  48. m_value = owner.EditorGUILayoutIntField( content, (int)m_value );
  49. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  50. m_active = !m_active;
  51. EditorGUILayout.EndHorizontal();
  52. }
  53. else
  54. {
  55. DrawPicker( ref owner, content );
  56. }
  57. }
  58. public void IntSlider( ref UndoParentNode owner, GUIContent content, int min, int max )
  59. {
  60. if( !m_active )
  61. {
  62. EditorGUILayout.BeginHorizontal();
  63. m_value = owner.EditorGUILayoutIntSlider( content, (int)m_value, min, max );
  64. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  65. m_active = !m_active;
  66. EditorGUILayout.EndHorizontal();
  67. }
  68. else
  69. {
  70. DrawPicker( ref owner, content );
  71. }
  72. }
  73. public void IntSlider( ref UndoParentNode owner, string content, int min, int max )
  74. {
  75. if( !m_active )
  76. {
  77. EditorGUILayout.BeginHorizontal();
  78. m_value = owner.EditorGUILayoutIntSlider( content, (int)m_value, min, max );
  79. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  80. m_active = !m_active;
  81. EditorGUILayout.EndHorizontal();
  82. }
  83. else
  84. {
  85. DrawPicker( ref owner, content );
  86. }
  87. }
  88. public void EnumTypePopup( ref UndoParentNode owner, string content, string[] displayOptions )
  89. {
  90. if( !m_active )
  91. {
  92. EditorGUILayout.BeginHorizontal();
  93. m_value = owner.EditorGUILayoutPopup( content, (int)m_value, displayOptions );
  94. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  95. m_active = !m_active;
  96. EditorGUILayout.EndHorizontal();
  97. }
  98. else
  99. {
  100. DrawPicker( ref owner, content );
  101. }
  102. }
  103. public void FloatField( ref UndoParentNode owner, string content )
  104. {
  105. if( !m_active )
  106. {
  107. EditorGUILayout.BeginHorizontal();
  108. m_value = owner.EditorGUILayoutFloatField( content, m_value );
  109. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  110. m_active = !m_active;
  111. EditorGUILayout.EndHorizontal();
  112. }
  113. else
  114. {
  115. DrawPicker( ref owner, content );
  116. }
  117. }
  118. public void SliderField( ref UndoParentNode owner, string content, float min, float max )
  119. {
  120. if( !m_active )
  121. {
  122. EditorGUILayout.BeginHorizontal();
  123. m_value = owner.EditorGUILayoutSlider( content, m_value, min, max );
  124. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  125. m_active = !m_active;
  126. EditorGUILayout.EndHorizontal();
  127. }
  128. else
  129. {
  130. DrawPicker( ref owner, content );
  131. }
  132. }
  133. public void RangedFloatField( ref UndoParentNode owner, string content, float min, float max )
  134. {
  135. if( !m_active )
  136. {
  137. EditorGUILayout.BeginHorizontal();
  138. m_value = owner.EditorGUILayoutRangedFloatField( content, m_value, min, max );
  139. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  140. m_active = !m_active;
  141. EditorGUILayout.EndHorizontal();
  142. }
  143. else
  144. {
  145. DrawPicker( ref owner, content );
  146. }
  147. }
  148. public void CustomDrawer( ref UndoParentNode owner, DrawPropertySection Drawer, string content )
  149. {
  150. if( !m_active )
  151. {
  152. EditorGUILayout.BeginHorizontal();
  153. Drawer( owner );
  154. if( GUILayout.Button( UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  155. m_active = !m_active;
  156. EditorGUILayout.EndHorizontal();
  157. }
  158. else
  159. {
  160. DrawPicker( ref owner, content );
  161. }
  162. }
  163. public delegate void DrawPropertySection( UndoParentNode owner );
  164. private void DrawPicker( ref UndoParentNode owner, GUIContent content )
  165. {
  166. DrawPicker( ref owner, content.text );
  167. }
  168. private void DrawPicker( ref UndoParentNode owner, string content )
  169. {
  170. EditorGUILayout.BeginHorizontal();
  171. m_nodeId = owner.EditorGUILayoutIntPopup( content, m_nodeId, UIUtils.FloatIntNodeArr(), UIUtils.FloatIntNodeIds() );
  172. if( GUILayout.Button( UIUtils.FloatIntIconOFF, UIUtils.FloatIntPickerONOFF, GUILayout.Width( 15 ), GUILayout.Height( 15 ) ) )
  173. m_active = !m_active;
  174. EditorGUILayout.EndHorizontal();
  175. }
  176. public string GetValueOrProperty( bool parentesis = true )
  177. {
  178. if( m_active )
  179. {
  180. PropertyNode node = GetPropertyNode();
  181. if( node != null )
  182. {
  183. return parentesis ? "[" + node.PropertyName + "]" : node.PropertyName;
  184. }
  185. else
  186. {
  187. m_active = false;
  188. m_nodeId = -1;
  189. return m_value.ToString();
  190. }
  191. }
  192. else
  193. {
  194. return m_value.ToString();
  195. }
  196. }
  197. public string GetValueOrProperty( string defaultValue, bool parentesis = true )
  198. {
  199. if( m_active )
  200. {
  201. PropertyNode node = GetPropertyNode();
  202. if( node != null )
  203. {
  204. return parentesis ? "[" + node.PropertyName + "]" : node.PropertyName;
  205. }
  206. else if( !string.IsNullOrEmpty( defaultValue ) )
  207. {
  208. m_active = false;
  209. m_nodeId = -1;
  210. return defaultValue;
  211. }
  212. else
  213. {
  214. m_active = false;
  215. m_nodeId = -1;
  216. return m_value.ToString();
  217. }
  218. }
  219. else
  220. {
  221. return defaultValue;
  222. }
  223. }
  224. public void ReadFromString( ref uint index, ref string[] nodeParams, bool isInt = true )
  225. {
  226. m_value = isInt ? Convert.ToInt32( nodeParams[ index++ ] ) : Convert.ToSingle( nodeParams[ index++ ] );
  227. m_active = Convert.ToBoolean( nodeParams[ index++ ] );
  228. m_nodeId = Convert.ToInt32( nodeParams[ index++ ] );
  229. }
  230. public void ReadFromSingle( string singleLine )
  231. {
  232. string[] data = singleLine.Split( IOUtils.VECTOR_SEPARATOR );
  233. m_value = Convert.ToSingle( data[ 0 ], System.Globalization.CultureInfo.InvariantCulture );
  234. m_active = Convert.ToBoolean( data[ 1 ] );
  235. m_nodeId = Convert.ToInt32( data[ 2 ] );
  236. }
  237. public void WriteToString( ref string nodeInfo )
  238. {
  239. IOUtils.AddFieldValueToString( ref nodeInfo, m_value );
  240. IOUtils.AddFieldValueToString( ref nodeInfo, m_active );
  241. IOUtils.AddFieldValueToString( ref nodeInfo, m_nodeId );
  242. }
  243. public string WriteToSingle()
  244. {
  245. return m_value.ToString( System.Globalization.CultureInfo.InvariantCulture ) + IOUtils.VECTOR_SEPARATOR + m_active + IOUtils.VECTOR_SEPARATOR + m_nodeId;
  246. }
  247. public void SetInlineNodeValue()
  248. {
  249. if( IsValid )
  250. {
  251. RangedFloatNode fnode = UIUtils.GetNode( m_nodeId ) as RangedFloatNode;
  252. if( fnode != null )
  253. {
  254. fnode.Value = m_value;
  255. fnode.SetMaterialValueFromInline( m_value );
  256. }
  257. else
  258. {
  259. IntNode inode = UIUtils.GetNode( m_nodeId ) as IntNode;
  260. inode.Value = (int)m_value;
  261. inode.SetMaterialValueFromInline( (int)m_value );
  262. }
  263. }
  264. }
  265. public bool IsValid { get { return m_active && m_nodeId != -1; } }
  266. public PropertyNode GetPropertyNode()
  267. {
  268. if( m_nodeId >= 0 )
  269. return UIUtils.GetNode( m_nodeId ) as PropertyNode;
  270. if( m_nodeId < -1 )
  271. {
  272. if(!string.IsNullOrEmpty(m_nodePropertyName))
  273. return UIUtils.GetInternalTemplateNode( m_nodePropertyName );
  274. return UIUtils.GetInternalTemplateNode( m_nodeId );
  275. }
  276. return null;
  277. }
  278. public int IntValue { get { return (int)m_value; } set { m_value = value; } }
  279. public float FloatValue { get { return m_value; } set { m_value = value; } }
  280. public bool Active { get { return m_active; } set { m_active = value; } }
  281. public int NodeId { get { return m_nodeId; } set { m_nodeId = value; } }
  282. }
  283. }