@@ -21,30 +21,67 @@ public static class ConnectionTypes
21
21
private static Dictionary < string , TypeData > types ;
22
22
23
23
/// <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
25
25
/// </summary>
26
- public static Type GetType ( string typeName )
26
+ public static Type GetType ( string typeName , bool createIfNotDeclared )
27
27
{
28
- return GetTypeData ( typeName ) . Type ?? NullType ;
28
+ TypeData data = GetTypeData ( typeName , createIfNotDeclared ) ;
29
+ return data != null ? data . Type : NullType ;
29
30
}
30
31
31
32
/// <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
33
34
/// </summary>
34
- public static TypeData GetTypeData ( string typeName )
35
+ public static TypeData GetTypeData ( string typeName , bool createIfNotDeclared )
35
36
{
36
37
if ( types == null || types . Count == 0 )
37
38
NodeEditor . ReInit ( false ) ;
38
39
TypeData typeData ;
39
40
if ( ! types . TryGetValue ( typeName , out typeData ) )
40
41
{
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
+ }
43
61
}
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 )
46
71
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
+ }
48
85
}
49
86
return typeData ;
50
87
}
@@ -54,60 +91,92 @@ public static TypeData GetTypeData (string typeName)
54
91
/// </summary>
55
92
internal static void FetchTypes ( )
56
93
{
57
- types = new Dictionary < string , TypeData > ( ) ;
94
+ types = new Dictionary < string , TypeData > { { "None" , new TypeData ( ) } } ;
58
95
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" ) ) ;
62
97
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 ;
67
103
if ( typeDecl == null )
68
104
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 ) ) ;
70
106
}
71
107
}
72
108
}
73
109
}
74
110
75
- public struct TypeData
111
+ public class TypeData
76
112
{
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 )
84
120
{
85
121
declaration = typeDecl ;
86
122
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);
88
137
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 ;
91
160
}
92
161
}
93
162
94
- public interface ITypeDeclaration
163
+ public interface IConnectionTypeDeclaration
95
164
{
96
- string name { get ; }
97
- Color col { get ; }
98
- string InputKnob_TexPath { get ; }
99
- string OutputKnob_TexPath { get ; }
165
+ string Identifier { get ; }
100
166
Type Type { get ; }
167
+ Color Color { get ; }
168
+ string InKnobTex { get ; }
169
+ string OutKnobTex { get ; }
101
170
}
102
171
103
172
// TODO: Node Editor: Built-In Connection Types
104
- public class FloatType : ITypeDeclaration
173
+ public class FloatType : IConnectionTypeDeclaration
105
174
{
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" ; } }
110
176
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" ; } }
111
180
}
112
181
113
182
0 commit comments