Tri-Inspector/Editor/Elements/TriInfoBoxElement.cs

65 lines
1.8 KiB
C#
Raw Normal View History

2021-12-07 10:20:36 -05:00
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
namespace TriInspector.Elements
{
public class TriInfoBoxElement : TriElement
{
private readonly GUIContent _message;
private readonly Color _color;
public TriInfoBoxElement(string message, MessageType type = MessageType.None, Color? color = null)
{
_message = new GUIContent(message, EditorGUIUtilityProxy.GetHelpIcon(type));
_color = color ?? GetColor(type);
}
public override float GetHeight(float width)
{
return EditorStyles.helpBox.CalcHeight(_message, width);
}
public override void OnGUI(Rect position)
{
2022-01-10 02:39:14 -05:00
using (TriGuiHelper.PushColor(_color))
{
2022-01-15 11:56:52 -05:00
GUI.Label(position, string.Empty, Styles.InfoBoxBg);
2022-01-10 02:39:14 -05:00
}
2022-01-15 11:56:52 -05:00
GUI.Label(position, _message, Styles.InfoBoxContent);
2021-12-07 10:20:36 -05:00
}
private static Color GetColor(MessageType type)
{
switch (type)
{
case MessageType.Error:
2022-01-15 11:56:52 -05:00
return new Color(1f, 0.4f, 0.4f);
2021-12-07 10:20:36 -05:00
case MessageType.Warning:
2022-01-15 11:56:52 -05:00
return new Color(1f, 0.8f, 0.2f);
2021-12-07 10:20:36 -05:00
default:
return Color.white;
}
}
2022-01-15 11:56:52 -05:00
private static class Styles
{
public static readonly GUIStyle InfoBoxBg;
public static readonly GUIStyle InfoBoxContent;
static Styles()
{
InfoBoxBg = new GUIStyle(EditorStyles.helpBox);
InfoBoxContent = new GUIStyle(EditorStyles.label)
{
padding = InfoBoxBg.padding,
fontSize = InfoBoxBg.fontSize,
alignment = InfoBoxBg.alignment,
};
}
}
2021-12-07 10:20:36 -05:00
}
}