Skip to content
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

Added optional Foldout feature to BoxGroup #328

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions Assets/NaughtyAttributes/README.html
Original file line number Diff line number Diff line change
Expand Up @@ -1403,20 +1403,20 @@ <h2>
<p>Give the fields meta data. A field can have more than one meta attributes.</p>
<h3>
<a id="user-content-boxgroup" class="anchor" href="#boxgroup" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>BoxGroup</h3>
<p>Surrounds grouped fields with a box.</p>
<p>Surrounds grouped fields with a box and optionally a foldout.</p>
<div class="highlight highlight-source-cs"><pre><span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">NaughtyComponent</span> : <span class="pl-en">MonoBehaviour</span>
{
[<span class="pl-en">BoxGroup</span>(<span class="pl-s"><span class="pl-pds">"</span>Integers<span class="pl-pds">"</span></span>)]
<span class="pl-k">public</span> <span class="pl-k">int</span> <span class="pl-smi">firstInt</span>;
[<span class="pl-en">BoxGroup</span>(<span class="pl-s"><span class="pl-pds">"</span>Integers<span class="pl-pds">"</span></span>)]
<span class="pl-k">public</span> <span class="pl-k">int</span> <span class="pl-smi">secondInt</span>;

[<span class="pl-en">BoxGroup</span>(<span class="pl-s"><span class="pl-pds">"</span>Floats<span class="pl-pds">"</span></span>)]
[<span class="pl-en">BoxGroup</span>(<span class="pl-s"><span class="pl-pds">"</span>Floats<span class="pl-pds">"</span> <span class="pl-c1">true</span></span>)]
<span class="pl-k">public</span> <span class="pl-k">float</span> <span class="pl-smi">firstFloat</span>;
[<span class="pl-en">BoxGroup</span>(<span class="pl-s"><span class="pl-pds">"</span>Floats<span class="pl-pds">"</span></span>)]
[<span class="pl-en">BoxGroup</span>(<span class="pl-s"><span class="pl-pds">"</span>Floats<span class="pl-pds">"</span> <span class="pl-c1">true</span></span>)]
<span class="pl-k">public</span> <span class="pl-k">float</span> <span class="pl-smi">secondFloat</span>;
}</pre></div>
<p><a href="https://github.com/dbrizov/NaughtyAttributes/blob/master/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.png" target="_blank" rel="noopener noreferrer"><img src="https://github.com/dbrizov/NaughtyAttributes/raw/master/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.png" alt="inspector" style="max-width:100%;"></a></p>
<p><a href="https://github.com/dbrizov/NaughtyAttributes/blob/master/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.gif" target="_blank" rel="noopener noreferrer"><img src="https://github.com/dbrizov/NaughtyAttributes/raw/master/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.gif" alt="inspector" style="max-width:100%;"></a></p>
<h3>
<a id="user-content-foldout" class="anchor" href="#foldout" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Foldout</h3>
<p>Makes a foldout group.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ namespace NaughtyAttributes
public class BoxGroupAttribute : MetaAttribute, IGroupAttribute
{
public string Name { get; private set; }
public bool Foldable { get; private set; }

public BoxGroupAttribute(string name = "")
public BoxGroupAttribute(string name = "", bool foldable = false)
{
Name = name;
Foldable = foldable;
}
}
}
32 changes: 25 additions & 7 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class NaughtyInspector : UnityEditor.Editor
private IEnumerable<PropertyInfo> _nativeProperties;
private IEnumerable<MethodInfo> _methods;
private Dictionary<string, SavedBool> _foldouts = new Dictionary<string, SavedBool>();
private Dictionary<string, SavedBool> _boxFoldouts = new Dictionary<string, SavedBool>();

protected virtual void OnEnable()
{
Expand Down Expand Up @@ -97,12 +98,29 @@ protected void DrawSerializedProperties()
continue;
}

NaughtyEditorGUI.BeginBoxGroup_Layout(group.Key);
foreach (var property in visibleProperties)
var boxRect = NaughtyEditorGUI.BeginBoxGroup_Layout(group.Key.name);
bool unfolded = true; //Default to true so when the Foldable optional isn't being used, we'll default to showing the properties.

if (group.Key.foldable)
{
NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true);
if (!_boxFoldouts.ContainsKey(group.Key.name))
{
_boxFoldouts[group.Key.name] = new SavedBool($"{target.GetInstanceID()}.{group.Key}", false);
}
//Align Foldout icon to the Label Rect created in BeginBoxGroup_Layout(), but with width as wide as the inspector, allowing us to click anywhere on the label to toggle folding.
boxRect.x -= .75f * (boxRect.height = EditorGUIUtility.singleLineHeight * 1.25f);
boxRect.width = EditorGUIUtility.currentViewWidth;
unfolded = EditorGUI.Foldout(boxRect, _boxFoldouts[group.Key.name].Value, "", true);
_boxFoldouts[group.Key.name].Value = unfolded;
}

if (unfolded)
{
foreach (var property in visibleProperties)
{
NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true);
}
}
NaughtyEditorGUI.EndBoxGroup_Layout();
}

Expand Down Expand Up @@ -194,14 +212,14 @@ private static IEnumerable<SerializedProperty> GetNonGroupedProperties(IEnumerab
{
return properties.Where(p => PropertyUtility.GetAttribute<IGroupAttribute>(p) == null);
}

private static IEnumerable<IGrouping<string, SerializedProperty>> GetGroupedProperties(IEnumerable<SerializedProperty> properties)
private static IEnumerable<IGrouping<(string name, bool foldable), SerializedProperty>> GetGroupedProperties(IEnumerable<SerializedProperty> properties)
{
return properties
.Where(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p) != null)
.GroupBy(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p).Name);
.GroupBy(p => (PropertyUtility.GetAttribute<BoxGroupAttribute>(p).Name, PropertyUtility.GetAttribute<BoxGroupAttribute>(p).Foldable));
}

private static IEnumerable<IGrouping<string, SerializedProperty>> GetFoldoutProperties(IEnumerable<SerializedProperty> properties)
{
return properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,22 @@ public static float GetIndentLength(Rect sourceRect)
return indentLength;
}

public static void BeginBoxGroup_Layout(string label = "")
public static Rect BeginBoxGroup_Layout(string label = "")
{
EditorGUILayout.BeginVertical(GUI.skin.box);
var boxRect = EditorGUILayout.BeginVertical(GUI.skin.box);

if (!string.IsNullOrEmpty(label))
{
EditorGUILayout.LabelField(label, EditorStyles.boldLabel);
}
//Indent so the properties in our Box group are fully contained within it, including sub-foldouts.
EditorGUI.indentLevel++;
return boxRect;
}

public static void EndBoxGroup_Layout()
{
EditorGUI.indentLevel--;
EditorGUILayout.EndVertical();
}

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public class NaughtyComponent : MonoBehaviour
Give the fields meta data. A field can have more than one meta attributes.

### BoxGroup
Surrounds grouped fields with a box.
Surrounds grouped fields with a box and optionally a foldout.

```csharp
public class NaughtyComponent : MonoBehaviour
Expand All @@ -431,14 +431,14 @@ public class NaughtyComponent : MonoBehaviour
[BoxGroup("Integers")]
public int secondInt;

[BoxGroup("Floats")]
[BoxGroup("Floats", true)]
public float firstFloat;
[BoxGroup("Floats")]
[BoxGroup("Floats", true)]
public float secondFloat;
}
```

![inspector](https://github.com/dbrizov/NaughtyAttributes/blob/master/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.png)
![inspector](https://github.com/dbrizov/NaughtyAttributes/blob/master/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.gif)

### Foldout
Makes a foldout group.
Expand Down