Created
July 7, 2024 14:34
-
-
Save digiwombat/87d3982545425cf58ada856fd2156c88 to your computer and use it in GitHub Desktop.
SizedBoxGroup Attribute for Odin Inspector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Sirenix.OdinInspector | |
{ | |
#pragma warning disable | |
using System; | |
using UnityEngine; | |
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)] | |
[System.Diagnostics.Conditional("UNITY_EDITOR")] | |
public class SizedBoxGroupAttribute : PropertyGroupAttribute | |
{ | |
private bool expanded; | |
public bool Expanded | |
{ | |
get { return this.expanded; } | |
set | |
{ | |
this.expanded = value; | |
this.HasDefinedExpanded = true; | |
} | |
} | |
public bool HasDefinedExpanded { get; private set; } | |
public bool HasDefinedColor = false; | |
public float R, G, B, A; | |
public float Padding; | |
public SizedBoxGroupAttribute(string groupName, float order = 0) | |
: base(groupName, order) | |
{ | |
} | |
public SizedBoxGroupAttribute(string groupName, string hexColor, float order = 0, float padding = 0) | |
: base(groupName, order) | |
{ | |
Color color = GameData.Colors.FromHex(hexColor); | |
this.R = color.r; | |
this.G = color.g; | |
this.B = color.b; | |
this.A = color.a; | |
HasDefinedColor = true; | |
this.Padding = padding; | |
} | |
public SizedBoxGroupAttribute(string groupName, float r, float g, float b, float a = 1f, float order = 0, float padding = 0) | |
: base(groupName, order) | |
{ | |
this.R = r; | |
this.G = g; | |
this.B = b; | |
this.A = a; | |
HasDefinedColor = true; | |
this.Padding = padding; | |
} | |
public SizedBoxGroupAttribute(string groupName, bool expanded, float order = 0) | |
: base(groupName, order) | |
{ | |
this.expanded = expanded; | |
this.HasDefinedExpanded = true; | |
} | |
protected override void CombineValuesWith(PropertyGroupAttribute other) | |
{ | |
var attr = other as SizedBoxGroupAttribute; | |
if (attr.HasDefinedExpanded) | |
{ | |
this.HasDefinedExpanded = true; | |
this.Expanded = attr.Expanded; | |
} | |
if (this.HasDefinedExpanded) | |
{ | |
attr.HasDefinedExpanded = true; | |
attr.Expanded = this.Expanded; | |
} | |
this.R = Math.Max(attr.R, this.R); | |
this.G = Math.Max(attr.G, this.G); | |
this.B = Math.Max(attr.B, this.B); | |
this.A = Math.Max(attr.A, this.A); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
namespace Sirenix.OdinInspector.Editor.Drawers | |
{ | |
#pragma warning disable | |
using Utilities.Editor; | |
using UnityEngine; | |
using UnityEditor; | |
using Sirenix.OdinInspector.Editor; | |
using Sirenix.OdinInspector.Editor.ValueResolvers; | |
public class SizedBoxGroupAttributeDrawer : OdinGroupDrawer<SizedBoxGroupAttribute> | |
{ | |
private ValueResolver<string> titleGetter; | |
private ValueResolver<Color> colorGetter; | |
private Color _color = Color.gray; | |
/// <summary> | |
/// Initializes this instance. | |
/// </summary> | |
protected override void Initialize() | |
{ | |
this.titleGetter = ValueResolver.GetForString(this.Property, this.Attribute.GroupName); | |
if (this.Attribute.HasDefinedExpanded) | |
{ | |
this.Property.State.Expanded = this.Attribute.Expanded; | |
} | |
if (this.Attribute.HasDefinedColor) | |
{ | |
_color = new Color(this.Attribute.R, this.Attribute.G, this.Attribute.B, this.Attribute.A); | |
} | |
else | |
{ | |
_color = Color.gray; | |
} | |
} | |
/// <summary> | |
/// Draws the property. | |
/// </summary> | |
protected override void DrawPropertyLayout(GUIContent label) | |
{ | |
var property = this.Property; | |
var attribute = this.Attribute; | |
if (this.titleGetter.HasError) | |
{ | |
SirenixEditorGUI.ErrorMessageBox(this.titleGetter.ErrorMessage); | |
} | |
GUIHelper.PushColor(_color); | |
SirenixEditorGUI.BeginBox(); | |
SirenixEditorGUI.BeginBoxHeader(); | |
GUIHelper.PopColor(); | |
var content = GUIHelper.TempContent(this.titleGetter.HasError ? property.Label.text : this.titleGetter.GetValue()); | |
GUIStyle s = new GUIStyle(SirenixGUIStyles.Label); | |
s.richText = true; | |
s.fixedWidth = s.CalcWidth(content); | |
s.wordWrap = true; | |
s.stretchHeight = true; | |
s.alignment = TextAnchor.UpperLeft; | |
var height = s.CalcHeight(content, s.fixedWidth); | |
if (this.Attribute.Padding > 0) | |
{ | |
GUILayout.Space(this.Attribute.Padding); | |
} | |
GUILayout.BeginVertical(); | |
GUILayout.Space(4); | |
if (this.Attribute.Padding > 0) | |
{ | |
GUILayout.Space(this.Attribute.Padding); | |
} | |
EditorGUILayout.LabelField(content, s); | |
if (this.Attribute.Padding > 0) | |
{ | |
GUILayout.Space(this.Attribute.Padding); | |
} | |
GUILayout.EndVertical(); | |
if (this.Attribute.Padding > 0) | |
{ | |
GUILayout.Space(this.Attribute.Padding); | |
} | |
SirenixEditorGUI.EndBoxHeader(); | |
for (int i = 0; i < property.Children.Count; i++) | |
{ | |
var child = property.Children[i]; | |
child.Draw(child.Label); | |
} | |
SirenixEditorGUI.EndBox(); | |
// EditorGUI.DrawRect( | |
// Property.LastDrawnValueRect.SetWidth(3), | |
// _color); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment