Tri-Inspector/Editor/Utilities/TriGuiHelper.cs

118 lines
2.8 KiB
C#
Raw Normal View History

2022-01-10 02:39:14 -05:00
using System;
using System.Collections.Generic;
2021-12-07 10:20:36 -05:00
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
2021-12-07 10:20:36 -05:00
namespace TriInspector.Utilities
{
public static class TriGuiHelper
{
private static readonly Stack<Object> TargetObjects = new Stack<Object>();
internal static bool IsAnyEditorPushed()
{
return TargetObjects.Count > 0;
}
internal static bool IsEditorTargetPushed(Object obj)
{
foreach (var targetObject in TargetObjects)
{
if (targetObject == obj)
{
return true;
}
}
return false;
}
internal static EditorScope PushEditorTarget(Object obj)
{
return new EditorScope(obj);
}
2022-01-10 02:39:14 -05:00
public static LabelWidthScope PushLabelWidth(float labelWidth)
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
return new LabelWidthScope(labelWidth);
2021-12-07 10:20:36 -05:00
}
2022-01-10 02:39:14 -05:00
public static IndentLevelScope PushIndentLevel(int indent = 1)
2022-01-08 12:45:11 -05:00
{
2022-01-10 02:39:14 -05:00
return new IndentLevelScope(indent);
2022-01-08 12:45:11 -05:00
}
2022-01-10 02:39:14 -05:00
public static GuiColorScope PushColor(Color color)
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
return new GuiColorScope(color);
2021-12-07 10:20:36 -05:00
}
public readonly struct EditorScope : IDisposable
{
public EditorScope(Object obj)
{
TargetObjects.Push(obj);
}
public void Dispose()
{
TargetObjects.Pop();
}
}
2022-01-10 02:39:14 -05:00
public readonly struct LabelWidthScope : IDisposable
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
private readonly float _oldLabelWidth;
2021-12-07 10:20:36 -05:00
2022-01-10 02:39:14 -05:00
public LabelWidthScope(float labelWidth)
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
_oldLabelWidth = EditorGUIUtility.labelWidth;
2021-12-07 10:20:36 -05:00
2022-01-10 02:39:14 -05:00
if (labelWidth > 0)
{
EditorGUIUtility.labelWidth = labelWidth;
}
}
2021-12-07 10:20:36 -05:00
2022-01-10 02:39:14 -05:00
public void Dispose()
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
EditorGUIUtility.labelWidth = _oldLabelWidth;
2021-12-07 10:20:36 -05:00
}
}
2022-01-10 02:39:14 -05:00
public readonly struct IndentLevelScope : IDisposable
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
private readonly int _oldIndentLevel;
2021-12-07 10:20:36 -05:00
2022-01-10 02:39:14 -05:00
public IndentLevelScope(int indentLevel)
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
_oldIndentLevel = EditorGUI.indentLevel;
2021-12-07 10:20:36 -05:00
2022-01-10 02:39:14 -05:00
EditorGUI.indentLevel += indentLevel;
2021-12-07 10:20:36 -05:00
}
2022-01-10 02:39:14 -05:00
public void Dispose()
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
EditorGUI.indentLevel = _oldIndentLevel;
2021-12-07 10:20:36 -05:00
}
}
2022-01-10 02:39:14 -05:00
public readonly struct GuiColorScope : IDisposable
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
private readonly Color _oldColor;
2021-12-07 10:20:36 -05:00
2022-01-10 02:39:14 -05:00
public GuiColorScope(Color color)
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
_oldColor = GUI.color;
2021-12-07 10:20:36 -05:00
2022-01-10 02:39:14 -05:00
GUI.color = color;
}
2021-12-07 10:20:36 -05:00
2022-01-10 02:39:14 -05:00
public void Dispose()
2021-12-07 10:20:36 -05:00
{
2022-01-10 02:39:14 -05:00
GUI.color = _oldColor;
2021-12-07 10:20:36 -05:00
}
}
}
}