1 module api.enums;
2 
3 import godotutil..string;
4 import api.classes, api.util;
5 
6 import asdf;
7 
8 import std.range;
9 import std.algorithm.searching, std.algorithm.iteration, std.algorithm.sorting;
10 import std.path;
11 import std.conv : text;
12 import std..string;
13 
14 string enumParent(string name)
15 {
16 	return name.splitEnumName[0];
17 }
18 
19 /// splits the name of an enum as obtained from the JSON into [class, enum] names.
20 string[2] splitEnumName(string type)
21 {
22 	string name = type[5..$];
23 	auto end = name.countUntil("::");
24 	if(end == -1) return [null, name]; // not a class
25 	return [name[0..end], name[end+2..$]];
26 }
27 
28 /// format the enum type for D.
29 string qualifyEnumName(string type)
30 {
31 	string[2] split = type.splitEnumName;
32 	if(!split[0]) return split[1].escapeD;
33 	return Type.get(split[0]).d~"."~split[1].escapeD;
34 }
35 
36 struct GodotEnum
37 {
38 	string name;
39 	int[string] values;
40 	
41 	@serializationIgnore:
42 	GodotClass parent;
43 	
44 	string[string] ddoc;
45 	
46 	string source() const
47 	{
48 		string ret = "\t/// \n\tenum "~name.escapeD~" : int\n\t{\n";
49 		
50 		foreach(n; values.keys.sort!((a, b)=>(values[a] < values[b])))
51 		{
52 			if(auto ptr = n in ddoc) ret ~= "\t\t/**\n\t\t" ~ (*ptr).replace("\n", "\n\t\t") ~ "\n\t\t*/\n";
53 			else ret ~= "\t\t/** */\n";
54 			ret ~= "\t\t"~n.snakeToCamel.escapeD~" = "~values[n].text~",\n";
55 		}
56 		
57 		ret ~= "\t}\n";
58 		return ret;
59 	}
60 }
61 
62