Singularity/Library/PackageCache/com.unity.2d.common@6.0.6/Editor/InspectorUtils.cs

145 lines
5.9 KiB
C#
Raw Permalink Normal View History

2024-05-06 14:45:45 -04:00
using System;
using UnityEditor;
using UnityEngine;
namespace UnityEditor.U2D.Common
{
/// <summary>
/// Utilities to draw foldout headers in Inspector
/// </summary>
internal class InspectorUtils
{
/// <summary>Draw a splitter separator</summary>
/// <param name="isBoxed">[Optional] add margin if the splitter is boxed</param>
public static void DrawSplitter(bool isBoxed = false)
{
var rect = GUILayoutUtility.GetRect(1f, 1f);
float xMin = rect.xMin;
// Splitter rect should be full-width
rect.xMin = 0f;
rect.width += 4f;
if (isBoxed)
{
rect.xMin = xMin == 7.0 ? 4.0f : EditorGUIUtility.singleLineHeight;
rect.width -= 1;
}
if (Event.current.type != EventType.Repaint)
return;
EditorGUI.DrawRect(rect, !EditorGUIUtility.isProSkin
? new Color(0.6f, 0.6f, 0.6f, 1.333f)
: new Color(0.12f, 0.12f, 0.12f, 1.333f));
}
/// <summary>Draw a header</summary>
/// <param name="title">Title of the header</param>
public static void DrawHeader(string title)
=> DrawHeader(EditorGUIUtility.TrTextContent(title));
/// <summary>Draw a header</summary>
/// <param name="title">Title of the header</param>
public static void DrawHeader(GUIContent title)
{
var backgroundRect = GUILayoutUtility.GetRect(1f, 17f);
var labelRect = backgroundRect;
labelRect.xMin += 16f;
labelRect.xMax -= 20f;
var foldoutRect = backgroundRect;
foldoutRect.y += 1f;
foldoutRect.width = 13f;
foldoutRect.height = 13f;
// Background rect should be full-width
backgroundRect.xMin = 0f;
backgroundRect.width += 4f;
// Background
float backgroundTint = EditorGUIUtility.isProSkin ? 0.1f : 1f;
EditorGUI.DrawRect(backgroundRect, new Color(backgroundTint, backgroundTint, backgroundTint, 0.2f));
// Title
EditorGUI.LabelField(labelRect, title, EditorStyles.boldLabel);
}
/// <summary> Draw a foldout header </summary>
/// <param name="title"> The title of the header </param>
/// <param name="state"> The state of the header </param>
/// <param name="isBoxed"> [optional] is the eader contained in a box style ? </param>
/// <param name="hasMoreOptions"> [optional] Delegate used to draw the right state of the advanced button. If null, no button drawn. </param>
/// <param name="toggleMoreOption"> [optional] Callback call when advanced button clicked. Should be used to toggle its state. </param>
/// <returns>return the state of the foldout header</returns>
public static bool DrawHeaderFoldout(string title, bool state, bool isBoxed = false, Func<bool> hasMoreOptions = null, Action toggleMoreOption = null)
=> DrawHeaderFoldout(EditorGUIUtility.TrTextContent(title), state, isBoxed, hasMoreOptions, toggleMoreOption);
/// <summary> Draw a foldout header </summary>
/// <param name="title"> The title of the header </param>
/// <param name="state"> The state of the header </param>
/// <param name="isBoxed"> [optional] is the eader contained in a box style ? </param>
/// <param name="hasMoreOptions"> [optional] Delegate used to draw the right state of the advanced button. If null, no button drawn. </param>
/// <param name="toggleMoreOptions"> [optional] Callback call when advanced button clicked. Should be used to toggle its state. </param>
/// <returns>return the state of the foldout header</returns>
public static bool DrawHeaderFoldout(GUIContent title, bool state, bool isBoxed = false, Func<bool> hasMoreOptions = null, Action toggleMoreOptions = null)
{
const float height = 17f;
var backgroundRect = GUILayoutUtility.GetRect(1f, height);
float xMin = backgroundRect.xMin;
var labelRect = backgroundRect;
labelRect.xMin += 16f;
labelRect.xMax -= 20f;
var foldoutRect = backgroundRect;
foldoutRect.y += 1f;
foldoutRect.width = 13f;
foldoutRect.height = 13f;
foldoutRect.x = labelRect.xMin + 15 * (EditorGUI.indentLevel - 1); //fix for presset
// More options 1/2
var moreOptionsRect = new Rect();
if (hasMoreOptions != null)
{
moreOptionsRect = backgroundRect;
moreOptionsRect.x += moreOptionsRect.width - 16 - 1;
moreOptionsRect.height = 15;
moreOptionsRect.width = 16;
}
// Background rect should be full-width
backgroundRect.xMin = 0f;
backgroundRect.width += 4f;
if (isBoxed)
{
labelRect.xMin += 5;
foldoutRect.xMin += 5;
backgroundRect.xMin = xMin == 7.0 ? 4.0f : EditorGUIUtility.singleLineHeight;
backgroundRect.width -= 1;
}
// Background
float backgroundTint = EditorGUIUtility.isProSkin ? 0.1f : 1f;
EditorGUI.DrawRect(backgroundRect, new Color(backgroundTint, backgroundTint, backgroundTint, 0.2f));
// Title
EditorGUI.LabelField(labelRect, title, EditorStyles.boldLabel);
// Active checkbox
state = GUI.Toggle(foldoutRect, state, GUIContent.none, EditorStyles.foldout);
var e = Event.current;
if (e.type == EventType.MouseDown && backgroundRect.Contains(e.mousePosition) && !moreOptionsRect.Contains(e.mousePosition) && e.button == 0)
{
state = !state;
e.Use();
}
return state;
}
}
}