-
Notifications
You must be signed in to change notification settings - Fork 156
Fixed bugs in PostNormalizedVectorDrawer #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| using System; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using UnityEditor; | ||
| using UnityEngine; | ||
| using UnityObject = UnityEngine.Object; | ||
|
|
@@ -17,84 +18,86 @@ static class Content | |
|
|
||
| class VectorPropertyGUIData | ||
| { | ||
| const int k_MaxElements = 4; | ||
|
|
||
| public readonly bool Valid; | ||
|
|
||
| // parent property | ||
| readonly SerializedProperty m_VectorProperty; | ||
| // per child property relative path; value is null if there are multiple different values | ||
| readonly KeyValuePair<string, double?>[] m_PreNormalizedValues = new KeyValuePair<string, double?>[4]; | ||
| // relative paths of element child properties | ||
| readonly IReadOnlyList<string> m_ElementPaths; | ||
| // the number of element child properties | ||
| readonly int m_NumElements; | ||
| // per child property; value is null if there are multiple different values | ||
| readonly double?[] m_PreNormalizedValues; | ||
| // per target; used to revert actual values for each object after displaying pre-normalized values | ||
| readonly Dictionary<SerializedObject, double4> m_PostNormalizedValues = new Dictionary<SerializedObject, double4>(); | ||
| readonly Dictionary<SerializedProperty, double4> m_PostNormalizedValues = new Dictionary<SerializedProperty, double4>(); | ||
|
|
||
| public VectorPropertyGUIData(SerializedProperty property) | ||
| { | ||
| m_VectorProperty = property; | ||
| if (Valid = TryUpdatePreNormalizedValues()) | ||
| UpdatePostNormalizedValues(); | ||
| } | ||
|
|
||
| bool TryUpdatePreNormalizedValues() | ||
| { | ||
| var iterator = m_VectorProperty.Copy(); | ||
| var parentPath = m_VectorProperty.propertyPath; | ||
| var i = 0; | ||
| var elementPaths = new List<string>(k_MaxElements); | ||
| var iterator = m_VectorProperty.Copy(); | ||
| while (iterator.Next(true) && iterator.propertyPath.StartsWith(parentPath)) | ||
| { | ||
| if (i >= m_PreNormalizedValues.Length || iterator.propertyType != SerializedPropertyType.Float) | ||
| return false; | ||
| m_PreNormalizedValues[i] = new KeyValuePair<string, double?>( | ||
| iterator.propertyPath.Substring(parentPath.Length + 1), | ||
| iterator.hasMultipleDifferentValues ? (double?)null : iterator.doubleValue | ||
| ); | ||
| ++i; | ||
| if (i >= k_MaxElements || iterator.propertyType != SerializedPropertyType.Float) | ||
| return; | ||
| elementPaths.Add(iterator.propertyPath.Substring(parentPath.Length + 1)); | ||
| i++; | ||
| } | ||
| return true; | ||
|
|
||
| Valid = true; | ||
| m_NumElements = elementPaths.Count; | ||
| m_ElementPaths = elementPaths; | ||
| m_PreNormalizedValues = elementPaths.Select(p => (double?)null).ToArray(); | ||
|
|
||
| UpdatePreNormalizedValues(); | ||
| UpdatePostNormalizedValues(); | ||
| } | ||
|
|
||
| void UpdatePostNormalizedValues() | ||
| { | ||
| m_PostNormalizedValues.Clear(); | ||
| foreach (var target in m_VectorProperty.serializedObject.targetObjects) | ||
| { | ||
| var postNormalizedValue = new double4(); | ||
| var parentProperty = new SerializedObject(target).FindProperty(m_VectorProperty.propertyPath); | ||
| for (var i = 0; i < 4; ++i) | ||
| { | ||
| if (string.IsNullOrEmpty(m_PreNormalizedValues[i].Key)) | ||
| break; | ||
| postNormalizedValue[i] = | ||
| parentProperty.FindPropertyRelative(m_PreNormalizedValues[i].Key).doubleValue; | ||
| } | ||
| m_PostNormalizedValues[parentProperty.serializedObject] = postNormalizedValue; | ||
| for (var i = 0; i < m_NumElements; ++i) | ||
| postNormalizedValue[i] = parentProperty.FindPropertyRelative(m_ElementPaths[i]).doubleValue; | ||
| m_PostNormalizedValues[parentProperty] = postNormalizedValue; | ||
| } | ||
| } | ||
|
|
||
| public void UpdatePreNormalizedValues() | ||
| { | ||
| TryUpdatePreNormalizedValues(); | ||
| for (var i = 0; i < m_NumElements; ++i) | ||
| { | ||
| var p = m_VectorProperty.FindPropertyRelative(m_ElementPaths[i]); | ||
| m_PreNormalizedValues[i] = p.hasMultipleDifferentValues ? (double?)null : p.doubleValue; | ||
| } | ||
| } | ||
|
|
||
| public void ApplyPreNormalizedValues() | ||
| { | ||
| m_VectorProperty.serializedObject.ApplyModifiedProperties(); | ||
| foreach (var rawValue in m_PreNormalizedValues) | ||
| for (var i = 0; i < m_NumElements; ++i) | ||
| { | ||
| if (rawValue.Value != null) | ||
| m_VectorProperty.FindPropertyRelative(rawValue.Key).doubleValue = rawValue.Value.Value; | ||
| if (m_PreNormalizedValues[i] != null) | ||
| m_VectorProperty.FindPropertyRelative(m_ElementPaths[i]).doubleValue = m_PreNormalizedValues[i].Value; | ||
| } | ||
| } | ||
|
|
||
| public void UnapplyPreNormalizedValues() | ||
| { | ||
| foreach (var target in m_PostNormalizedValues) | ||
| { | ||
| target.Key.Update(); | ||
| var sp = target.Key.FindProperty(m_VectorProperty.propertyPath); | ||
| for (var i = 0; i < 4; ++i) | ||
| target.Key.serializedObject.Update(); | ||
| for (var i = 0; i < m_NumElements; ++i) | ||
| { | ||
| if (string.IsNullOrEmpty(m_PreNormalizedValues[i].Key)) | ||
| break; | ||
| sp.FindPropertyRelative(m_PreNormalizedValues[i].Key).doubleValue = target.Value[i]; | ||
| target.Key.ApplyModifiedProperties(); | ||
| target.Key.FindPropertyRelative(m_ElementPaths[i]).doubleValue = target.Value[i]; | ||
| target.Key.serializedObject.ApplyModifiedProperties(); | ||
| } | ||
| } | ||
| m_VectorProperty.serializedObject.Update(); | ||
|
|
@@ -103,28 +106,38 @@ public void UnapplyPreNormalizedValues() | |
| public void PostNormalize(Func<double4, double4> normalize) | ||
| { | ||
| m_VectorProperty.serializedObject.ApplyModifiedProperties(); | ||
| foreach (var target in m_VectorProperty.serializedObject.targetObjects) | ||
| foreach (var target in m_PostNormalizedValues) | ||
| { | ||
| target.Key.serializedObject.Update(); | ||
| var postNormalizedValue = new double4(); | ||
| var sp = new SerializedObject(target).FindProperty(m_VectorProperty.propertyPath); | ||
| for (var i = 0; i < 4; ++i) | ||
| { | ||
| if (string.IsNullOrEmpty(m_PreNormalizedValues[i].Key)) | ||
| break; | ||
| postNormalizedValue[i] = sp.FindPropertyRelative(m_PreNormalizedValues[i].Key).doubleValue; | ||
| } | ||
| postNormalizedValue = normalize(postNormalizedValue); | ||
| for (var i = 0; i < 4; ++i) | ||
| { | ||
| if (string.IsNullOrEmpty(m_PreNormalizedValues[i].Key)) | ||
| break; | ||
| sp.FindPropertyRelative(m_PreNormalizedValues[i].Key).doubleValue = postNormalizedValue[i]; | ||
| } | ||
| sp.serializedObject.ApplyModifiedProperties(); | ||
| for (var i = 0; i < m_NumElements; ++i) | ||
| postNormalizedValue[i] = target.Key.FindPropertyRelative(m_ElementPaths[i]).doubleValue; | ||
| postNormalizedValue = normalize(normalize(postNormalizedValue)); | ||
| for (var i = 0; i < m_NumElements; ++i) | ||
| target.Key.FindPropertyRelative(m_ElementPaths[i]).doubleValue = postNormalizedValue[i]; | ||
| target.Key.serializedObject.ApplyModifiedProperties(); | ||
| } | ||
| UpdatePostNormalizedValues(); | ||
| m_VectorProperty.serializedObject.Update(); | ||
| } | ||
|
|
||
| public void RebuildIfDirty() | ||
| { | ||
| foreach (var target in m_PostNormalizedValues) | ||
| { | ||
| target.Key.serializedObject.Update(); | ||
| for (var i = 0; i < m_NumElements; ++i) | ||
| { | ||
| var serialized = target.Key.FindPropertyRelative(m_ElementPaths[i]).doubleValue; | ||
| if (target.Value[i] != serialized) | ||
| { | ||
| UpdatePreNormalizedValues(); | ||
| UpdatePostNormalizedValues(); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Dictionary<string, VectorPropertyGUIData> m_GUIDataPerPropertyPath = new Dictionary<string, VectorPropertyGUIData>(); | ||
|
|
@@ -139,20 +152,25 @@ protected virtual double4 Normalize(double4 value) | |
| return math.normalizesafe(value); | ||
| } | ||
|
|
||
| public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | ||
| VectorPropertyGUIData GetGUIData(SerializedProperty property) | ||
| { | ||
| VectorPropertyGUIData guiData; | ||
| if (!m_GUIDataPerPropertyPath.TryGetValue(property.propertyPath, out guiData)) | ||
| { | ||
| guiData = new VectorPropertyGUIData(GetVectorProperty(property)); | ||
| m_GUIDataPerPropertyPath[property.propertyPath] = guiData; | ||
| } | ||
| return guiData.Valid ? base.GetPropertyHeight(property, label) : EditorGUIUtility.singleLineHeight; | ||
| return guiData; | ||
| } | ||
|
|
||
| public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | ||
| { | ||
| return GetGUIData(property).Valid ? base.GetPropertyHeight(property, label) : EditorGUIUtility.singleLineHeight; | ||
| } | ||
|
|
||
| public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | ||
| { | ||
| var guiData = m_GUIDataPerPropertyPath[property.propertyPath]; | ||
| var guiData = GetGUIData(property); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This call ensures drawing the property works if GetPropertyHeight() has not been called yet |
||
| if (!guiData.Valid) | ||
| { | ||
| EditorGUI.HelpBox( | ||
|
|
@@ -166,6 +184,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten | |
| if (string.IsNullOrEmpty(label.tooltip)) | ||
| label.tooltip = Content.tooltip; | ||
|
|
||
| guiData.RebuildIfDirty(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method was added to solve the problem of serialized data getting updated from a different context, and then being overwritten by the pre-normalized data cached by the drawer |
||
| guiData.ApplyPreNormalizedValues(); | ||
| EditorGUI.BeginChangeCheck(); | ||
| base.OnGUI(position, property, label); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double normalization fixes a problem where data were sometimes normalized with values slightly less than 1