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.

293 lines
9.4 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. namespace AmplifyShaderEditor
  6. {
  7. public class NodeUtils
  8. {
  9. public delegate void DrawPropertySection();
  10. public static void DrawPropertyGroup( string sectionName, DrawPropertySection DrawSection )
  11. {
  12. Color cachedColor = GUI.color;
  13. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  14. EditorGUILayout.BeginHorizontal( UIUtils.MenuItemToolbarStyle );
  15. GUI.color = cachedColor;
  16. GUILayout.Label( sectionName, UIUtils.MenuItemToggleStyle );
  17. EditorGUILayout.EndHorizontal();
  18. cachedColor = GUI.color;
  19. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  20. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  21. GUI.color = cachedColor;
  22. DrawSection();
  23. EditorGUILayout.Separator();
  24. EditorGUILayout.EndVertical();
  25. }
  26. public static void DrawNestedPropertyGroup( ref bool foldoutValue, string sectionName, DrawPropertySection DrawSection, int horizontalSpacing = 15 )
  27. {
  28. GUILayout.BeginHorizontal();
  29. {
  30. GUILayout.Space( horizontalSpacing );
  31. EditorGUILayout.BeginVertical( EditorStyles.helpBox );
  32. {
  33. Color cachedColor = GUI.color;
  34. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  35. EditorGUILayout.BeginHorizontal();
  36. {
  37. GUI.color = cachedColor;
  38. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  39. if( Event.current.button == Constants.FoldoutMouseId )
  40. {
  41. foldoutValue = value;
  42. }
  43. }
  44. EditorGUILayout.EndHorizontal();
  45. EditorGUI.indentLevel--;
  46. if( foldoutValue )
  47. {
  48. cachedColor = GUI.color;
  49. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  50. {
  51. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  52. {
  53. GUI.color = cachedColor;
  54. DrawSection();
  55. }
  56. EditorGUILayout.EndVertical();
  57. EditorGUILayout.Separator();
  58. }
  59. }
  60. EditorGUI.indentLevel++;
  61. }
  62. EditorGUILayout.EndVertical();
  63. }
  64. GUILayout.EndHorizontal();
  65. }
  66. public static void DrawNestedPropertyGroup( ref bool foldoutValue, Rect rect, string sectionName, DrawPropertySection DrawSection, int horizontalSpacing = 15 )
  67. {
  68. var box = rect;
  69. box.height -= 2;
  70. GUI.Label( box, string.Empty, EditorStyles.helpBox );
  71. var tog = rect;
  72. #if UNITY_2019_3_OR_NEWER
  73. tog.y -= ( tog.height - ( EditorGUIUtility.singleLineHeight + 5 ) ) * 0.5f;
  74. #endif
  75. tog.xMin += 2;
  76. tog.xMax -= 2;
  77. tog.yMin += 2;
  78. bool value = GUI.Toggle( tog, foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  79. if( Event.current.button == Constants.FoldoutMouseId )
  80. {
  81. foldoutValue = value;
  82. }
  83. if( foldoutValue )
  84. {
  85. DrawSection();
  86. }
  87. }
  88. public static void DrawNestedPropertyGroup( ref bool foldoutValue, string sectionName, DrawPropertySection DrawSection, DrawPropertySection HeaderSection )
  89. {
  90. GUILayout.BeginHorizontal();
  91. {
  92. GUILayout.Space( 15 );
  93. EditorGUILayout.BeginVertical( EditorStyles.helpBox );
  94. Color cachedColor = GUI.color;
  95. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  96. EditorGUILayout.BeginHorizontal();
  97. GUI.color = cachedColor;
  98. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  99. if( Event.current.button == Constants.FoldoutMouseId )
  100. {
  101. foldoutValue = value;
  102. }
  103. HeaderSection();
  104. EditorGUILayout.EndHorizontal();
  105. EditorGUI.indentLevel--;
  106. if( foldoutValue )
  107. {
  108. cachedColor = GUI.color;
  109. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  110. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  111. GUI.color = cachedColor;
  112. DrawSection();
  113. EditorGUILayout.EndVertical();
  114. EditorGUILayout.Separator();
  115. }
  116. EditorGUI.indentLevel++;
  117. EditorGUILayout.EndVertical();
  118. }
  119. GUILayout.EndHorizontal();
  120. }
  121. public static void DrawNestedPropertyGroup( UndoParentNode owner, ref bool foldoutValue, ref bool enabledValue, string sectionName, DrawPropertySection DrawSection )
  122. {
  123. GUILayout.BeginHorizontal();
  124. {
  125. GUILayout.Space( 15 );
  126. EditorGUILayout.BeginVertical( EditorStyles.helpBox );
  127. Color cachedColor = GUI.color;
  128. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  129. EditorGUILayout.BeginHorizontal();
  130. GUI.color = cachedColor;
  131. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  132. if( Event.current.button == Constants.FoldoutMouseId )
  133. {
  134. foldoutValue = value;
  135. }
  136. value = ( (object)owner != null ) ? owner.GUILayoutToggle( enabledValue, string.Empty,UIUtils.MenuItemEnableStyle, GUILayout.Width( 16 ) ) :
  137. GUILayout.Toggle( enabledValue, string.Empty, UIUtils.MenuItemEnableStyle, GUILayout.Width( 16 ) );
  138. if( Event.current.button == Constants.FoldoutMouseId )
  139. {
  140. enabledValue = value;
  141. }
  142. EditorGUILayout.EndHorizontal();
  143. EditorGUI.indentLevel--;
  144. if( foldoutValue )
  145. {
  146. cachedColor = GUI.color;
  147. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  148. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  149. GUI.color = cachedColor;
  150. DrawSection();
  151. EditorGUILayout.EndVertical();
  152. EditorGUILayout.Separator();
  153. }
  154. EditorGUI.indentLevel++;
  155. EditorGUILayout.EndVertical();
  156. }
  157. GUILayout.EndHorizontal();
  158. }
  159. public static void DrawPropertyGroup( ref bool foldoutValue, string sectionName, DrawPropertySection DrawSection )
  160. {
  161. Color cachedColor = GUI.color;
  162. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  163. EditorGUILayout.BeginHorizontal( UIUtils.MenuItemToolbarStyle );
  164. GUI.color = cachedColor;
  165. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  166. if( Event.current.button == Constants.FoldoutMouseId )
  167. {
  168. foldoutValue = value;
  169. }
  170. EditorGUILayout.EndHorizontal();
  171. if( foldoutValue )
  172. {
  173. cachedColor = GUI.color;
  174. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  175. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  176. {
  177. GUI.color = cachedColor;
  178. EditorGUI.indentLevel++;
  179. DrawSection();
  180. EditorGUI.indentLevel--;
  181. EditorGUILayout.Separator();
  182. }
  183. EditorGUILayout.EndVertical();
  184. }
  185. }
  186. public static void DrawPropertyGroup( ref bool foldoutValue, string sectionName, DrawPropertySection DrawSection, DrawPropertySection HeaderSection )
  187. {
  188. Color cachedColor = GUI.color;
  189. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  190. EditorGUILayout.BeginHorizontal( UIUtils.MenuItemToolbarStyle );
  191. GUI.color = cachedColor;
  192. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  193. if( Event.current.button == Constants.FoldoutMouseId )
  194. {
  195. foldoutValue = value;
  196. }
  197. HeaderSection();
  198. EditorGUILayout.EndHorizontal();
  199. if( foldoutValue )
  200. {
  201. cachedColor = GUI.color;
  202. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  203. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  204. {
  205. GUI.color = cachedColor;
  206. EditorGUI.indentLevel++;
  207. DrawSection();
  208. EditorGUI.indentLevel--;
  209. EditorGUILayout.Separator();
  210. }
  211. EditorGUILayout.EndVertical();
  212. }
  213. }
  214. public static bool DrawPropertyGroup( UndoParentNode owner, ref bool foldoutValue, ref bool enabledValue, string sectionName, DrawPropertySection DrawSection )
  215. {
  216. bool enableChanged = false;
  217. Color cachedColor = GUI.color;
  218. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  219. EditorGUILayout.BeginHorizontal( UIUtils.MenuItemToolbarStyle );
  220. GUI.color = cachedColor;
  221. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle, GUILayout.ExpandWidth( true ) );
  222. if( Event.current.button == Constants.FoldoutMouseId )
  223. {
  224. foldoutValue = value;
  225. }
  226. EditorGUI.BeginChangeCheck();
  227. value = ( (object)owner != null ) ? owner.EditorGUILayoutToggle( string.Empty, enabledValue, UIUtils.MenuItemEnableStyle, GUILayout.Width( 16 ) ) :
  228. EditorGUILayout.Toggle( string.Empty, enabledValue, UIUtils.MenuItemEnableStyle, GUILayout.Width( 16 ) );
  229. if( Event.current.button == Constants.FoldoutMouseId )
  230. {
  231. enabledValue = value;
  232. }
  233. if( EditorGUI.EndChangeCheck() )
  234. {
  235. enableChanged = true;
  236. }
  237. EditorGUILayout.EndHorizontal();
  238. if( foldoutValue )
  239. {
  240. cachedColor = GUI.color;
  241. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  242. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  243. GUI.color = cachedColor;
  244. EditorGUILayout.Separator();
  245. EditorGUI.BeginDisabledGroup( !enabledValue );
  246. EditorGUI.indentLevel += 1;
  247. DrawSection();
  248. EditorGUI.indentLevel -= 1;
  249. EditorGUI.EndDisabledGroup();
  250. EditorGUILayout.Separator();
  251. EditorGUILayout.EndVertical();
  252. }
  253. return enableChanged;
  254. }
  255. }
  256. }