-
Notifications
You must be signed in to change notification settings - Fork 871
Expand file tree
/
Copy pathDefaultVolumeProfileEditor.cs
More file actions
254 lines (211 loc) · 10.1 KB
/
DefaultVolumeProfileEditor.cs
File metadata and controls
254 lines (211 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
namespace UnityEditor.Rendering
{
/// <summary>
/// Editor for the Default Volume Profile
/// </summary>
public sealed partial class DefaultVolumeProfileEditor
{
const string k_TemplatePath = "Packages/com.unity.render-pipelines.core/Editor/UXML/DefaultVolumeProfileEditor.uxml";
const int k_ContainerMarginLeft = 27;
const int k_ImguiContainerPaddingLeft = 18;
static Lazy<GUIStyle> s_ImguiContainerScopeStyle = new(() => new GUIStyle
{
padding = new RectOffset(k_ImguiContainerPaddingLeft, 0, 0, 0)
});
static Lazy<VisualTreeAsset> s_Template = new(() => AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(k_TemplatePath));
readonly Dictionary<VolumeComponentEditor, string> m_VolumeComponentHelpUrls = new();
readonly VolumeProfile m_Profile;
readonly SerializedObject m_TargetSerializedObject;
readonly Editor m_BaseEditor;
DefaultVolumeProfileCategories m_Categories;
VisualElement m_Root;
ToolbarSearchField m_SearchField;
/// <summary>
/// Constructor
/// </summary>
/// <param name="profile">VolumeProfile to display</param>
/// <param name="targetSerializedObject">Target serialized object to update when the default volume profile changes</param>
public DefaultVolumeProfileEditor(VolumeProfile profile, SerializedObject targetSerializedObject)
{
m_Profile = profile;
m_TargetSerializedObject = targetSerializedObject;
}
/// <summary>
/// List of all VolumeComponentEditors
/// </summary>
public List<VolumeComponentEditor> allEditors
{
get
{
var editors = new List<VolumeComponentEditor>();
foreach (var (_, categoryEditors) in m_Categories.categories)
{
editors.AddRange(categoryEditors);
}
return editors;
}
}
/// <summary>
/// Create the visual hierarchy
/// </summary>
/// <returns>Root element of the visual hierarchy</returns>
public VisualElement Create()
{
m_Root = s_Template.Value.Instantiate();
m_SearchField = m_Root.Q<ToolbarSearchField>();
m_SearchField.RegisterValueChangedCallback(_ => CreateComponentLists());
m_Categories = new DefaultVolumeProfileCategories(m_Profile);
CreateComponentLists();
Undo.undoRedoPerformed += OnUndoRedoPerformed;
return m_Root;
}
void CreateComponentLists()
{
var componentListElement = m_Root.Q("component-list");
componentListElement.Clear();
var searchString = m_SearchField.value;
bool MatchesSearchString(string title)
{
return searchString.Length == 0 || title.Contains(searchString, StringComparison.OrdinalIgnoreCase);
}
foreach (var (categoryName, categoryEditors) in m_Categories.categories)
{
List<VolumeComponentEditor> filteredCategoryEditors = new();
foreach (var category in categoryEditors)
{
if (MatchesSearchString(category.GetDisplayTitle().text))
filteredCategoryEditors.Add(category);
}
if (filteredCategoryEditors.Count == 0)
continue;
VisualElement categoryTitleLabel = new Label(categoryName);
categoryTitleLabel.AddToClassList("category-header");
componentListElement.Add(categoryTitleLabel);
Func<VisualElement> makeItem = () =>
{
var container = new IMGUIContainer();
container.cullingEnabled = true;
return container;
};
Action<VisualElement, int> bindItem = (e, i) =>
{
(e as IMGUIContainer).onGUIHandler = () =>
{
using var indentScope = new SettingsProviderGUIScope();
/* values adapted to the ProjectSettings > Graphics */
/* they also works in the DefaultVolume inspector */
var minWidth = 120;
var indent = 66;
var ratio = 0.45f;
EditorGUIUtility.labelWidth = Mathf.Max(minWidth, (int)((e.worldBound.width - indent) * ratio));
VolumeComponentEditorOnGUI(filteredCategoryEditors[i]);
};
};
ListView listView = new ListView(filteredCategoryEditors, -1, makeItem, bindItem);
listView.selectionType = SelectionType.None;
listView.reorderable = false;
listView.virtualizationMethod = CollectionVirtualizationMethod.DynamicHeight;
componentListElement.Add(listView);
foreach (var editor in filteredCategoryEditors)
{
DocumentationUtils.TryGetHelpURL(editor.volumeComponent.GetType(), out string helpUrl);
helpUrl ??= string.Empty;
m_VolumeComponentHelpUrls[editor] = helpUrl;
}
}
}
void OnUndoRedoPerformed()
{
VolumeManager.instance.OnVolumeProfileChanged(m_Profile);
}
/// <summary>
/// Destroy all Editors owned by this class
/// </summary>
public void Destroy()
{
m_Categories.Destroy();
Undo.undoRedoPerformed -= OnUndoRedoPerformed;
}
/// <summary>
/// Rebuild all ListViews to force them to update their expanded state.
/// </summary>
public void RebuildListViews()
{
// Rebuild is necessary to avoid gradual "animation" effect when collapsing/expanding many foldouts at once.
// This happens because the items have cullingEnabled=true and are not updated until they come to view.
m_Root.Query<ListView>().ForEach(l => l.Rebuild());
}
void VolumeComponentEditorOnGUI(VolumeComponentEditor editor)
{
using (new EditorGUILayout.VerticalScope(s_ImguiContainerScopeStyle.Value))
{
using var changedScope = new EditorGUI.ChangeCheckScope();
CoreEditorUtils.DrawSplitter();
bool displayContent = CoreEditorUtils.DrawHeaderToggleFoldout(
new GUIContent(editor.GetDisplayTitle()),
editor.expanded,
null,
pos => OnVolumeComponentContextClick(pos, editor),
editor.hasAdditionalProperties ? () => editor.showAdditionalProperties : null,
() => editor.showAdditionalProperties ^= true,
m_VolumeComponentHelpUrls[editor]
);
if (displayContent ^ editor.expanded)
editor.expanded = displayContent;
if (editor.expanded)
{
using var scope = new EditorGUILayout.VerticalScope();
// This rect is drawn to suppress mouse hover highlight in order to match the old imgui
// implementation. Not doing this causes visual bugs with AdditionalProperties animations.
var highlightSuppressRect = scope.rect;
highlightSuppressRect.xMin -= k_ImguiContainerPaddingLeft+k_ContainerMarginLeft;
EditorGUI.DrawRect(highlightSuppressRect, CoreEditorStyles.backgroundColor);
editor.OnInternalInspectorGUI();
}
if (changedScope.changed)
{
m_TargetSerializedObject.ApplyModifiedProperties();
VolumeManager.instance.OnVolumeProfileChanged(m_Profile);
}
}
}
void OnVolumeComponentContextClick(Vector2 position, VolumeComponentEditor targetEditor)
{
var targetComponent = targetEditor.volumeComponent;
var menu = new GenericMenu();
menu.AddItem(VolumeProfileUtils.Styles.collapseAll, false, () =>
{
VolumeProfileUtils.SetComponentEditorsExpanded(allEditors, false);
RebuildListViews();
});
menu.AddItem(VolumeProfileUtils.Styles.expandAll, false, () =>
{
VolumeProfileUtils.SetComponentEditorsExpanded(allEditors, true);
RebuildListViews();
});
menu.AddSeparator(string.Empty);
menu.AddItem(VolumeProfileUtils.Styles.reset, false, () =>
{
VolumeProfileUtils.ResetComponentsInternal(targetEditor.serializedObject, m_Profile, new[] { targetComponent }, true);
});
menu.AddSeparator(string.Empty);
if (targetEditor.hasAdditionalProperties)
menu.AddAdvancedPropertiesBoolMenuItem(() => targetEditor.showAdditionalProperties, () => targetEditor.showAdditionalProperties ^= true);
menu.AddSeparator(string.Empty);
menu.AddItem(VolumeProfileUtils.Styles.openInRenderingDebugger, false, DebugDisplaySettingsVolume.OpenInRenderingDebugger);
menu.AddSeparator(string.Empty);
menu.AddItem(VolumeProfileUtils.Styles.copySettings, false, () => VolumeComponentCopyPaste.CopySettings(targetComponent));
if (VolumeComponentCopyPaste.CanPaste(targetComponent))
menu.AddItem(VolumeProfileUtils.Styles.pasteSettings, false, () => VolumeComponentCopyPaste.PasteSettings(targetComponent));
else
menu.AddDisabledItem(VolumeProfileUtils.Styles.pasteSettings);
menu.DropDown(new Rect(position, Vector2.zero));
}
}
}