Created
December 17, 2020 14:48
-
-
Save Schwapo/8898b35e68b650613b0ed96debdf799f to your computer and use it in GitHub Desktop.
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
using Sirenix.OdinInspector; | |
using Sirenix.Utilities; | |
#if UNITY_EDITOR | |
using Sirenix.OdinInspector.Editor; | |
using Sirenix.Utilities.Editor; | |
using UnityEditor; | |
#endif | |
using UnityEngine; | |
/// <summary> | |
/// <para>ColoredBoxGroup is used on any property and organizes the property in a boxed group with a colored header.</para> | |
/// <para>Use this to cleanly organize relevant values together in the inspector.</para> | |
/// </summary> | |
public class ColoredBoxGroupAttribute : PropertyGroupAttribute | |
{ | |
private static readonly byte[] GradientColorsLTR = | |
{ | |
163, 163, 163, 254, | |
165, 165, 165, 255, | |
169, 169, 169, 254, | |
171, 171, 171, 255, | |
174, 174, 174, 255, | |
176, 176, 176, 255, | |
180, 180, 180, 255, | |
182, 182, 182, 255, | |
186, 186, 186, 255, | |
189, 189, 189, 254, | |
192, 192, 192, 255, | |
194, 194, 194, 255, | |
198, 198, 198, 254, | |
200, 200, 200, 255, | |
204, 204, 204, 254, | |
206, 206, 206, 255, | |
209, 209, 209, 255, | |
212, 212, 212, 255, | |
215, 215, 215, 255, | |
219, 219, 219, 254, | |
221, 221, 221, 255, | |
224, 224, 224, 255, | |
227, 227, 227, 255, | |
231, 231, 231, 254, | |
233, 233, 233, 255, | |
237, 237, 237, 254, | |
239, 239, 239, 255, | |
242, 242, 242, 255, | |
245, 245, 245, 254, | |
248, 248, 248, 254, | |
251, 251, 251, 255, | |
254, 254, 254, 255 | |
}; | |
private static readonly byte[] GradientColorsRTL = | |
{ | |
253, 253, 253, 254, | |
248, 248, 248, 255, | |
245, 245, 245, 254, | |
241, 241, 241, 255, | |
237, 237, 237, 255, | |
232, 232, 232, 255, | |
229, 229, 229, 255, | |
225, 225, 225, 255, | |
221, 221, 221, 255, | |
218, 218, 218, 254, | |
214, 214, 214, 255, | |
209, 209, 209, 255, | |
206, 206, 206, 254, | |
201, 201, 201, 255, | |
199, 199, 199, 254, | |
193, 193, 193, 255, | |
190, 190, 190, 255, | |
185, 185, 185, 255, | |
182, 182, 182, 255, | |
178, 178, 178, 254, | |
174, 174, 174, 255, | |
170, 170, 170, 255, | |
165, 165, 165, 255, | |
162, 162, 162, 254, | |
157, 157, 157, 255, | |
154, 154, 154, 254, | |
149, 149, 149, 255, | |
145, 145, 145, 255, | |
142, 142, 142, 254, | |
138, 138, 138, 254, | |
134, 134, 134, 255, | |
130, 130, 130, 255 | |
}; | |
private static readonly byte[] GradientColorsCentered = | |
{ | |
132, 132, 132, 254, | |
140, 140, 140, 255, | |
149, 149, 149, 254, | |
156, 156, 156, 255, | |
164, 164, 164, 255, | |
172, 172, 172, 255, | |
180, 180, 180, 255, | |
188, 188, 188, 255, | |
196, 196, 196, 255, | |
204, 204, 204, 254, | |
212, 212, 212, 255, | |
220, 220, 220, 255, | |
229, 229, 229, 254, | |
235, 235, 235, 255, | |
245, 245, 245, 254, | |
251, 251, 251, 255, | |
250, 250, 250, 255, | |
242, 242, 242, 255, | |
235, 235, 235, 255, | |
227, 227, 227, 254, | |
218, 218, 218, 255, | |
211, 211, 211, 255, | |
202, 202, 202, 255, | |
195, 195, 195, 254, | |
187, 187, 187, 255, | |
179, 179, 179, 254, | |
171, 171, 171, 255, | |
163, 163, 163, 255, | |
156, 156, 156, 254, | |
147, 147, 147, 254, | |
139, 139, 139, 255, | |
132, 132, 132, 255 | |
}; | |
public enum Gradient { LeftToRight, RightToLeft, Centered } | |
public readonly Gradient GradientType; | |
public readonly Color HeaderColor; | |
public readonly Color LabelColor; | |
public readonly bool ApplyGradient; | |
public readonly bool CenterLabel; | |
public readonly bool BoldLabel; | |
private static Texture2D _headerGradientLTR; | |
private static Texture2D _headerGradientRTL; | |
private static Texture2D _headerGradientCentered; | |
public static Texture2D HeaderGradientLTR | |
{ | |
get | |
{ | |
if (_headerGradientLTR == null) | |
{ | |
_headerGradientLTR = new Texture2D(32, 1, TextureFormat.RGBA32, false) {wrapMode = TextureWrapMode.Clamp}; | |
_headerGradientLTR.LoadRawTextureData(GradientColorsLTR); | |
_headerGradientLTR.Apply(); | |
} | |
return _headerGradientLTR; | |
} | |
} | |
public static Texture2D HeaderGradientRTL | |
{ | |
get | |
{ | |
if (_headerGradientRTL == null) | |
{ | |
_headerGradientRTL = new Texture2D(32, 1, TextureFormat.RGBA32, false) {wrapMode = TextureWrapMode.Clamp}; | |
_headerGradientRTL.LoadRawTextureData(GradientColorsRTL); | |
_headerGradientRTL.Apply(); | |
} | |
return _headerGradientRTL; | |
} | |
} | |
public static Texture2D HeaderGradientCentered | |
{ | |
get | |
{ | |
if (_headerGradientCentered == null) | |
{ | |
_headerGradientCentered = new Texture2D(32, 1, TextureFormat.RGBA32, false) {wrapMode = TextureWrapMode.Clamp}; | |
_headerGradientCentered.LoadRawTextureData(GradientColorsCentered); | |
_headerGradientCentered.Apply(); | |
} | |
return _headerGradientCentered; | |
} | |
} | |
/// <param name="groupName">The box group name.</param> | |
/// <param name="headerColor">The color of the header e.g. "#FF0000".</param> | |
/// <param name="labelColor">The color of the label e.g. "#FF0000".</param> | |
/// <param name="boldLabel">Makes the header label bold if set to true. (default: false)</param> | |
/// <param name="applyGradient">Adds a gradient to the header if set to true. (default: true)</param> | |
/// <param name="gradientType">Applies the given gradient type. (default: Gradient.LeftToRight)</param> | |
/// <param name="centerLabel">Centers the label inside the header if set to true. (default: false)</param> | |
public ColoredBoxGroupAttribute(string groupName, string headerColor = null, string labelColor = null, bool boldLabel = false, | |
bool applyGradient = true, Gradient gradientType = Gradient.LeftToRight, bool centerLabel = false) : base(groupName) | |
{ | |
if (headerColor.IsNullOrWhitespace()) HeaderColor = SirenixGUIStyles.HeaderBoxBackgroundColor; | |
else ColorUtility.TryParseHtmlString(headerColor, out HeaderColor); | |
if (labelColor.IsNullOrWhitespace()) LabelColor = SirenixGUIStyles.WhiteLabel.normal.textColor; | |
else ColorUtility.TryParseHtmlString(labelColor, out LabelColor); | |
BoldLabel = boldLabel; | |
CenterLabel = centerLabel; | |
ApplyGradient = applyGradient; | |
GradientType = gradientType; | |
} | |
} | |
#if UNITY_EDITOR | |
public class ColoredBoxGroupAttributeDrawer : OdinGroupDrawer<ColoredBoxGroupAttribute> | |
{ | |
protected override void DrawPropertyLayout(GUIContent label) | |
{ | |
SirenixEditorGUI.BeginBox(); | |
BeginColoredBoxHeader(Attribute); | |
GUIHelper.PushColor(Attribute.LabelColor); | |
GUIStyle labelAlignment = GUI.skin.label; | |
labelAlignment.alignment = TextAnchor.MiddleCenter; | |
GUIStyle labelStyle = new GUIStyle(GUI.skin.label) | |
{ | |
alignment = Attribute.CenterLabel ? TextAnchor.MiddleCenter : TextAnchor.MiddleLeft, | |
fontStyle = Attribute.BoldLabel ? FontStyle.Bold : FontStyle.Normal | |
}; | |
EditorGUILayout.LabelField(Attribute.GroupName, labelStyle); | |
GUIHelper.PopColor(); | |
SirenixEditorGUI.EndBoxHeader(); | |
GUILayout.Space(1f); | |
foreach (InspectorProperty inspectorProperty in Property.Children) | |
inspectorProperty.Draw(); | |
SirenixEditorGUI.EndBox(); | |
} | |
private static void BeginColoredBoxHeader(ColoredBoxGroupAttribute attr) | |
{ | |
GUILayout.Space(-3f); | |
Rect rect = EditorGUILayout.BeginHorizontal(SirenixGUIStyles.BoxHeaderStyle, GUILayoutOptions.ExpandWidth()); | |
if (Event.current.type == EventType.Repaint) | |
{ | |
rect = new Rect(rect.x - 3f, rect.y + 1f, rect.width + 6f, rect.height - 1f); | |
Texture2D texture = Texture2D.whiteTexture; | |
if (attr.ApplyGradient) | |
{ | |
texture = attr.GradientType switch | |
{ | |
ColoredBoxGroupAttribute.Gradient.LeftToRight => ColoredBoxGroupAttribute.HeaderGradientLTR, | |
ColoredBoxGroupAttribute.Gradient.RightToLeft => ColoredBoxGroupAttribute.HeaderGradientRTL, | |
ColoredBoxGroupAttribute.Gradient.Centered => ColoredBoxGroupAttribute.HeaderGradientCentered, | |
_ => texture | |
}; | |
} | |
GUI.DrawTexture(rect, texture, ScaleMode.StretchToFill, true, 0f, attr.HeaderColor, 0f, 2f); | |
rect.y += 1f; | |
SirenixEditorGUI.DrawBorders(rect, 0, 0, 0, 1, new Color(0f, 0f, 0f, 0.4f)); | |
} | |
GUIHelper.PushLabelWidth(GUIHelper.BetterLabelWidth - 4f); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment