A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3
4namespace UnityEditor.Rendering
5{
6 /// <summary>
7 /// Create a toggleable header for material UI, must be used within a scope.
8 /// </summary>
9 /// <example>
10 /// <code>
11 /// void OnGUI()
12 /// {
13 /// using (var header = new MaterialHeaderScope(text, ExpandBit, editor))
14 /// {
15 /// if (header.expanded)
16 /// EditorGUILayout.LabelField("Hello World !");
17 /// }
18 /// }
19 /// </code>
20 /// </example>
21 public struct MaterialHeaderScope : IDisposable
22 {
23 /// <summary>Indicates whether the header is expanded or not. Is true if the header is expanded, false otherwise.</summary>
24 public readonly bool expanded;
25 bool spaceAtEnd;
26#if !UNITY_2020_1_OR_NEWER
27 int oldIndentLevel;
28#endif
29
30 /// <summary>
31 /// Creates a material header scope to display the foldout in the material UI.
32 /// </summary>
33 /// <param name="title">GUI Content of the header.</param>
34 /// <param name="bitExpanded">Bit index which specifies the state of the header (whether it is open or collapsed) inside Editor Prefs.</param>
35 /// <param name="materialEditor">The current material editor.</param>
36 /// <param name="spaceAtEnd">Set this to true to make the block include space at the bottom of its UI. Set to false to not include any space.</param>
37 /// <param name="subHeader">Set to true to make this into a sub-header. This affects the style of the header. Set to false to make this use the standard style.</param>
38 /// <param name="defaultExpandedState">The default state if the header is not present</param>
39 /// <param name="documentationURL">[optional] Documentation page</param>
40 public MaterialHeaderScope(GUIContent title, uint bitExpanded, MaterialEditor materialEditor, bool spaceAtEnd = true, bool subHeader = false, uint defaultExpandedState = uint.MaxValue, string documentationURL = "")
41 {
42 if (title == null)
43 throw new ArgumentNullException(nameof(title));
44
45 bool beforeExpanded = materialEditor.IsAreaExpanded(bitExpanded, defaultExpandedState);
46
47#if !UNITY_2020_1_OR_NEWER
48 oldIndentLevel = EditorGUI.indentLevel;
49 EditorGUI.indentLevel = subHeader ? 1 : 0; //fix for preset in 2019.3 (preset are one more indentation depth in material)
50#endif
51
52 this.spaceAtEnd = spaceAtEnd;
53 if (!subHeader)
54 CoreEditorUtils.DrawSplitter();
55 GUILayout.BeginVertical();
56
57 bool saveChangeState = GUI.changed;
58 expanded = subHeader
59 ? CoreEditorUtils.DrawSubHeaderFoldout(title, beforeExpanded, isBoxed: false)
60 : CoreEditorUtils.DrawHeaderFoldout(title, beforeExpanded, documentationURL: documentationURL);
61 if (expanded ^ beforeExpanded)
62 {
63 materialEditor.SetIsAreaExpanded((uint)bitExpanded, expanded);
64 saveChangeState = true;
65 }
66 GUI.changed = saveChangeState;
67 }
68
69 /// <summary>
70 /// Creates a material header scope to display the foldout in the material UI.
71 /// </summary>
72 /// <param name="title">Title of the header.</param>
73 /// <param name="bitExpanded">Bit index which specifies the state of the header (whether it is open or collapsed) inside Editor Prefs.</param>
74 /// <param name="materialEditor">The current material editor.</param>
75 /// <param name="spaceAtEnd">Set this to true to make the block include space at the bottom of its UI. Set to false to not include any space.</param>
76 /// <param name="subHeader">Set to true to make this into a sub-header. This affects the style of the header. Set to false to make this use the standard style.</param>
77 public MaterialHeaderScope(string title, uint bitExpanded, MaterialEditor materialEditor, bool spaceAtEnd = true, bool subHeader = false)
78 : this(EditorGUIUtility.TrTextContent(title, string.Empty), bitExpanded, materialEditor, spaceAtEnd, subHeader)
79 {
80 }
81
82 /// <summary>Disposes of the material scope header and cleans up any resources it used.</summary>
83 void IDisposable.Dispose()
84 {
85 if (expanded && spaceAtEnd && (Event.current.type == EventType.Repaint || Event.current.type == EventType.Layout))
86 EditorGUILayout.Space();
87
88#if !UNITY_2020_1_OR_NEWER
89 EditorGUI.indentLevel = oldIndentLevel;
90#endif
91 GUILayout.EndVertical();
92 }
93 }
94}