Make InfoBox messages more compact

This commit is contained in:
VladV 2022-08-27 16:46:29 +03:00
parent 8034200f4c
commit 7887cab152
2 changed files with 29 additions and 6 deletions

View File

@ -8,18 +8,22 @@ namespace TriInspector.Elements
public class TriInfoBoxElement : TriElement
{
private readonly GUIContent _message;
private readonly Texture2D _icon;
private readonly Color _color;
public TriInfoBoxElement(string message, TriMessageType type = TriMessageType.None, Color? color = null)
{
var messageType = GetMessageType(type);
_message = new GUIContent(message, EditorGUIUtilityProxy.GetHelpIcon(messageType));
_icon = EditorGUIUtilityProxy.GetHelpIcon(messageType);
_message = new GUIContent(message);
_color = color ?? GetColor(type);
}
public override float GetHeight(float width)
{
return EditorStyles.helpBox.CalcHeight(_message, width);
var style = _icon == null ? Styles.InfoBoxContentNone : Styles.InfoBoxContent;
var height = style.CalcHeight(_message, width);
return Mathf.Max(26, height);
}
public override void OnGUI(Rect position)
@ -31,7 +35,21 @@ namespace TriInspector.Elements
GUI.Label(position, string.Empty, Styles.InfoBoxBg);
}
if (_icon != null)
{
var iconRect = new Rect(position)
{
xMin = position.xMin + 4,
width = 20,
};
GUI.Label(position, _message, Styles.InfoBoxContent);
GUI.DrawTexture(iconRect, _icon, ScaleMode.ScaleToFit);
}
else
{
GUI.Label(position, _message, Styles.InfoBoxContentNone);
}
}
private static Color GetColor(TriMessageType type)
@ -65,17 +83,22 @@ namespace TriInspector.Elements
{
public static readonly GUIStyle InfoBoxBg;
public static readonly GUIStyle InfoBoxContent;
public static readonly GUIStyle InfoBoxContentNone;
static Styles()
{
InfoBoxBg = new GUIStyle(EditorStyles.helpBox);
InfoBoxContent = new GUIStyle(EditorStyles.label)
InfoBoxContentNone = new GUIStyle(EditorStyles.label)
{
padding = InfoBoxBg.padding,
padding = new RectOffset(4, 4, 4, 4),
fontSize = InfoBoxBg.fontSize,
alignment = TextAnchor.MiddleLeft,
wordWrap = true,
};
InfoBoxContent = new GUIStyle(InfoBoxContentNone)
{
padding = new RectOffset(26, 4, 4, 4),
};
}
}
}

View File

@ -2,7 +2,7 @@
_Advanced inspector attributes for Unity_
![Tri-Inspector-Demo](https://user-images.githubusercontent.com/26966368/171363040-f7a7d6dd-eef4-4a9e-adb0-7d0e033a81ce.png)
![Tri-Inspector-Demo](https://user-images.githubusercontent.com/26966368/187032895-8c41295b-dd82-40ad-80c3-1efaad202732.png)
- [How to Install](#How-to-Install)
- [Roadmap](#Roadmap)