Skip to content

Commit

Permalink
dbrizov#133 caching isVisible, isEnabled and special drawers
Browse files Browse the repository at this point in the history
  • Loading branch information
niggo1243 committed Sep 10, 2021
1 parent 4f5a6b1 commit 8eb3aa1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
50 changes: 44 additions & 6 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class NaughtyInspector : UnityEditor.Editor
private Dictionary<string, SavedBool> _foldouts = new Dictionary<string, SavedBool>();

private bool _anyNaughtyAttribute;
private bool _useCachedMetaAttributes;
private bool _changeDetected;

protected virtual void OnEnable()
{
Expand Down Expand Up @@ -80,10 +82,14 @@ public virtual void Prepare()
_groupedSerialzedProperty = GetGroupedProperties(_serializedProperties);

_foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties);

_useCachedMetaAttributes = false;
}

public override void OnInspectorGUI()
{
_changeDetected = false;

if (!_anyNaughtyAttribute)
{
DrawDefaultInspector();
Expand All @@ -96,6 +102,8 @@ public override void OnInspectorGUI()
DrawNonSerializedFields();
DrawNativeProperties();
DrawButtons();

_useCachedMetaAttributes = !_changeDetected;
}

protected virtual void GetSerializedProperties(ref List<NaughtyProperty> outSerializedProperties)
Expand Down Expand Up @@ -133,13 +141,33 @@ protected virtual void DrawSerializedProperties()
// Draw non-grouped serialized properties
foreach (var naughtyProperty in _nonGroupedSerializedProperty)
{
NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true);
if (!_useCachedMetaAttributes)
{
naughtyProperty.cachedIsVisible = PropertyUtility.IsVisible(naughtyProperty.showIfAttribute,
naughtyProperty.serializedProperty);

naughtyProperty.cachedIsEnabled = PropertyUtility.IsEnabled(naughtyProperty.readOnlyAttribute, naughtyProperty.enableIfAttribute,
naughtyProperty.serializedProperty);
}

_changeDetected |= NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true);
}

// Draw grouped serialized properties
foreach (var group in _groupedSerialzedProperty)
{
IEnumerable<NaughtyProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty));
IEnumerable<NaughtyProperty> visibleProperties =
_useCachedMetaAttributes
? group.Where(p => p.cachedIsVisible)
: group.Where(p =>
{
p.cachedIsEnabled = PropertyUtility.IsEnabled(p.readOnlyAttribute, p.enableIfAttribute,
p.serializedProperty);

return p.cachedIsVisible =
PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty);
});

if (!visibleProperties.Any())
{
continue;
Expand All @@ -148,16 +176,26 @@ protected virtual void DrawSerializedProperties()
NaughtyEditorGUI.BeginBoxGroup_Layout(group.Key);
foreach (var naughtyProperty in visibleProperties)
{
NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true);
_changeDetected |= NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true);
}

NaughtyEditorGUI.EndBoxGroup_Layout();
}

// Draw foldout serialized properties
foreach (var group in _foldoutGroupedSerializedProperty)
{
IEnumerable<NaughtyProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty));
IEnumerable<NaughtyProperty> visibleProperties =
_useCachedMetaAttributes
? group.Where(p => p.cachedIsVisible)
: group.Where(p =>
{
p.cachedIsEnabled = PropertyUtility.IsEnabled(p.readOnlyAttribute, p.enableIfAttribute,
p.serializedProperty);

return p.cachedIsVisible =
PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty);
});

