db1dfcf6ac663166fb0fa1ec261f1c4d19295c78
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / oo / base.qh
1 #ifndef BASE_H
2 #define BASE_H
3
4 #include "../../common/util.qh"
5 #include "../../dpdefs/keycodes.qh"
6
7 #define NULL (null_entity)
8 #define world NULL
9
10 .string classname;
11 .string vtblname;
12 .entity vtblbase;
13 entity spawnVtbl(entity this, entity base)
14 {
15         entity vtbl = spawn();
16         copyentity(this, vtbl);
17         vtbl.vtblname = vtbl.classname;
18         vtbl.classname = "vtbl";
19         vtbl.vtblbase = base ? base : vtbl; // Top level objects use vtbl as base
20         return vtbl;
21 }
22
23 entity Object_vtbl;
24 entity spawnObject(entity this, entity)
25 {
26         this = spawn();
27         this.classname = "Object";
28         if (!Object_vtbl) Object_vtbl = spawnVtbl(this, null_entity);
29         return this;
30 }
31
32 #define NEW(cname) (spawn##cname(null_entity, null_entity))
33
34 #define CLASS(cname, base)                                          \
35 entity spawn##cname(entity this, entity basevtbl) {                 \
36     this = NEW(base); basevtbl = base##_vtbl;                       \
37 }
38
39 #define METHOD(cname, name, prototype)                              \
40 prototype cname##_##name;                                           \
41 .prototype name;                                                    \
42 [[accumulate]] entity spawn##cname(entity this, entity basevtbl) {  \
43     this.name = cname##_##name;                                     \
44 }
45
46 #define ATTRIB(cname, name, type, val)                              \
47 .type name;                                                         \
48 [[accumulate]] entity spawn##cname(entity this, entity basevtbl) {  \
49     this.name = val;                                                \
50 }
51
52 #define ATTRIBARRAY(cname, name, type, cnt)                         \
53 .type name[cnt];
54
55 #define ENDCLASS(cname)                                             \
56 .bool instanceOf##cname;                                            \
57 entity cname##_vtbl;                                                \
58 [[last]] entity spawn##cname(entity this, entity basevtbl) {        \
59     this.instanceOf##cname = true;                                  \
60     this.classname = #cname;                                        \
61     if (!cname##_vtbl) cname##_vtbl = spawnVtbl(this, basevtbl);    \
62     return this;                                                    \
63 }
64
65 #define SUPER(cname) (cname##_vtbl.vtblbase)
66
67 #endif