Skip to content

Commit

Permalink
dbrizov#133 some refactorings in the naming
Browse files Browse the repository at this point in the history
  • Loading branch information
niggo1243 committed Sep 6, 2021
1 parent 51ab783 commit b35d73c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 48 deletions.
34 changes: 11 additions & 23 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@

namespace NaughtyAttributes.Editor
{
public class NaughtyProperty
{
public SerializedProperty property;
public SpecialCaseDrawerAttribute specialCaseDrawerAttribute;
public ShowIfAttributeBase showIfAttribute;

public EnableIfAttributeBase enableIfAttribute;

public ReadOnlyAttribute readOnlyAttribute;

public ValidatorAttribute[] validatorAttributes;
}

[CanEditMultipleObjects]
[CustomEditor(typeof(UnityEngine.Object), true)]
public class NaughtyInspector : UnityEditor.Editor
Expand Down Expand Up @@ -55,12 +42,13 @@ protected virtual void OnEnable()

GetSerializedProperties(ref _serializedProperties);

_anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute<INaughtyAttribute>(p.property) != null);
_anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute<INaughtyAttribute>(p.serializedProperty) != null);

_nonGroupedSerializedProperty = GetNonGroupedProperties(_serializedProperties);
NaughtyProperty[] mScripts = _serializedProperties.Where(p => p.property.name.Equals("m_Script")).ToArray();

m_ScriptProperty = mScripts.Length > 0 ? mScripts[0].property : null;
//.First(...) doesnt work for some reason because the m_Script field isnt loaded yet I assume
NaughtyProperty[] mScripts = _serializedProperties.Where(p => p.serializedProperty.name.Equals("m_Script")).ToArray();
m_ScriptProperty = mScripts.Length > 0 ? mScripts[0].serializedProperty : null;

_groupedSerialzedProperty = GetGroupedProperties(_serializedProperties);

Expand Down Expand Up @@ -128,7 +116,7 @@ protected void DrawSerializedProperties()
// Draw grouped serialized properties
foreach (var group in _groupedSerialzedProperty)
{
IEnumerable<NaughtyProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.property));
IEnumerable<NaughtyProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.serializedProperty));
if (!visibleProperties.Any())
{
continue;
Expand All @@ -146,7 +134,7 @@ protected void DrawSerializedProperties()
// Draw foldout serialized properties
foreach (var group in _foldoutGroupedSerializedProperty)
{
IEnumerable<NaughtyProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.property));
IEnumerable<NaughtyProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.serializedProperty));
if (!visibleProperties.Any())
{
continue;
Expand Down Expand Up @@ -229,21 +217,21 @@ protected void DrawButtons(bool drawHeader = false)

private static IEnumerable<NaughtyProperty> GetNonGroupedProperties(IEnumerable<NaughtyProperty> properties)
{
return properties.Where(p => PropertyUtility.GetAttribute<IGroupAttribute>(p.property) == null && !p.property.name.Equals("m_Script"));
return properties.Where(p => PropertyUtility.GetAttribute<IGroupAttribute>(p.serializedProperty) == null && !p.serializedProperty.name.Equals("m_Script"));
}

private static IEnumerable<IGrouping<string, NaughtyProperty>> GetGroupedProperties(IEnumerable<NaughtyProperty> properties)
{
return properties
.Where(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p.property) != null)
.GroupBy(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p.property).Name);
.Where(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p.serializedProperty) != null)
.GroupBy(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p.serializedProperty).Name);
}

private static IEnumerable<IGrouping<string, NaughtyProperty>> GetFoldoutProperties(IEnumerable<NaughtyProperty> properties)
{
return properties
.Where(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p.property) != null)
.GroupBy(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p.property).Name);
.Where(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p.serializedProperty) != null)
.GroupBy(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p.serializedProperty).Name);
}

private static GUIStyle GetHeaderGUIStyle()
Expand Down
17 changes: 17 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using UnityEditor;

namespace NaughtyAttributes.Editor
{
public class NaughtyProperty
{
public SerializedProperty serializedProperty;
public SpecialCaseDrawerAttribute specialCaseDrawerAttribute;
public ShowIfAttributeBase showIfAttribute;

public EnableIfAttributeBase enableIfAttribute;

public ReadOnlyAttribute readOnlyAttribute;

public ValidatorAttribute[] validatorAttributes;
}
}
11 changes: 11 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,63 @@ public static class NaughtyEditorGUI