if (!visibleProperties.Any())
{
continue;
Expand All @@ -173,7 +211,7 @@ protected virtual void DrawSerializedProperties()
{
foreach (var naughtyProperty in visibleProperties)
{
NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, true);
_changeDetected |= NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, true);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ public class NaughtyProperty
public EnableIfAttributeBase enableIfAttribute;
public ReadOnlyAttribute readOnlyAttribute;
public ValidatorAttribute[] validatorAttributes;

public bool cachedIsVisible = true;
public bool cachedIsEnabled = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,41 @@ namespace NaughtyAttributes.Editor
{
public abstract class SpecialCasePropertyDrawerBase
{
public void OnGUI(Rect rect, SerializedProperty property)
public bool OnGUI(Rect rect, NaughtyProperty naughtyProperty)
{
bool changeDetected = false;

// Check if visible
bool visible = PropertyUtility.IsVisible(property);
bool visible = PropertyUtility.IsVisible(naughtyProperty.showIfAttribute, naughtyProperty.serializedProperty);
if (!visible)
{
return;
return false;
}

// Validate
ValidatorAttribute[] validatorAttributes = PropertyUtility.GetAttributes<ValidatorAttribute>(property);
ValidatorAttribute[] validatorAttributes = naughtyProperty.validatorAttributes;
foreach (var validatorAttribute in validatorAttributes)
{
validatorAttribute.GetValidator().ValidateProperty(property);
validatorAttribute.GetValidator().ValidateProperty(naughtyProperty.serializedProperty);
}

// Check if enabled and draw
EditorGUI.BeginChangeCheck();
bool enabled = PropertyUtility.IsEnabled(property);
bool enabled = PropertyUtility.IsEnabled(naughtyProperty.readOnlyAttribute, naughtyProperty.enableIfAttribute, naughtyProperty.serializedProperty);

using (new EditorGUI.DisabledScope(disabled: !enabled))
{
OnGUI_Internal(rect, property, PropertyUtility.GetLabel(property));
OnGUI_Internal(rect, naughtyProperty.serializedProperty, PropertyUtility.GetLabel(naughtyProperty.labelAttribute, naughtyProperty.serializedProperty));
}

// Call OnValueChanged callbacks
if (EditorGUI.EndChangeCheck())
{
PropertyUtility.CallOnValueChangedCallbacks(property);
changeDetected = true;
PropertyUtility.CallOnValueChangedCallbacks(naughtyProperty.serializedProperty);
}

return changeDetected;
}

public float GetPropertyHeight(SerializedProperty property)
Expand Down
24 changes: 14 additions & 10 deletions Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public static class NaughtyEditorGUI

private delegate void PropertyFieldFunction(Rect rect, NaughtyProperty naughtyProperty, GUIContent label, bool includeChildren);

public static void PropertyField(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren)
public static bool PropertyField(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren)
{
PropertyField_Implementation(rect, naughtyProperty, includeChildren, DrawPropertyField);
return PropertyField_Implementation(rect, naughtyProperty, includeChildren, DrawPropertyField);
}

public static void PropertyField_Layout(NaughtyProperty naughtyProperty, bool includeChildren)
public static bool PropertyField_Layout(NaughtyProperty naughtyProperty, bool includeChildren)
{
Rect dummyRect = new Rect();
PropertyField_Implementation(dummyRect, naughtyProperty, includeChildren, DrawPropertyField_Layout);
return PropertyField_Implementation(dummyRect, naughtyProperty, includeChildren, DrawPropertyField_Layout);
}

private static void DrawPropertyField(Rect rect, NaughtyProperty naughtyProperty, GUIContent label, bool includeChildren)
Expand All @@ -40,19 +40,20 @@ private static void DrawPropertyField_Layout(Rect rect, NaughtyProperty naughtyP
EditorGUILayout.PropertyField(naughtyProperty.serializedProperty, label, includeChildren);
}

private static void PropertyField_Implementation(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren, PropertyFieldFunction propertyFieldFunction)
private static bool PropertyField_Implementation(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren, PropertyFieldFunction propertyFieldFunction)
{
bool changeDetected = false;

if (naughtyProperty.specialCaseDrawerAttribute != null)
{
naughtyProperty.specialCaseDrawerAttribute.GetDrawer().OnGUI(rect, naughtyProperty.serializedProperty);
return naughtyProperty.specialCaseDrawerAttribute.GetDrawer().OnGUI(rect, naughtyProperty);
}
else
{
// Check if visible
bool visible = PropertyUtility.IsVisible(naughtyProperty.showIfAttribute, naughtyProperty.serializedProperty);
if (!visible)
if (!naughtyProperty.cachedIsVisible)
{
return;
return false;
}

// Validate
Expand All @@ -63,7 +64,7 @@ private static void PropertyField_Implementation(Rect rect, NaughtyProperty naug

// Check if enabled and draw
EditorGUI.BeginChangeCheck();
bool enabled = PropertyUtility.IsEnabled(naughtyProperty.readOnlyAttribute, naughtyProperty.enableIfAttribute, naughtyProperty.serializedProperty);
bool enabled = naughtyProperty.cachedIsEnabled;

using (new EditorGUI.DisabledScope(disabled: !enabled))
{
Expand All @@ -73,9 +74,12 @@ private static void PropertyField_Implementation(Rect rect, NaughtyProperty naug
// Call OnValueChanged callbacks
if (EditorGUI.EndChangeCheck())
{
changeDetected = true;
PropertyUtility.CallOnValueChangedCallbacks(naughtyProperty.serializedProperty);
}
}

return changeDetected;
}

public static float GetIndentLength(Rect sourceRect)
Expand Down

0 comments on commit 8eb3aa1

Please sign in to comment.