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