private static GUIStyle _buttonStyle = new GUIStyle(GUI.skin.button) { richText = true };

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

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

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

private static void DrawPropertyField(Rect rect, NaughtyProperty property, GUIContent label, bool includeChildren)
private static void DrawPropertyField(Rect rect, NaughtyProperty naughtyProperty, GUIContent label, bool includeChildren)
{
EditorGUI.PropertyField(rect, property.property, label, includeChildren);
EditorGUI.PropertyField(rect, naughtyProperty.serializedProperty, label, includeChildren);
}

private static void DrawPropertyField_Layout(Rect rect, NaughtyProperty property, GUIContent label, bool includeChildren)
private static void DrawPropertyField_Layout(Rect rect, NaughtyProperty naughtyProperty, GUIContent label, bool includeChildren)
{
EditorGUILayout.PropertyField(property.property, label, includeChildren);
EditorGUILayout.PropertyField(naughtyProperty.serializedProperty, label, includeChildren);
}

private static void PropertyField_Implementation(Rect rect, NaughtyProperty property, bool includeChildren, PropertyFieldFunction propertyFieldFunction)
private static void PropertyField_Implementation(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren, PropertyFieldFunction propertyFieldFunction)
{
if (property.specialCaseDrawerAttribute != null)
if (naughtyProperty.specialCaseDrawerAttribute != null)
{
property.specialCaseDrawerAttribute.GetDrawer().OnGUI(rect, property.property);
naughtyProperty.specialCaseDrawerAttribute.GetDrawer().OnGUI(rect, naughtyProperty.serializedProperty);
}
else
{
// Check if visible
bool visible = PropertyUtility.IsVisible(property.showIfAttribute, property.property);
bool visible = PropertyUtility.IsVisible(naughtyProperty.showIfAttribute, naughtyProperty.serializedProperty);
if (!visible)
{
return;
}

// Validate
foreach (var validatorAttribute in property.validatorAttributes)
foreach (var validatorAttribute in naughtyProperty.validatorAttributes)
{
validatorAttribute.GetValidator().ValidateProperty(property.property);
validatorAttribute.GetValidator().ValidateProperty(naughtyProperty.serializedProperty);
}

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

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

// Call OnValueChanged callbacks
if (EditorGUI.EndChangeCheck())
{
PropertyUtility.CallOnValueChangedCallbacks(property.property);
PropertyUtility.CallOnValueChangedCallbacks(naughtyProperty.serializedProperty);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ public static class PropertyUtility
return (T[])fieldInfo.GetCustomAttributes(typeof(T), true);
}

public static NaughtyProperty CreateNaughtyProperty(SerializedProperty property)
public static NaughtyProperty CreateNaughtyProperty(SerializedProperty serializedProperty)
{
NaughtyProperty naughtyProperty = new NaughtyProperty();
naughtyProperty.property = property;
naughtyProperty.serializedProperty = serializedProperty;

naughtyProperty.readOnlyAttribute = PropertyUtility.GetAttribute<ReadOnlyAttribute>(naughtyProperty.property);
naughtyProperty.enableIfAttribute = PropertyUtility.GetAttribute<EnableIfAttributeBase>(naughtyProperty.property);
naughtyProperty.readOnlyAttribute = PropertyUtility.GetAttribute<ReadOnlyAttribute>(naughtyProperty.serializedProperty);
naughtyProperty.enableIfAttribute = PropertyUtility.GetAttribute<EnableIfAttributeBase>(naughtyProperty.serializedProperty);

naughtyProperty.showIfAttribute = PropertyUtility.GetAttribute<ShowIfAttributeBase>(naughtyProperty.property);
naughtyProperty.validatorAttributes = PropertyUtility.GetAttributes<ValidatorAttribute>(naughtyProperty.property);
naughtyProperty.showIfAttribute = PropertyUtility.GetAttribute<ShowIfAttributeBase>(naughtyProperty.serializedProperty);
naughtyProperty.validatorAttributes = PropertyUtility.GetAttributes<ValidatorAttribute>(naughtyProperty.serializedProperty);

naughtyProperty.specialCaseDrawerAttribute =
PropertyUtility.GetAttribute<SpecialCaseDrawerAttribute>(naughtyProperty.property);
PropertyUtility.GetAttribute<SpecialCaseDrawerAttribute>(naughtyProperty.serializedProperty);

return naughtyProperty;
}
Expand Down

0 comments on commit b35d73c

Please sign in to comment.