@@ -21,30 +21,67 @@ public static class ConnectionTypes
2121 private static Dictionary < string , TypeData > types ;
2222
2323 /// <summary>
24- /// Gets the Type the specified type name representates, if declared
24+ /// Gets the Type the specified identifier representates or , if not declared and checked, creates a new type data for the passed type
2525 /// </summary>
26- public static Type GetType ( string typeName )
26+ public static Type GetType ( string typeName , bool createIfNotDeclared )
2727 {
28- return GetTypeData ( typeName ) . Type ?? NullType ;
28+ TypeData data = GetTypeData ( typeName , createIfNotDeclared ) ;
29+ return data != null ? data . Type : NullType ;
2930 }
3031
3132 /// <summary>
32- /// Gets the type data for the specified type name , if declared
33+ /// Gets the type data with the specified identifier or , if not declared and checked, creates a new one when a valid type name with namespace is passed
3334 /// </summary>
34- public static TypeData GetTypeData ( string typeName )
35+ public static TypeData GetTypeData ( string typeName , bool createIfNotDeclared )
3536 {
3637 if ( types == null || types . Count == 0 )
3738 NodeEditor . ReInit ( false ) ;
3839 TypeData typeData ;
3940 if ( ! types . TryGetValue ( typeName , out typeData ) )
4041 {
41- Debug . LogError ( "No TypeData defined for: " + typeName ) ;
42- typeData = types . First ( ) . Value ;
42+ if ( createIfNotDeclared )
43+ {
44+ Type type = Type . GetType ( typeName ) ;
45+ if ( type == null )
46+ {
47+ typeData = types . First ( ) . Value ;
48+ Debug . LogError ( "No TypeData defined for: " + typeName + " and type could not be found either!" ) ;
49+ }
50+ else
51+ {
52+ typeData = new TypeData ( type ) ;
53+ types . Add ( typeName , typeData ) ;
54+ }
55+ }
56+ else
57+ {
58+ typeData = types . First ( ) . Value ;
59+ Debug . LogError ( "No TypeData defined for: " + typeName + "!" ) ;
60+ }
4361 }
44- if ( typeData . declaration == null || typeData . InputKnob == null || typeData . OutputKnob == null )
45- {
62+ return typeData ;
63+ }
64+
65+ /// <summary>
66+ /// Gets the type data for the specified type or, if not declared and checked, creates a new one for that type
67+ /// </summary>
68+ public static TypeData GetTypeData ( Type type , bool createIfNotDeclared )
69+ {
70+ if ( types == null || types . Count == 0 )
4671 NodeEditor . ReInit ( false ) ;
47- typeData = GetTypeData ( typeName ) ;
72+ TypeData typeData = types . Values . First ( ( TypeData tData ) => tData . Type == type ) ;
73+ if ( typeData == null )
74+ {
75+ if ( createIfNotDeclared )
76+ {
77+ typeData = new TypeData ( type ) ;
78+ types . Add ( type . FullName , typeData ) ;
79+ }
80+ else
81+ {
82+ typeData = types . First ( ) . Value ;
83+ Debug . LogError ( "No TypeData defined for: " + type . FullName + "!" ) ;
84+ }
4885 }
4986 return typeData ;
5087 }
@@ -54,60 +91,92 @@ public static TypeData GetTypeData (string typeName)
5491 /// </summary>
5592 internal static void FetchTypes ( )
5693 {
57- types = new Dictionary < string , TypeData > ( ) ;
94+ types = new Dictionary < string , TypeData > { { "None" , new TypeData ( ) } } ;
5895
59- List < Assembly > scriptAssemblies = AppDomain . CurrentDomain . GetAssemblies ( ) . Where ( ( Assembly assembly ) => assembly . FullName . Contains ( "Assembly" ) ) . ToList ( ) ;
60- if ( ! scriptAssemblies . Contains ( Assembly . GetExecutingAssembly ( ) ) )
61- scriptAssemblies . Add ( Assembly . GetExecutingAssembly ( ) ) ;
96+ IEnumerable < Assembly > scriptAssemblies = AppDomain . CurrentDomain . GetAssemblies ( ) . Where ( ( Assembly assembly ) => assembly . FullName . Contains ( "Assembly" ) ) ;
6297 foreach ( Assembly assembly in scriptAssemblies )
63- {
64- foreach ( Type type in assembly . GetTypes ( ) . Where ( T => T . IsClass && ! T . IsAbstract && T . GetInterfaces ( ) . Contains ( typeof ( ITypeDeclaration ) ) ) )
65- {
66- ITypeDeclaration typeDecl = assembly . CreateInstance ( type . FullName ) as ITypeDeclaration ;
98+ { // Iterate through each script assembly
99+ IEnumerable < Type > typeDeclarations = assembly . GetTypes ( ) . Where ( T => T . IsClass && ! T . IsAbstract && T . GetInterface ( typeof ( IConnectionTypeDeclaration ) . FullName ) != null ) ;
100+ foreach ( Type type in typeDeclarations )
101+ { // get all type declarations and create a typeData for them
102+ IConnectionTypeDeclaration typeDecl = assembly . CreateInstance ( type . FullName ) as IConnectionTypeDeclaration ;
67103 if ( typeDecl == null )
68104 throw new UnityException ( "Error with Type Declaration " + type . FullName ) ;
69- types . Add ( typeDecl . name , new TypeData ( typeDecl ) ) ;
105+ types . Add ( typeDecl . Identifier , new TypeData ( typeDecl ) ) ;
70106 }
71107 }
72108 }
73109 }
74110
75- public struct TypeData
111+ public class TypeData
76112 {
77- public ITypeDeclaration declaration ;
78- public Type Type ;
79- public Color col ;
80- public Texture2D InputKnob ;
81- public Texture2D OutputKnob ;
82-
83- public TypeData ( ITypeDeclaration typeDecl )
113+ private IConnectionTypeDeclaration declaration ;
114+ public Type Type { get ; private set ; }
115+ public Color Color { get ; private set ; }
116+ public Texture2D InKnobTex { get ; private set ; }
117+ public Texture2D OutKnobTex { get ; private set ; }
118+
119+ internal TypeData ( IConnectionTypeDeclaration typeDecl )
84120 {
85121 declaration = typeDecl ;
86122 Type = declaration . Type ;
87- col = declaration . col ;
123+ Color = declaration . Color ;
124+
125+ InKnobTex = ResourceManager . GetTintedTexture ( declaration . InKnobTex , Color ) ;
126+ OutKnobTex = ResourceManager . GetTintedTexture ( declaration . OutKnobTex , Color ) ;
127+
128+ if ( InKnobTex == null || InKnobTex == null )
129+ throw new UnityException ( "Invalid textures for default typeData " + declaration . Identifier + "!" ) ;
130+ }
131+
132+ internal TypeData ( Type type )
133+ {
134+ declaration = null ;
135+ Type = type ;
136+ Color = Color . white ; //(float)type.GetHashCode() / (int.MaxValue/3);
88137
89- InputKnob = ResourceManager . GetTintedTexture ( declaration . InputKnob_TexPath , col ) ;
90- OutputKnob = ResourceManager . GetTintedTexture ( declaration . OutputKnob_TexPath , col ) ;
138+ InKnobTex = ResourceManager . GetTintedTexture ( "Textures/In_Knob.png" , Color ) ;
139+ OutKnobTex = ResourceManager . GetTintedTexture ( "Textures/Out_Knob.png" , Color ) ;
140+
141+ if ( InKnobTex == null || InKnobTex == null )
142+ throw new UnityException ( "Invalid textures for default typeData " + type . ToString ( ) + "!" ) ;
143+ }
144+
145+ internal TypeData ( )
146+ {
147+ declaration = null ;
148+ Type = typeof ( object ) ;
149+ Color = Color . white ;
150+ InKnobTex = ResourceManager . LoadTexture ( "Textures/In_Knob.png" ) ;
151+ OutKnobTex = ResourceManager . LoadTexture ( "Textures/Out_Knob.png" ) ;
152+
153+ if ( InKnobTex == null || InKnobTex == null )
154+ throw new UnityException ( "Invalid textures for default typeData!" ) ;
155+ }
156+
157+ public bool isValid ( )
158+ {
159+ return Type != null && InKnobTex != null && OutKnobTex != null ;
91160 }
92161 }
93162
94- public interface ITypeDeclaration
163+ public interface IConnectionTypeDeclaration
95164 {
96- string name { get ; }
97- Color col { get ; }
98- string InputKnob_TexPath { get ; }
99- string OutputKnob_TexPath { get ; }
165+ string Identifier { get ; }
100166 Type Type { get ; }
167+ Color Color { get ; }
168+ string InKnobTex { get ; }
169+ string OutKnobTex { get ; }
101170 }
102171
103172 // TODO: Node Editor: Built-In Connection Types
104- public class FloatType : ITypeDeclaration
173+ public class FloatType : IConnectionTypeDeclaration
105174 {
106- public string name { get { return "Float" ; } }
107- public Color col { get { return Color . cyan ; } }
108- public string InputKnob_TexPath { get { return "Textures/In_Knob.png" ; } }
109- public string OutputKnob_TexPath { get { return "Textures/Out_Knob.png" ; } }
175+ public string Identifier { get { return "Float" ; } }
110176 public Type Type { get { return typeof ( float ) ; } }
177+ public Color Color { get { return Color . cyan ; } }
178+ public string InKnobTex { get { return "Textures/In_Knob.png" ; } }
179+ public string OutKnobTex { get { return "Textures/Out_Knob.png" ; } }
111180 }
112181
113182
0 commit comments