EditorGUIHelper.cs
7.79 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
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using UnityEngine.PostProcessing;
namespace UnityEditor.PostProcessing
{
public static class EditorGUIHelper
{
static EditorGUIHelper()
{
s_GUIContentCache = new Dictionary<string, GUIContent>();
}
#region GUIContent caching
static Dictionary<string, GUIContent> s_GUIContentCache;
public static GUIContent GetContent(string textAndTooltip)
{
if (string.IsNullOrEmpty(textAndTooltip))
return GUIContent.none;
GUIContent content;
if (!s_GUIContentCache.TryGetValue(textAndTooltip, out content))
{
var s = textAndTooltip.Split('|');
content = new GUIContent(s[0]);
if (s.Length > 1 && !string.IsNullOrEmpty(s[1]))
content.tooltip = s[1];
s_GUIContentCache.Add(textAndTooltip, content);
}
return content;
}
#endregion
public static bool Header(string title, SerializedProperty group, Action resetAction)
{
var rect = GUILayoutUtility.GetRect(16f, 22f, FxStyles.header);
GUI.Box(rect, title, FxStyles.header);
var display = group == null || group.isExpanded;
var foldoutRect = new Rect(rect.x + 4f, rect.y + 2f, 13f, 13f);
var e = Event.current;
var popupRect = new Rect(rect.x + rect.width - FxStyles.paneOptionsIcon.width - 5f, rect.y + FxStyles.paneOptionsIcon.height / 2f + 1f, FxStyles.paneOptionsIcon.width, FxStyles.paneOptionsIcon.height);
GUI.DrawTexture(popupRect, FxStyles.paneOptionsIcon);
if (e.type == EventType.Repaint)
FxStyles.headerFoldout.Draw(foldoutRect, false, false, display, false);
if (e.type == EventType.MouseDown)
{
if (popupRect.Contains(e.mousePosition))
{
var popup = new GenericMenu();
popup.AddItem(GetContent("Reset"), false, () => resetAction());
popup.AddSeparator(string.Empty);
popup.AddItem(GetContent("Copy Settings"), false, () => CopySettings(group));
if (CanPaste(group))
popup.AddItem(GetContent("Paste Settings"), false, () => PasteSettings(group));
else
popup.AddDisabledItem(GetContent("Paste Settings"));
popup.ShowAsContext();
}
else if (rect.Contains(e.mousePosition) && group != null)
{
display = !display;
if (group != null)
group.isExpanded = !group.isExpanded;
e.Use();
}
}
return display;
}
public static bool Header(string title, SerializedProperty group, SerializedProperty enabledField, Action resetAction)
{
var field = ReflectionUtils.GetFieldInfoFromPath(enabledField.serializedObject.targetObject, enabledField.propertyPath);
object parent = null;
PropertyInfo prop = null;
if (field != null && field.IsDefined(typeof(GetSetAttribute), false))
{
var attr = (GetSetAttribute)field.GetCustomAttributes(typeof(GetSetAttribute), false)[0];
parent = ReflectionUtils.GetParentObject(enabledField.propertyPath, enabledField.serializedObject.targetObject);
prop = parent.GetType().GetProperty(attr.name);
}
var display = group == null || group.isExpanded;
var enabled = enabledField.boolValue;
var rect = GUILayoutUtility.GetRect(16f, 22f, FxStyles.header);
GUI.Box(rect, title, FxStyles.header);
var toggleRect = new Rect(rect.x + 4f, rect.y + 4f, 13f, 13f);
var e = Event.current;
var popupRect = new Rect(rect.x + rect.width - FxStyles.paneOptionsIcon.width - 5f, rect.y + FxStyles.paneOptionsIcon.height / 2f + 1f, FxStyles.paneOptionsIcon.width, FxStyles.paneOptionsIcon.height);
GUI.DrawTexture(popupRect, FxStyles.paneOptionsIcon);
if (e.type == EventType.Repaint)
FxStyles.headerCheckbox.Draw(toggleRect, false, false, enabled, false);
if (e.type == EventType.MouseDown)
{
const float kOffset = 2f;
toggleRect.x -= kOffset;
toggleRect.y -= kOffset;
toggleRect.width += kOffset * 2f;
toggleRect.height += kOffset * 2f;
if (toggleRect.Contains(e.mousePosition))
{
enabledField.boolValue = !enabledField.boolValue;
if (prop != null)
prop.SetValue(parent, enabledField.boolValue, null);
e.Use();
}
else if (popupRect.Contains(e.mousePosition))
{
var popup = new GenericMenu();
popup.AddItem(GetContent("Reset"), false, () => resetAction());
popup.AddSeparator(string.Empty);
popup.AddItem(GetContent("Copy Settings"), false, () => CopySettings(group));
if (CanPaste(group))
popup.AddItem(GetContent("Paste Settings"), false, () => PasteSettings(group));
else
popup.AddDisabledItem(GetContent("Paste Settings"));
popup.ShowAsContext();
}
else if (rect.Contains(e.mousePosition) && group != null)
{
display = !display;
group.isExpanded = !group.isExpanded;
e.Use();
}
}
return display;
}
static void CopySettings(SerializedProperty settings)
{
var t = typeof(PostProcessingProfile);
var settingsStruct = ReflectionUtils.GetFieldValueFromPath(settings.serializedObject.targetObject, ref t, settings.propertyPath);
var serializedString = t.ToString() + '|' + JsonUtility.ToJson(settingsStruct);
EditorGUIUtility.systemCopyBuffer = serializedString;
}
static bool CanPaste(SerializedProperty settings)
{
var data = EditorGUIUtility.systemCopyBuffer;
if (string.IsNullOrEmpty(data))
return false;
var parts = data.Split('|');
if (string.IsNullOrEmpty(parts[0]))
return false;
var field = ReflectionUtils.GetFieldInfoFromPath(settings.serializedObject.targetObject, settings.propertyPath);
return parts[0] == field.FieldType.ToString();
}
static void PasteSettings(SerializedProperty settings)
{
Undo.RecordObject(settings.serializedObject.targetObject, "Paste effect settings");
var field = ReflectionUtils.GetFieldInfoFromPath(settings.serializedObject.targetObject, settings.propertyPath);
var json = EditorGUIUtility.systemCopyBuffer.Substring(field.FieldType.ToString().Length + 1);
var obj = JsonUtility.FromJson(json, field.FieldType);
var parent = ReflectionUtils.GetParentObject(settings.propertyPath, settings.serializedObject.targetObject);
field.SetValue(parent, obj, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, CultureInfo.CurrentCulture);
}
}
}