NodeUtils.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. tog.y -= ( tog.height - ( EditorGUIUtility.singleLineHeight + 5 ) ) * 0.5f;
  73. tog.xMin += 2;
  74. tog.xMax -= 2;
  75. tog.yMin += 2;
  76. bool value = GUI.Toggle( tog, foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  77. if( Event.current.button == Constants.FoldoutMouseId )
  78. {
  79. foldoutValue = value;
  80. }
  81. if( foldoutValue )
  82. {
  83. DrawSection();
  84. }
  85. }
  86. public static void DrawNestedPropertyGroup( ref bool foldoutValue, string sectionName, DrawPropertySection DrawSection, DrawPropertySection HeaderSection )
  87. {
  88. GUILayout.BeginHorizontal();
  89. {
  90. GUILayout.Space( 15 );
  91. EditorGUILayout.BeginVertical( EditorStyles.helpBox );
  92. Color cachedColor = GUI.color;
  93. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  94. EditorGUILayout.BeginHorizontal();
  95. GUI.color = cachedColor;
  96. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  97. if( Event.current.button == Constants.FoldoutMouseId )
  98. {
  99. foldoutValue = value;
  100. }
  101. HeaderSection();
  102. EditorGUILayout.EndHorizontal();
  103. EditorGUI.indentLevel--;
  104. if( foldoutValue )
  105. {
  106. cachedColor = GUI.color;
  107. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  108. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  109. GUI.color = cachedColor;
  110. DrawSection();
  111. EditorGUILayout.EndVertical();
  112. EditorGUILayout.Separator();
  113. }
  114. EditorGUI.indentLevel++;
  115. EditorGUILayout.EndVertical();
  116. }
  117. GUILayout.EndHorizontal();
  118. }
  119. public static void DrawNestedPropertyGroup( UndoParentNode owner, ref bool foldoutValue, ref bool enabledValue, string sectionName, DrawPropertySection DrawSection )
  120. {
  121. GUILayout.BeginHorizontal();
  122. {
  123. GUILayout.Space( 15 );
  124. EditorGUILayout.BeginVertical( EditorStyles.helpBox );
  125. Color cachedColor = GUI.color;
  126. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  127. EditorGUILayout.BeginHorizontal();
  128. GUI.color = cachedColor;
  129. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  130. if( Event.current.button == Constants.FoldoutMouseId )
  131. {
  132. foldoutValue = value;
  133. }
  134. value = ( (object)owner != null ) ? owner.GUILayoutToggle( enabledValue, string.Empty,UIUtils.MenuItemEnableStyle, GUILayout.Width( 16 ) ) :
  135. GUILayout.Toggle( enabledValue, string.Empty, UIUtils.MenuItemEnableStyle, GUILayout.Width( 16 ) );
  136. if( Event.current.button == Constants.FoldoutMouseId )
  137. {
  138. enabledValue = value;
  139. }
  140. EditorGUILayout.EndHorizontal();
  141. EditorGUI.indentLevel--;
  142. if( foldoutValue )
  143. {
  144. cachedColor = GUI.color;
  145. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  146. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  147. GUI.color = cachedColor;
  148. DrawSection();
  149. EditorGUILayout.EndVertical();
  150. EditorGUILayout.Separator();
  151. }
  152. EditorGUI.indentLevel++;
  153. EditorGUILayout.EndVertical();
  154. }
  155. GUILayout.EndHorizontal();
  156. }
  157. public static void DrawPropertyGroup( ref bool foldoutValue, string sectionName, DrawPropertySection DrawSection )
  158. {
  159. Color cachedColor = GUI.color;
  160. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  161. EditorGUILayout.BeginHorizontal( UIUtils.MenuItemToolbarStyle );
  162. GUI.color = cachedColor;
  163. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  164. if( Event.current.button == Constants.FoldoutMouseId )
  165. {
  166. foldoutValue = value;
  167. }
  168. EditorGUILayout.EndHorizontal();
  169. if( foldoutValue )
  170. {
  171. cachedColor = GUI.color;
  172. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  173. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  174. {
  175. GUI.color = cachedColor;
  176. EditorGUI.indentLevel++;
  177. DrawSection();
  178. EditorGUI.indentLevel--;
  179. EditorGUILayout.Separator();
  180. }
  181. EditorGUILayout.EndVertical();
  182. }
  183. }
  184. public static void DrawPropertyGroup( ref bool foldoutValue, string sectionName, DrawPropertySection DrawSection, DrawPropertySection HeaderSection )
  185. {
  186. Color cachedColor = GUI.color;
  187. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  188. EditorGUILayout.BeginHorizontal( UIUtils.MenuItemToolbarStyle );
  189. GUI.color = cachedColor;
  190. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle );
  191. if( Event.current.button == Constants.FoldoutMouseId )
  192. {
  193. foldoutValue = value;
  194. }
  195. HeaderSection();
  196. EditorGUILayout.EndHorizontal();
  197. if( foldoutValue )
  198. {
  199. cachedColor = GUI.color;
  200. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  201. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  202. {
  203. GUI.color = cachedColor;
  204. EditorGUI.indentLevel++;
  205. DrawSection();
  206. EditorGUI.indentLevel--;
  207. EditorGUILayout.Separator();
  208. }
  209. EditorGUILayout.EndVertical();
  210. }
  211. }
  212. public static bool DrawPropertyGroup( UndoParentNode owner, ref bool foldoutValue, ref bool enabledValue, string sectionName, DrawPropertySection DrawSection )
  213. {
  214. bool enableChanged = false;
  215. Color cachedColor = GUI.color;
  216. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  217. EditorGUILayout.BeginHorizontal( UIUtils.MenuItemToolbarStyle );
  218. GUI.color = cachedColor;
  219. bool value = GUILayout.Toggle( foldoutValue, sectionName, UIUtils.MenuItemToggleStyle, GUILayout.ExpandWidth( true ) );
  220. if( Event.current.button == Constants.FoldoutMouseId )
  221. {
  222. foldoutValue = value;
  223. }
  224. EditorGUI.BeginChangeCheck();
  225. value = ( (object)owner != null ) ? owner.EditorGUILayoutToggle( string.Empty, enabledValue, UIUtils.MenuItemEnableStyle, GUILayout.Width( 16 ) ) :
  226. EditorGUILayout.Toggle( string.Empty, enabledValue, UIUtils.MenuItemEnableStyle, GUILayout.Width( 16 ) );
  227. if( Event.current.button == Constants.FoldoutMouseId )
  228. {
  229. enabledValue = value;
  230. }
  231. if( EditorGUI.EndChangeCheck() )
  232. {
  233. enableChanged = true;
  234. }
  235. EditorGUILayout.EndHorizontal();
  236. if( foldoutValue )
  237. {
  238. cachedColor = GUI.color;
  239. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  240. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  241. GUI.color = cachedColor;
  242. EditorGUILayout.Separator();
  243. EditorGUI.BeginDisabledGroup( !enabledValue );
  244. EditorGUI.indentLevel += 1;
  245. DrawSection();
  246. EditorGUI.indentLevel -= 1;
  247. EditorGUI.EndDisabledGroup();
  248. EditorGUILayout.Separator();
  249. EditorGUILayout.EndVertical();
  250. }
  251. return enableChanged;
  252. }
  253. }
  254. }