@@ -11,7 +11,9 @@ namespace GraphProcessor
1111{
1212 public static class FieldFactory
1313 {
14- static Dictionary < Type , Type > fieldDrawers = new Dictionary < Type , Type > ( ) ;
14+ static readonly Dictionary < Type , Type > fieldDrawers = new Dictionary < Type , Type > ( ) ;
15+
16+ static readonly MethodInfo createFieldMethod = typeof ( FieldFactory ) . GetMethod ( "CreateFieldSpecific" , BindingFlags . Static | BindingFlags . Public ) ;
1517
1618 static FieldFactory ( )
1719 {
@@ -26,8 +28,9 @@ static FieldFactory()
2628 }
2729
2830 // щ(ºДºщ) ...
29- AddDrawer ( typeof ( int ) , typeof ( IntegerField ) ) ;
30- AddDrawer ( typeof ( float ) , typeof ( DoubleField ) ) ;
31+ AddDrawer ( typeof ( int ) , typeof ( IntegerField ) ) ;
32+ AddDrawer ( typeof ( long ) , typeof ( LongField ) ) ;
33+ AddDrawer ( typeof ( float ) , typeof ( FloatField ) ) ;
3134 AddDrawer ( typeof ( double ) , typeof ( DoubleField ) ) ;
3235 AddDrawer ( typeof ( string ) , typeof ( TextField ) ) ;
3336 AddDrawer ( typeof ( Bounds ) , typeof ( BoundsField ) ) ;
@@ -42,34 +45,55 @@ static FieldFactory()
4245
4346 static void AddDrawer ( Type fieldType , Type drawerType )
4447 {
45- if ( ! drawerType . IsSubclassOf ( typeof ( INotifyValueChanged < > ) ) )
48+ var iNotifyType = typeof ( INotifyValueChanged < > ) . MakeGenericType ( fieldType ) ;
49+
50+ if ( ! iNotifyType . IsAssignableFrom ( drawerType ) )
4651 {
47- Debug . LogError ( "The custom field drawer " + drawerType + " does not implements INotifyValueChanged< T >" ) ;
52+ Debug . LogError ( "The custom field drawer " + drawerType + " does not implements INotifyValueChanged< " + fieldType + " >") ;
4853 return ;
4954 }
5055
5156 fieldDrawers [ fieldType ] = drawerType ;
5257 }
5358
5459 public static INotifyValueChanged < T > CreateField < T > ( )
60+ {
61+ return CreateField ( typeof ( T ) ) as INotifyValueChanged < T > ;
62+ }
63+
64+ public static VisualElement CreateField ( Type t )
5565 {
5666 Type drawerType ;
5767
58- fieldDrawers . TryGetValue ( typeof ( T ) , out drawerType ) ;
68+ fieldDrawers . TryGetValue ( t , out drawerType ) ;
5969
6070 if ( drawerType == null )
61- {
62- throw new ArgumentException ( "Can't find field drawer for type" + typeof ( T ) ) ;
63- }
71+ drawerType = fieldDrawers . FirstOrDefault ( kp => kp . Key . IsReallyAssignableFrom ( t ) ) . Value ;
72+
73+ if ( drawerType == null )
74+ throw new ArgumentException ( "Can't find field drawer for type: " + t ) ;
6475
6576 var field = Activator . CreateInstance ( drawerType ) ;
66- return field as INotifyValueChanged < T > ;
77+
78+ return field as VisualElement ;
79+ }
80+
81+ public static INotifyValueChanged < T > CreateFieldSpecific < T > ( FieldInfo field , Action < object > onValueChanged )
82+ {
83+ var fieldDrawer = CreateField < T > ( ) ;
84+
85+ fieldDrawer . OnValueChanged ( ( e ) => {
86+ onValueChanged ( e . newValue ) ;
87+ } ) ;
88+
89+ return fieldDrawer as INotifyValueChanged < T > ;
6790 }
6891
69- public static VisualElement CreateField ( FieldInfo field )
92+ public static VisualElement CreateField ( FieldInfo field , Action < object > onValueChanged )
7093 {
71- //TODO: Create new field drawer and attach chnage events to this field
72- return null ;
94+ var createFieldSpecificMethod = createFieldMethod . MakeGenericMethod ( field . FieldType ) ;
95+
96+ return createFieldSpecificMethod . Invoke ( null , new object [ ] { field , onValueChanged } ) as VisualElement ;
7397 }
7498 }
7599}
0 commit comments