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.

300 lines
7.9 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace AmplifyShaderEditor
  8. {
  9. // UI STRUCTURES
  10. [Serializable]
  11. public class TemplateOptionUIItem
  12. {
  13. public delegate void OnActionPerformed( bool isRefreshing, bool invertAction, TemplateOptionUIItem uiItem, params TemplateActionItem[] validActions );
  14. public event OnActionPerformed OnActionPerformedEvt;
  15. [SerializeField]
  16. private bool m_isVisible = true;
  17. [SerializeField]
  18. private bool m_wasVisible = true;
  19. [SerializeField]
  20. private int m_currentOption = 0;
  21. [SerializeField]
  22. private TemplateOptionsItem m_options;
  23. [SerializeField]
  24. private bool m_checkOnExecute = false;
  25. [SerializeField]
  26. private bool m_invertActionOnDeselection = false;
  27. public TemplateOptionUIItem( TemplateOptionsItem options )
  28. {
  29. m_options = options;
  30. if( m_options.Type == AseOptionsType.Field )
  31. {
  32. m_options.FieldValue.FloatValue = m_options.DefaultFieldValue;
  33. }
  34. else
  35. {
  36. m_currentOption = m_options.DefaultOptionIndex;
  37. }
  38. m_invertActionOnDeselection = options.Setup == AseOptionItemSetup.InvertActionOnDeselection;
  39. }
  40. public void CopyValuesFrom( TemplateOptionUIItem origin )
  41. {
  42. m_isVisible = origin.IsVisible;
  43. m_wasVisible = origin.WasVisible;
  44. m_currentOption = origin.CurrentOption;
  45. m_options.FieldValue.FloatValue = origin.CurrentFieldValue;
  46. m_checkOnExecute = origin.CheckOnExecute;
  47. m_invertActionOnDeselection = origin.InvertActionOnDeselection;
  48. }
  49. public void Draw( UndoParentNode owner )
  50. {
  51. if( m_isVisible )
  52. {
  53. int lastOption = m_currentOption;
  54. EditorGUI.BeginChangeCheck();
  55. switch( m_options.UIWidget )
  56. {
  57. case AseOptionsUIWidget.Dropdown:
  58. {
  59. m_currentOption = owner.EditorGUILayoutPopup( m_options.Name, m_currentOption, m_options.DisplayOptions );
  60. }
  61. break;
  62. case AseOptionsUIWidget.Toggle:
  63. {
  64. m_currentOption = owner.EditorGUILayoutToggle( m_options.Name, m_currentOption == 1 ) ? 1 : 0;
  65. }
  66. break;
  67. case AseOptionsUIWidget.Float:
  68. {
  69. if( m_options.FieldInline )
  70. {
  71. m_options.FieldValue.FloatField( ref owner, m_options.Name );
  72. if( m_options.FieldValue.Active )
  73. m_currentOption = 1;
  74. else
  75. m_currentOption = 0;
  76. }
  77. else
  78. {
  79. m_options.FieldValue.FloatValue = owner.EditorGUILayoutFloatField( m_options.Name, m_options.FieldValue.FloatValue );
  80. }
  81. }
  82. break;
  83. case AseOptionsUIWidget.Int:
  84. {
  85. if( m_options.FieldInline )
  86. {
  87. m_options.FieldValue.IntField( ref owner, m_options.Name );
  88. if( m_options.FieldValue.Active )
  89. m_currentOption = 1;
  90. else
  91. m_currentOption = 0;
  92. }
  93. else
  94. m_options.FieldValue.FloatValue = owner.EditorGUILayoutIntField( m_options.Name, (int)m_options.FieldValue.FloatValue );
  95. }
  96. break;
  97. case AseOptionsUIWidget.FloatRange:
  98. {
  99. if( m_options.FieldInline )
  100. {
  101. m_options.FieldValue.SliderField( ref owner, m_options.Name, m_options.FieldMin, m_options.FieldMax );
  102. if( m_options.FieldValue.Active )
  103. m_currentOption = 1;
  104. else
  105. m_currentOption = 0;
  106. }
  107. else
  108. m_options.FieldValue.FloatValue = owner.EditorGUILayoutSlider( m_options.Name, m_options.FieldValue.FloatValue, m_options.FieldMin, m_options.FieldMax );
  109. }
  110. break;
  111. case AseOptionsUIWidget.IntRange:
  112. {
  113. if( m_options.FieldInline )
  114. {
  115. m_options.FieldValue.IntSlider( ref owner, m_options.Name, (int)m_options.FieldMin, (int)m_options.FieldMax );
  116. if( m_options.FieldValue.Active )
  117. m_currentOption = 1;
  118. else
  119. m_currentOption = 0;
  120. }
  121. else
  122. m_options.FieldValue.FloatValue = owner.EditorGUILayoutIntSlider( m_options.Name, (int)m_options.FieldValue.FloatValue, (int)m_options.FieldMin, (int)m_options.FieldMax );
  123. }
  124. break;
  125. }
  126. if( EditorGUI.EndChangeCheck() )
  127. {
  128. if( OnActionPerformedEvt != null )
  129. {
  130. if( m_invertActionOnDeselection )
  131. OnActionPerformedEvt( false, lastOption != m_options.DisableIdx, this, m_options.ActionsPerOption[ lastOption ] );
  132. OnActionPerformedEvt( false, false, this, m_options.ActionsPerOption[ m_currentOption ] );
  133. }
  134. }
  135. }
  136. }
  137. public void CheckEnDisable()
  138. {
  139. //string deb = string.Empty;// "-- Checked --" + m_options.Name+" "+ m_isVisible + " "+ m_wasVisible;
  140. if( m_isVisible )
  141. {
  142. if( !m_wasVisible )
  143. {
  144. //deb = "-- Enable --" + m_options.Name;
  145. //Debug.Log( deb );
  146. if( OnActionPerformedEvt != null )
  147. {
  148. if( m_invertActionOnDeselection )
  149. {
  150. for( int i = 0; i < m_options.Count; i++ )
  151. {
  152. if( i != m_currentOption && i != m_options.DisableIdx )
  153. {
  154. OnActionPerformedEvt( false, true, this, m_options.ActionsPerOption[ i ] );
  155. }
  156. }
  157. }
  158. OnActionPerformedEvt( false, false, this, m_options.ActionsPerOption[ m_currentOption ] );
  159. //if( !m_isVisible )
  160. //OnActionPerformedEvt( isRefreshing, false, this, m_options.ActionsPerOption[ m_options.DisableIdx ] );
  161. }
  162. }
  163. m_wasVisible = true;
  164. }
  165. else if( m_wasVisible )
  166. {
  167. //deb = "-- Disable --" + m_options.Name;
  168. //Debug.Log( deb );
  169. m_wasVisible = false;
  170. if( OnActionPerformedEvt != null )
  171. {
  172. OnActionPerformedEvt( false, false, this, m_options.ActionsPerOption[ m_options.DisableIdx ] );
  173. }
  174. }
  175. }
  176. public void FillDataCollector( ref MasterNodeDataCollector dataCollector )
  177. {
  178. if( m_isVisible && m_checkOnExecute )
  179. {
  180. for( int i = 0; i < m_options.ActionsPerOption[ m_currentOption ].Length; i++ )
  181. {
  182. switch( m_options.ActionsPerOption[ m_currentOption ][ i ].ActionType )
  183. {
  184. case AseOptionsActionType.SetDefine:
  185. {
  186. dataCollector.AddToDefines( -1, m_options.ActionsPerOption[ m_currentOption ][ i ].ActionData );
  187. }
  188. break;
  189. case AseOptionsActionType.SetUndefine:
  190. {
  191. dataCollector.AddToDefines( -1, m_options.ActionsPerOption[ m_currentOption ][ i ].ActionData, false );
  192. }
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. public void Refresh()
  199. {
  200. if( OnActionPerformedEvt != null )
  201. {
  202. if( m_invertActionOnDeselection )
  203. {
  204. for( int i = 0; i < m_options.Count; i++ )
  205. {
  206. if( i != m_currentOption && i != m_options.DisableIdx )
  207. {
  208. OnActionPerformedEvt( true, true, this, m_options.ActionsPerOption[ i ] );
  209. }
  210. }
  211. }
  212. OnActionPerformedEvt( true, false, this, m_options.ActionsPerOption[ m_currentOption ] );
  213. }
  214. }
  215. public TemplateOptionsItem Options { get { return m_options; } }
  216. public void Destroy()
  217. {
  218. OnActionPerformedEvt = null;
  219. }
  220. public bool IsVisible
  221. {
  222. get { return m_isVisible; }
  223. set { m_isVisible = value; }
  224. }
  225. public bool WasVisible
  226. {
  227. get { return m_wasVisible; }
  228. set { m_wasVisible = value; }
  229. }
  230. public bool CheckOnExecute
  231. {
  232. get { return m_checkOnExecute; }
  233. set { m_checkOnExecute = value; }
  234. }
  235. public InlineProperty FieldValue
  236. {
  237. get { return m_options.FieldValue; }
  238. set { m_options.FieldValue = value; }
  239. }
  240. public float CurrentFieldValue
  241. {
  242. get { return m_options.FieldValue.FloatValue; }
  243. set { m_options.FieldValue.FloatValue = value; }
  244. }
  245. public int CurrentOption
  246. {
  247. get { return m_currentOption; }
  248. set
  249. {
  250. m_currentOption = Mathf.Clamp( value, 0, m_options.Options.Length - 1 );
  251. // why refreshing here?
  252. //Refresh();
  253. }
  254. }
  255. public int CurrentOptionIdx
  256. {
  257. set
  258. {
  259. m_currentOption = Mathf.Clamp( value, 0, m_options.Options.Length - 1 );
  260. }
  261. }
  262. public bool EmptyEvent { get { return OnActionPerformedEvt == null; } }
  263. public TemplateActionItemGrid.TemplateActionItemRow CurrentOptionActions
  264. {
  265. get
  266. {
  267. return m_options.ActionsPerOption.Rows[m_currentOption];
  268. }
  269. }
  270. public bool InvertActionOnDeselection { get { return m_invertActionOnDeselection; } }
  271. }
  272. }