1 module unecht.core.componentManager;
2 
3 version(UEIncludeEditor):
4 
5 import unecht.core.object;
6 import unecht.core.components.editor.menus:EditorMenuItem;
7 
8 //TODO: get rid using new editor creation
9 ///
10 mixin template UERegisterInspector(T)
11 {
12     shared static this()
13     {
14         import unecht.meta.uda:getUDA;
15         
16         enum componentName = getUDA!(T,EditorInspector).componentName;
17 
18         UEComponentsManager.editors[componentName] = new T();
19     }
20 }
21 
22 /// UDA
23 struct EditorInspector
24 {
25     ///
26     string componentName;
27 }
28 
29 ///
30 interface IComponentEditor
31 {	
32     ///
33 	bool render(UEObject _component);
34 }
35 
36 ///
37 static struct UEComponentsManager
38 {
39     ///
40 	static IComponentEditor[string] editors;
41     ///
42     static string[] componentNames;
43     ///
44     version(UEIncludeEditor)static EditorMenuItem[] menuItems;
45 
46     ///
47     static void initComponentManager()
48     {
49         import unecht.core.component;
50         import std.stdio;
51 
52         auto tid = typeid(UEObject);
53 
54         foreach(m; ModuleInfo)
55         {
56             //writefln("scan mod: %s",m.name);
57 
58             foreach(cla; m.localClasses)
59             {
60                 if(hasBaseClass(cla, tid))
61                 {
62                     //writefln("obj: %s",cla.name);
63 
64                     scope obj = cast(UEObject)cla.create();
65                     if(obj)
66                     {
67                         auto editor = obj.createEditor();
68                         if(editor)
69                         {
70                             // only add editor if not already registered by a custom implementation
71                             if(!(obj.typename in UEComponentsManager.editors))
72                                 UEComponentsManager.editors[obj.typename] = editor;
73                         }
74                     }
75 
76                     if(hasBaseClass(cla, typeid(UEComponent)))
77                     {
78                         scope comp = cast(UEComponent)cla.create();
79                         if(comp)
80                         {
81                             UEComponentsManager.componentNames ~= cla.name;
82                             comp.getMenuItems(UEComponentsManager.menuItems);
83                         }
84                     }
85                 }
86             }       
87         }
88     }
89 }
90 
91 ///
92 private bool hasBaseClass(in TypeInfo_Class v, in TypeInfo_Class base) pure nothrow
93 {
94     if(v is base)
95         return true;
96     else if(v && v !is typeid(Object))
97         return hasBaseClass(v.base, base);
98     else
99         return false;
100 }