5
5
using System . Linq ;
6
6
using System . Reflection ;
7
7
using System . Text ;
8
- using UnityEngine . UI ;
9
8
10
9
namespace System . Windows . Forms . Design
11
10
{
12
11
public class ControlDesigner : IControlDesigner
13
12
{
14
- private List < controlProperty > props ;
13
+ private objectEditor editor ;
15
14
private bool toggleEditor = true ;
16
15
17
16
public Control Control { get ; set ; }
@@ -20,122 +19,248 @@ public ControlDesigner(Control c)
20
19
{
21
20
Control = c ;
22
21
23
- if ( Control != null )
24
- Init ( ) ;
22
+ editor = new objectEditor ( c , Control . GetType ( ) . Name ) ;
23
+ editor . toggleEditor = true ;
25
24
}
26
25
27
- private Control DrawProperty ( controlProperty p )
26
+ public virtual object Draw ( int width , int height )
28
27
{
28
+ if ( Control == null ) return null ;
29
+
29
30
Control controlToSet = null ;
30
- var val = p . info . GetValue ( Control , null ) ;
31
- Type type = null ;
32
- if ( val != null )
33
- type = val . GetType ( ) ;
34
31
35
- var pSetMethod = p . info . GetSetMethod ( true ) ;
36
- if ( pSetMethod == null || pSetMethod . IsPrivate )
37
- {
38
- Editor . Label ( p . info . Name , val ) ;
39
- return null ;
40
- }
32
+ Editor . BeginGroup ( width - 24 , "" ) ;
41
33
42
- if ( val is bool )
43
- {
44
- var bVal = ( bool ) val ;
45
- var ebVal = Editor . BooleanField ( p . info . Name , bVal ) ;
46
- if ( ebVal . Changed )
47
- p . info . SetValue ( Control , ebVal . Value , null ) ;
48
- }
49
- else if ( val is Control )
50
- {
51
- var cVal = val as Control ;
52
- if ( Editor . Button ( p . info . Name , cVal . GetType ( ) . Name ) )
53
- controlToSet = cVal ;
54
- }
55
- else if ( val is Color )
56
- {
57
- var colorVal = ( Color ) val ;
58
- Editor . ColorField ( p . info . Name , colorVal , c => p . info . SetValue ( Control , c , null ) ) ;
59
- }
60
- else if ( val is string )
34
+ controlToSet = editor . Draw ( ) ;
35
+
36
+ Editor . EndGroup ( ) ;
37
+
38
+ return controlToSet ;
39
+ }
40
+
41
+ private class controlProperty
42
+ {
43
+ public objectEditor editor ;
44
+ public bool expanded ;
45
+ public PropertyInfo info ;
46
+ }
47
+ private class objectEditor
48
+ {
49
+ private readonly List < FieldInfo > fields ;
50
+ private readonly List < MethodInfo > methods ;
51
+ private readonly object obj ;
52
+ private readonly List < controlProperty > props ;
53
+ private readonly string name ;
54
+
55
+ public bool toggleEditor ;
56
+
57
+ public objectEditor ( object o , string objName )
61
58
{
62
- var stringtVal = ( string ) val ;
63
- var esVal = Editor . TextField ( p . info . Name , stringtVal ) ;
64
- if ( esVal . Changed )
65
- p . info . SetValue ( Control , esVal . Value , null ) ;
59
+ obj = o ;
60
+ name = objName ;
61
+
62
+ var objType = obj . GetType ( ) ;
63
+ var pList = objType . GetProperties ( BindingFlags . Instance | BindingFlags . Public ) . ToList ( ) ;
64
+ pList . Sort ( ( x , y ) => string . Compare ( x . Name , y . Name , StringComparison . Ordinal ) ) ;
65
+
66
+ props = new List < controlProperty > ( ) ;
67
+ for ( int i = 0 ; i < pList . Count ; i ++ )
68
+ {
69
+ var p = pList [ i ] ;
70
+ var cp = new controlProperty ( )
71
+ {
72
+ info = p ,
73
+ } ;
74
+
75
+ props . Add ( cp ) ;
76
+ }
77
+
78
+ fields = objType . GetFields ( BindingFlags . Public | BindingFlags . Instance ) . ToList ( ) ;
79
+ fields . Sort ( ( x , y ) => x . Name . CompareTo ( y . Name ) ) ;
80
+
81
+ methods = objType . GetMethods ( BindingFlags . DeclaredOnly | BindingFlags . Instance | BindingFlags . Public ) . ToList ( ) ;
82
+ methods . Sort ( ( x , y ) => String . Compare ( x . Name , y . Name , StringComparison . Ordinal ) ) ;
83
+ for ( int i = 0 ; i < methods . Count ; i ++ )
84
+ {
85
+ var m = methods [ i ] ;
86
+ if ( m . GetParameters ( ) . Length == 0 && m . ReturnType == typeof ( void ) ) continue ;
87
+
88
+ methods . RemoveAt ( i ) ;
89
+ i -- ;
90
+ }
66
91
}
67
- else if ( ( type != null && type . IsArray ) || val is IEnumerable )
92
+
93
+ public Control Draw ( )
68
94
{
69
- p . expanded = Editor . Foldout ( p . info . Name , p . expanded ) ;
70
- if ( p . expanded )
95
+ Control control = null ;
96
+ if ( props . Count == 0 && methods . Count == 0 )
71
97
{
72
- var vEnum = val as IEnumerable ;
73
- foreach ( var e in vEnum )
98
+ Editor . Label ( name ) ;
99
+ return null ;
100
+ }
101
+
102
+ Editor . BeginVertical ( "Box" ) ;
103
+ toggleEditor = Editor . Foldout ( name , toggleEditor ) ;
104
+ if ( toggleEditor )
105
+ {
106
+ // Fields.
107
+ if ( fields . Count > 0 )
74
108
{
75
- var ec = e as Control ;
76
- if ( ec != null )
109
+ Editor . BeginVertical ( "Box" ) ;
110
+ for ( int i = 0 ; i < fields . Count ; i ++ )
77
111
{
78
- if ( Editor . Button ( ec . ToString ( ) ) )
79
- controlToSet = ec ;
112
+ var tc = Draw ( fields [ i ] ) ;
113
+ if ( tc != null )
114
+ control = tc ;
80
115
}
81
- else
82
- Editor . Label ( e ) ;
116
+ Editor . EndVertical ( ) ;
117
+
118
+ Editor . NewLine ( 1 ) ;
83
119
}
84
- }
85
- }
86
- else
87
- Editor . Label ( p . info . Name , val ) ;
88
120
89
- return controlToSet ;
90
- }
91
- private void Init ( )
92
- {
93
- var pList = Control . GetType ( ) . GetProperties ( BindingFlags . Instance | BindingFlags . Public ) . ToList ( ) ;
94
- pList . Sort ( ( x , y ) => string . Compare ( x . Name , y . Name , StringComparison . Ordinal ) ) ;
121
+ // Properties.
122
+ for ( int i = 0 ; i < props . Count ; i ++ )
123
+ {
124
+ var tc = Draw ( props [ i ] ) ;
125
+ if ( tc != null )
126
+ control = tc ;
127
+ }
95
128
96
- props = new List < controlProperty > ( ) ;
97
- for ( int i = 0 ; i < pList . Count ; i ++ )
98
- {
99
- var p = pList [ i ] ;
100
- var cp = new controlProperty ( )
101
- {
102
- info = p ,
103
- } ;
129
+ // Methods.
130
+ if ( methods . Count > 0 )
131
+ {
132
+ Editor . NewLine ( 1 ) ;
104
133
105
- props . Add ( cp ) ;
134
+ for ( int i = 0 ; i < methods . Count ; i ++ )
135
+ {
136
+ var tc = Draw ( methods [ i ] ) ;
137
+ if ( tc != null )
138
+ control = tc ;
139
+ }
140
+ }
141
+ }
142
+ Editor . EndVertical ( ) ;
143
+ return control ;
106
144
}
107
- }
145
+ public Control Draw ( controlProperty p )
146
+ {
147
+ Control controlToSet = null ;
148
+ var val = p . info . GetValue ( obj , null ) ;
149
+ Type type = null ;
150
+ if ( val != null )
151
+ type = val . GetType ( ) ;
108
152
109
- public virtual object Draw ( int width , int height )
110
- {
111
- if ( Control == null ) return null ;
112
- if ( props == null ) return null ;
153
+ // Array & List.
154
+ if ( val is string == false )
155
+ if ( ( type != null && type . IsArray ) || val is IEnumerable )
156
+ {
157
+ Editor . BeginVertical ( "Box" ) ;
158
+ p . expanded = Editor . Foldout ( p . info . Name , p . expanded ) ;
159
+ if ( p . expanded )
160
+ {
161
+ var vEnum = val as IEnumerable ;
162
+ var arrayIndex = 0 ;
163
+ foreach ( var e in vEnum )
164
+ {
165
+ var ec = e as Control ;
166
+ if ( ec != null )
167
+ {
168
+ if ( Editor . Button ( ec . ToString ( ) ) )
169
+ controlToSet = ec ;
170
+ }
171
+ else
172
+ {
173
+ if ( p . editor == null )
174
+ p . editor = new objectEditor ( e , arrayIndex . ToString ( ) ) ;
113
175
114
- Control controlToSet = null ;
176
+ p . editor . Draw ( ) ;
177
+ }
178
+ arrayIndex ++ ;
179
+ }
180
+ }
181
+ Editor . EndVertical ( ) ;
182
+ return controlToSet ;
183
+ }
115
184
116
- Editor . BeginGroup ( width - 24 ) ;
185
+ // If there is no Set() method then skip.
186
+ var pSetMethod = p . info . GetSetMethod ( true ) ;
187
+ if ( pSetMethod == null || pSetMethod . IsPrivate )
188
+ {
189
+ Editor . Label ( p . info . Name , val ) ;
190
+ return null ;
191
+ }
117
192
118
- toggleEditor = Editor . Foldout ( "Control (" + Control . GetType ( ) . Name + ")" , toggleEditor ) ;
119
- if ( toggleEditor )
120
- {
121
- for ( int i = 0 ; i < props . Count ; i ++ )
193
+ // Other editors.
194
+ if ( val is bool )
195
+ {
196
+ var bVal = ( bool ) val ;
197
+ var ebVal = Editor . BooleanField ( p . info . Name , bVal ) ;
198
+ if ( ebVal . Changed )
199
+ p . info . SetValue ( obj , ebVal . Value , null ) ;
200
+ }
201
+ else if ( val is Control )
202
+ {
203
+ var cVal = val as Control ;
204
+ if ( Editor . Button ( p . info . Name , cVal . GetType ( ) . Name ) )
205
+ controlToSet = cVal ;
206
+ }
207
+ else if ( val is Color )
208
+ {
209
+ var colorVal = ( Color ) val ;
210
+ Editor . ColorField ( p . info . Name , colorVal , c => p . info . SetValue ( obj , c , null ) ) ;
211
+ }
212
+ else if ( val is string )
122
213
{
123
- var p = props [ i ] ;
124
- var c = DrawProperty ( p ) ;
125
- if ( c != null )
126
- controlToSet = c ;
214
+ var stringtVal = ( string ) val ;
215
+ var esVal = Editor . TextField ( p . info . Name , stringtVal ) ;
216
+ if ( esVal . Changed )
217
+ p . info . SetValue ( obj , esVal . Value , null ) ;
127
218
}
219
+ else if ( val is int )
220
+ {
221
+ var eiVal = Editor . IntField ( p . info . Name , ( int ) val ) ;
222
+ if ( eiVal . Changed )
223
+ p . info . SetValue ( obj , eiVal . Value [ 0 ] , null ) ;
224
+ }
225
+ else if ( val is byte || val is sbyte || val is short || val is ushort || val is uint || val is long || val is ulong || val is float || val is double )
226
+ {
227
+ // TODO: editors for common types (like for int ^up there).
228
+ Editor . Label ( p . info . Name , val ) ;
229
+ }
230
+ else if ( val is Enum )
231
+ {
232
+ var eeVal = Editor . EnumField ( p . info . Name , ( Enum ) val ) ;
233
+ if ( eeVal . Changed )
234
+ p . info . SetValue ( obj , eeVal . Value , null ) ;
235
+ }
236
+ else if ( val != null )
237
+ {
238
+ if ( p . editor == null )
239
+ p . editor = new objectEditor ( val , p . info . Name ) ;
240
+
241
+ p . editor . Draw ( ) ;
242
+ }
243
+
244
+ return controlToSet ;
128
245
}
246
+ public Control Draw ( FieldInfo f )
247
+ {
248
+ // TODO: editors for fields.
129
249
130
- Editor . EndGroup ( ) ;
250
+ var val = f . GetValue ( obj ) ;
251
+ Editor . Label ( f . Name , val ) ;
131
252
132
- return controlToSet ;
133
- }
253
+ return null ;
254
+ }
255
+ public Control Draw ( MethodInfo m )
256
+ {
257
+ if ( Editor . Button ( m . Name ) )
258
+ {
259
+ m . Invoke ( obj , null ) ;
260
+ }
134
261
135
- private class controlProperty
136
- {
137
- public bool expanded ;
138
- public PropertyInfo info ;
262
+ return null ;
263
+ }
139
264
}
140
265
}
141
266
}
0 commit comments