1 ///
2 module glui.utils;
3 
4 import raylib;
5 import std.meta;
6 
7 import glui.style;
8 import glui.structs;
9 
10 @safe:
11 
12 /// Create a function to easily construct nodes.
13 template simpleConstructor(T) {
14 
15     T simpleConstructor(Args...)(Args args) {
16 
17         return new T(args);
18 
19     }
20 
21 }
22 
23 // lmao
24 // AliasSeq!(AliasSeq!(T...)) won't work, this is a workaround
25 // too lazy to document, used to generate node constructors with variadic or optional arguments.
26 alias BasicNodeParamLength = Alias!5;
27 template BasicNodeParam(int index) {
28 
29     static if (index == 0) alias BasicNodeParam = AliasSeq!(Layout, const Theme);
30     static if (index == 1) alias BasicNodeParam = AliasSeq!(const Theme, Layout);
31     static if (index == 2) alias BasicNodeParam = AliasSeq!(Layout);
32     static if (index == 3) alias BasicNodeParam = AliasSeq!(const Theme);
33     static if (index == 4) alias BasicNodeParam = AliasSeq!();
34 
35 }
36 
37 /// Check if the rectangle contains a point.
38 bool contains(Rectangle rectangle, Vector2 point) {
39 
40     return rectangle.x <= point.x
41         && point.x < rectangle.x + rectangle.width
42         && rectangle.y <= point.y
43         && point.y < rectangle.y + rectangle.height;
44 
45 }
46 
47 /// Get names of static fields in the given object.
48 template StaticFieldNames(T) {
49 
50     import std.traits : hasStaticMember;
51     import std.meta : Alias, Filter;
52 
53     // Prepare data
54     alias Members = __traits(allMembers, T);
55 
56     // Check if the said member is static
57     enum isStaticMember(alias member) =
58 
59         // Make sure this isn't an alias
60         __traits(compiles,
61             Alias!(__traits(getMember, T, member))
62         )
63 
64         // Find the member
65         && hasStaticMember!(T, member);
66 
67     // Result
68     alias StaticFieldNames = Filter!(isStaticMember, Members);
69 
70 }
71 
72 /// Get the current HiDPI scale. Returns Vector2(1, 1) if HiDPI is off.
73 Vector2 hidpiScale() @trusted {
74 
75     // HiDPI is on
76     return IsWindowState(ConfigFlags.FLAG_WINDOW_HIGHDPI)
77         ? GetWindowScaleDPI
78         : Vector2.one;
79 
80 }