1 module godot.util.generator.enums;
2 
3 import godot.util.tools.string;
4 import godot.util.generator.classes, godot.util.generator.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     return name.splitEnumName[0];
16 }
17 
18 /// splits the name of an enum as obtained from the JSON into [class, enum] names.
19 string[2] splitEnumName(string type) {
20     // skip 'enum::' part
21     string name = type[6 .. $];
22     auto end = name.countUntil("."); // enum:: arleady skipped, now look for scope qualifier e.g. TextServer.Hinting 
23     if (end == -1)
24         return [null, name]; // not a class
25     return [name[0 .. end], name[end + 1 .. $]];
26 }
27 
28 /// format the enum type for D.
29 string qualifyEnumName(string type) {
30     string[2] split = type.splitEnumName;
31     if (!split[0])
32         return split[1].escapeD;
33     return Type.get(split[0]).dType ~ "." ~ split[1].escapeD;
34 }
35 
36 struct EnumValues {
37     string name;
38     int value;
39 }
40 
41 struct GodotEnum {
42     string name;
43     EnumValues[] values;
44     @serdeOptional bool is_bitfield;
45 
46 @serdeIgnore:
47     GodotClass parent;
48 
49     string[string] ddoc;
50 
51     string source() const {
52         string ret = "\t/// \n\tenum " ~ name.escapeD ~ " : int\n\t{\n";
53 
54         foreach (n; values /*.sort!((a, b)=>(a.value < b.value))*/ ) {
55             if (auto ptr = n.name in ddoc)
56                 ret ~= "\t\t/**\n\t\t" ~ (*ptr).replace("\n", "\n\t\t") ~ "\n\t\t*/\n";
57             else
58                 ret ~= "\t\t/** */\n";
59             ret ~= "\t\t" ~ n.name.snakeToCamel.escapeD ~ " = " ~ n.value.text ~ ",\n";
60         }
61 
62         ret ~= "\t}\n";
63         return ret;
64     }
65 }