22using System . Reflection ;
33using TriInspector ;
44using TriInspector . Drawers ;
5+ using TriInspector . Elements ;
56using TriInspector . Resolvers ;
7+ using TriInspector . Utilities ;
68using UnityEditor ;
79using UnityEngine ;
810
@@ -16,12 +18,10 @@ public class ButtonDrawer : TriAttributeDrawer<ButtonAttribute>
1618
1719 public override TriExtensionInitializationResult Initialize ( TriPropertyDefinition propertyDefinition )
1820 {
19- var isValidMethod = propertyDefinition . TryGetMemberInfo ( out var memberInfo ) &&
20- memberInfo is MethodInfo mi &&
21- mi . GetParameters ( ) . Length == 0 ;
21+ var isValidMethod = propertyDefinition . TryGetMemberInfo ( out var memberInfo ) && memberInfo is MethodInfo ;
2222 if ( ! isValidMethod )
2323 {
24- return "[Button] valid only on methods without parameters " ;
24+ return "[Button] valid only on methods" ;
2525 }
2626
2727 _nameResolver = ValueResolver . ResolveString ( propertyDefinition , Attribute . Name ) ;
@@ -33,33 +33,94 @@ memberInfo is MethodInfo mi &&
3333 return TriExtensionInitializationResult . Ok ;
3434 }
3535
36- public override float GetHeight ( float width , TriProperty property , TriElement next )
36+ public override TriElement CreateElement ( TriProperty property , TriElement next )
3737 {
38- if ( Attribute . ButtonSize != 0 )
39- {
40- return Attribute . ButtonSize ;
41- }
42-
43- return EditorGUIUtility . singleLineHeight ;
38+ return new TriButtonElement ( property , Attribute , _nameResolver ) ;
4439 }
4540
46- public override void OnGUI ( Rect position , TriProperty property , TriElement next )
41+ private class TriButtonElement : TriHeaderGroupBaseElement
4742 {
48- var name = _nameResolver . GetValue ( property ) ;
43+ private readonly TriProperty _property ;
44+ private readonly ButtonAttribute _attribute ;
45+ private readonly ValueResolver < string > _nameResolver ;
46+ private readonly object [ ] _invocationArgs ;
4947
50- if ( string . IsNullOrEmpty ( name ) )
48+ public TriButtonElement ( TriProperty property , ButtonAttribute attribute ,
49+ ValueResolver < string > nameResolver )
5150 {
52- name = property . DisplayName ;
51+ _property = property ;
52+ _attribute = attribute ;
53+ _nameResolver = nameResolver ;
54+
55+ var mi = property . TryGetMemberInfo ( out var memberInfo )
56+ ? ( MethodInfo ) memberInfo
57+ : throw new Exception ( "TriButtonElement requires MethodInfo" ) ;
58+
59+ var parameters = mi . GetParameters ( ) ;
60+
61+ _invocationArgs = new object [ parameters . Length ] ;
62+
63+ for ( var i = 0 ; i < parameters . Length ; i ++ )
64+ {
65+ var pIndex = i ;
66+ var pInfo = parameters [ pIndex ] ;
67+
68+ if ( pInfo . HasDefaultValue )
69+ {
70+ _invocationArgs [ pIndex ] = pInfo . DefaultValue ;
71+ }
72+
73+ var pTriDefinition = TriPropertyDefinition . CreateForGetterSetter (
74+ pIndex , pInfo . Name , pInfo . ParameterType ,
75+ ( ( self , targetIndex ) => _invocationArgs [ pIndex ] ) ,
76+ ( ( self , targetIndex , value ) => _invocationArgs [ pIndex ] = value ) ) ;
77+
78+ var pTriProperty = new TriProperty ( _property . PropertyTree , _property , pTriDefinition , null ) ;
79+
80+ AddChild ( new TriPropertyElement ( pTriProperty ) ) ;
81+ }
5382 }
5483
55- if ( string . IsNullOrEmpty ( name ) )
84+ protected override float GetHeaderHeight ( float width )
5685 {
57- name = property . RawName ;
86+ return GetButtonHeight ( ) ;
87+ }
88+
89+ protected override void DrawHeader ( Rect position )
90+ {
91+ if ( _invocationArgs . Length > 0 )
92+ {
93+ TriEditorGUI . DrawBox ( position , TriEditorStyles . TabOnlyOne ) ;
94+ }
95+
96+ var name = _nameResolver . GetValue ( _property ) ;
97+
98+ if ( string . IsNullOrEmpty ( name ) )
99+ {
100+ name = _property . DisplayName ;
101+ }
102+
103+ if ( string . IsNullOrEmpty ( name ) )
104+ {
105+ name = _property . RawName ;
106+ }
107+
108+ var buttonRect = new Rect ( position )
109+ {
110+ height = GetButtonHeight ( ) ,
111+ } ;
112+
113+ if ( GUI . Button ( buttonRect , name ) )
114+ {
115+ InvokeButton ( _property , _invocationArgs ) ;
116+ }
58117 }
59118
60- if ( GUI . Button ( position , name ) )
119+ private float GetButtonHeight ( )
61120 {
62- InvokeButton ( property , Array . Empty < object > ( ) ) ;
121+ return _attribute . ButtonSize != 0
122+ ? _attribute . ButtonSize
123+ : EditorGUIUtility . singleLineHeight ;
63124 }
64125 }
65126
0 commit comments