1- using TriInspector . Utilities ;
1+ using System ;
2+ using TriInspector . Utilities ;
23using TriInspectorUnityInternalBridge ;
34using UnityEditor ;
45using UnityEngine ;
@@ -7,22 +8,44 @@ namespace TriInspector.Elements
78{
89 public class TriInfoBoxElement : TriElement
910 {
11+ private const int ActionSpacing = 5 ;
12+ private const int ActionWidth = 100 ;
13+ private const int ActionWidthWithSpacing = ActionWidth + ActionSpacing * 2 ;
14+
1015 private readonly GUIContent _message ;
1116 private readonly Texture2D _icon ;
1217 private readonly Color _color ;
18+ private readonly Action _inlineAction ;
19+ private readonly GUIContent _inlineActionContent ;
1320
14- public TriInfoBoxElement ( string message , TriMessageType type = TriMessageType . None , Color ? color = null )
21+ public TriInfoBoxElement ( string message , TriMessageType type = TriMessageType . None , Color ? color = null ,
22+ Action inlineAction = null , GUIContent inlineActionContent = null )
1523 {
1624 var messageType = GetMessageType ( type ) ;
1725 _icon = EditorGUIUtilityProxy . GetHelpIcon ( messageType ) ;
1826 _message = new GUIContent ( message ) ;
1927 _color = color ?? GetColor ( type ) ;
28+ _inlineAction = inlineAction ;
29+ _inlineActionContent = inlineActionContent ?? GUIContent . none ;
2030 }
2131
2232 public override float GetHeight ( float width )
2333 {
34+ var labelWidth = width ;
35+
36+ if ( _inlineAction != null )
37+ {
38+ labelWidth -= ActionWidthWithSpacing ;
39+ }
40+
2441 var style = _icon == null ? Styles . InfoBoxContentNone : Styles . InfoBoxContent ;
25- var height = style . CalcHeight ( _message , width ) ;
42+ var height = style . CalcHeight ( _message , labelWidth ) ;
43+
44+ if ( _inlineAction != null )
45+ {
46+ height = Mathf . Max ( height , CalcActionHeight ( ) + ActionSpacing * 2 ) ;
47+ }
48+
2649 return Mathf . Max ( 26 , height ) ;
2750 }
2851
@@ -33,21 +56,57 @@ public override void OnGUI(Rect position)
3356 GUI . Label ( position , string . Empty , Styles . InfoBoxBg ) ;
3457 }
3558
59+ var labelWidth = position . width ;
60+
61+ if ( _inlineAction != null )
62+ {
63+ labelWidth -= ActionWidthWithSpacing ;
64+ }
65+
3666 if ( _icon != null )
3767 {
68+ var labelRect = new Rect ( position )
69+ {
70+ width = labelWidth ,
71+ } ;
72+
3873 var iconRect = new Rect ( position )
3974 {
4075 xMin = position . xMin + 4 ,
4176 width = 20 ,
4277 } ;
4378
44- GUI . Label ( position , _message , Styles . InfoBoxContent ) ;
79+ GUI . Label ( labelRect , _message , Styles . InfoBoxContent ) ;
4580 GUI . DrawTexture ( iconRect , _icon , ScaleMode . ScaleToFit ) ;
4681 }
4782 else
4883 {
4984 GUI . Label ( position , _message , Styles . InfoBoxContentNone ) ;
5085 }
86+
87+ if ( _inlineAction != null )
88+ {
89+ var fixHeight = CalcActionHeight ( ) ;
90+
91+ var actionRect = new Rect ( position )
92+ {
93+ xMax = position . xMax - ActionSpacing ,
94+ xMin = position . xMax - ActionWidth - ActionSpacing ,
95+ yMin = position . center . y - fixHeight / 2 ,
96+ yMax = position . center . y + fixHeight / 2 ,
97+ } ;
98+
99+ if ( GUI . Button ( actionRect , _inlineActionContent , Styles . InfoBoxInlineAction ) )
100+ {
101+ _inlineAction ? . Invoke ( ) ;
102+ }
103+ }
104+ }
105+
106+
107+ private float CalcActionHeight ( )
108+ {
109+ return Styles . InfoBoxInlineAction . CalcHeight ( _inlineActionContent , ActionWidth ) ;
51110 }
52111
53112 private static Color GetColor ( TriMessageType type )
@@ -82,6 +141,7 @@ private static class Styles
82141 public static readonly GUIStyle InfoBoxBg ;
83142 public static readonly GUIStyle InfoBoxContent ;
84143 public static readonly GUIStyle InfoBoxContentNone ;
144+ public static readonly GUIStyle InfoBoxInlineAction ;
85145
86146 static Styles ( )
87147 {
@@ -97,6 +157,10 @@ static Styles()
97157 {
98158 padding = new RectOffset ( 26 , 4 , 4 , 4 ) ,
99159 } ;
160+ InfoBoxInlineAction = new GUIStyle ( GUI . skin . button )
161+ {
162+ wordWrap = true ,
163+ } ;
100164 }
101165 }
102166 }
0 commit comments