Skip to content

Commit

Permalink
Merge branch 'release/1.0.0-preview.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
shniqq committed Jan 23, 2022
2 parents 21e2e2f + 887b4ef commit 85f7413
Show file tree
Hide file tree
Showing 27 changed files with 383 additions and 170 deletions.
Binary file modified Documentation~/CustomRuleExample1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Documentation~/CustomRuleExample2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Documentation~/CustomRuleExample3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Documentation~/HierarchyExample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Documentation~/RandomColorStylingExample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Documentation~/RuleAdditionExample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Documentation~/SettingsExample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ internal class DisplayScriptsFromSpecifiedAssemblyHierarchyRule : HierarchyLabel
[SerializeField] public string AssemblyName;
[SerializeField] private bool _assemblyFullName;

public override bool GetLabel(Component component, out string label)
public override bool GetLabel(Component component, out string label, out GUIStyle style)
{
style = StyleProvider.GetStyle(component);
if (_types.ContainsKey(component.GetType()))
{
label = component.GetType().Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ internal class DisplaySpecificComponentTypeHierarchyRule : HierarchyLabelRule
{
[SerializeField] private string _typeName;

public override bool GetLabel(Component component, out string label)
public override bool GetLabel(Component component, out string label, out GUIStyle style)
{
style = StyleProvider.GetStyle(component);
if (component.GetType().Name == _typeName)
{
label = _typeName;
Expand Down
3 changes: 3 additions & 0 deletions Editor/BuiltInStyles.meta

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

Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
using System;
using System.ComponentModel;
using UnityEditor;
using UnityEngine;
using Component = UnityEngine.Component;

namespace HierarchyLabels
namespace HierarchyLabels.BuiltInStyles
{
[Serializable]
[Serializable, DisplayName("Colored Label")]
public class ColorLabelStyleProvider : ILabelStyleProvider, ISerializationCallbackReceiver
{
[SerializeField] private Color _color = Color.black;

public GUIStyle GetStyle()
public GUIStyle GetStyle(Component component)
{
var style = new DefaultLabelStyleProvider().GetStyle();
style.normal.textColor = _color;
var style = new GUIStyle(DefaultLabelStyleProvider.Style)
{
normal =
{
textColor = _color
}
};
return style;
}

Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions Editor/BuiltInStyles/DefaultLabelStyleProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.ComponentModel;
using UnityEditor;
using UnityEngine;
using Component = UnityEngine.Component;

namespace HierarchyLabels.BuiltInStyles
{
[Serializable, DisplayName("Default Label Style")]
public class DefaultLabelStyleProvider : ILabelStyleProvider
{
private static GUIStyle _style;
public static GUIStyle Style =>
_style ??= new GUIStyle(EditorStyles.whiteLabel)
{
name = nameof(DefaultLabelStyleProvider),
fontSize = EditorStyles.whiteLabel.fontSize,
normal =
{
background = Texture2D.grayTexture
},
alignment = TextAnchor.MiddleCenter
};

public GUIStyle GetStyle(Component component)
{
return Style;
}
}
}
File renamed without changes.
23 changes: 0 additions & 23 deletions Editor/DefaultLabelStyleProvider.cs

This file was deleted.

61 changes: 40 additions & 21 deletions Editor/HierarchyLabelManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HierarchyLabels.BuiltInStyles;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
Expand All @@ -27,56 +28,74 @@ private static void OnDrawHierarchyItem(int instanceID, Rect selectionRect)

if (gameObject != null)
{
selectionRect.x += HierarchyLabelsSettings.instance.StyleProvider.GetStyle().CalcSize(new GUIContent(gameObject.name)).x +
selectionRect.x += EditorStyles.label.CalcSize(new GUIContent(gameObject.name)).x +
HierarchyLabelsSettings.instance.Alignment.x;
selectionRect.y += HierarchyLabelsSettings.instance.Alignment.y;

ApplyLabel(selectionRect, GetLabel(gameObject));
ApplyLabels(selectionRect, GetLabels(gameObject));
}
}

private static void ApplyLabel(Rect selectionRect, string label)
private static void ApplyLabels(Rect selectionRect, List<Tuple<string, GUIStyle>> labels)
{
if (string.IsNullOrEmpty(label))
if (!labels.Any())
{
return;
}

var labelStyle = HierarchyLabelsSettings.instance.StyleProvider.GetStyle();
var isFirst = true;
foreach (var (label, style) in labels)
{
if (!isFirst)
{
DrawLabelWithStyle(ref selectionRect, DefaultLabelStyleProvider.Style,
HierarchyLabelsSettings.instance.Separator);
}

DrawLabelWithStyle(ref selectionRect, style, label);
isFirst = false;
}
}

private static void DrawLabelWithStyle(ref Rect selectionRect, GUIStyle style, string label)
{
var labelStyle = new GUIStyle(style);
labelStyle.fontSize =
Convert.ToInt32(labelStyle.fontSize * HierarchyLabelsSettings.instance.FontSizeFactory);
var size = labelStyle.CalcSize(new GUIContent(label));

selectionRect.y = selectionRect.y + (selectionRect.height - size.y)/2f;
selectionRect.y += (selectionRect.height - size.y) / 2f;
selectionRect.width = size.x;
selectionRect.height = size.y;

GUI.Box(selectionRect, label, labelStyle);
selectionRect.x += size.x;
}

private static string GetLabel(GameObject gameObject)
private static List<Tuple<string, GUIStyle>> GetLabels(GameObject gameObject)
{
var label = new StringBuilder();
var list = new List<Tuple<string, GUIStyle>>();
if (HierarchyLabelsSettings.instance.HierarchyLabelRules == null)
{
return list;
}

foreach (var component in gameObject.GetComponents<Component>())
{
var componentLabel = string.Empty;
if (HierarchyLabelsSettings.instance.HierarchyLabelRules != null &&
HierarchyLabelsSettings.instance.HierarchyLabelRules
.Where(e => e != null)
.Any(rule => rule.GetLabel(component, out componentLabel)))
var applicableRules = HierarchyLabelsSettings.instance.HierarchyLabelRules
.Where(e => e != null)
.Select(rule => rule.GetLabel(component, out var componentLabel, out var style)
? new Tuple<string, GUIStyle>(componentLabel, style)
: null)
.Where(e => e is not null)
.ToList();
if (applicableRules.Any())
{
if (label.Length > 0)
{
label.Append(HierarchyLabelsSettings.instance.Separator);
}

label.Append(componentLabel);
list.AddRange(applicableRules);
}
}

return label.ToString();
return list;
}
}
}
6 changes: 5 additions & 1 deletion Editor/HierarchyLabelRule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using HierarchyLabels.BuiltInStyles;
using UnityEditor;
using UnityEngine;

Expand All @@ -7,7 +8,10 @@ namespace HierarchyLabels
[Serializable]
public abstract class HierarchyLabelRule : IHierarchyLabelRule, ISerializationCallbackReceiver
{
public abstract bool GetLabel(Component component, out string label);
[SerializeReference] private ILabelStyleProvider _styleProviderProvider = new DefaultLabelStyleProvider();
protected ILabelStyleProvider StyleProvider => _styleProviderProvider;

public abstract bool GetLabel(Component component, out string label, out GUIStyle style);

public virtual void OnBeforeSerialize()
{
Expand Down
Loading

0 comments on commit 85f7413

Please sign in to comment.