]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/self.qh
Introduce touch accessors
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / self.qh
1 #pragma once
2
3 // Transition from global 'self' to local 'this'
4
5 // Step 1: auto oldself
6 #if 1
7     #define SELFPARAM() noref const entity this = self
8 #endif
9
10 // Step 2: const self
11 #if 1
12     #define self (0, self)
13     [[alias("self")]] entity __self;
14     #define setself(s) (__self = s)
15     #define WITHSELF(value, block) WITH(entity, __self, value, (0, block))
16 #endif
17
18 // Step 3: propagate SELFPARAM()
19 #if 1
20     #undef self
21     #define self (this, self)
22     #undef SELFPARAM
23     #define SELFPARAM() const entity this = __self
24 #endif
25
26 // Step 4: kill unstructured setself
27 #if 0
28     #undef setself
29 #endif
30
31 // Step 5: this should work
32 #if 0
33     #undef self
34     #define self (0, this)
35 #endif
36
37 // Step 6: remove SELFPARAM, add parameters
38 #if 0
39     #undef SELFPARAM
40 #endif
41
42 // Step 7: remove WITHSELF, no replacement
43 #if 0
44     #undef WITHSELF
45     #define WITHSELF(value, block) block
46 #endif
47
48 #define SELFWRAP(T, R, args, forward) \
49     .R() T; \
50     .R() __##T = T; \
51     .R args self##T; \
52     R T##_self() { SELFPARAM(); return this.self##T forward; }
53
54 noref entity _selftemp;
55 #define SELFWRAP_SET(T, e, f) \
56     (_selftemp = (e), _selftemp.__##T = ((f) ? T##_self : func_null), _selftemp.self##T = (f))
57 #define SELFWRAP_GET(T, e) \
58     (0, (e).self##T)
59 #define _SELFWRAP_SET(T, e, f) \
60     ((e).__##T = (f))
61 #define _SELFWRAP_GET(T, e) \
62     (0, (e).__##T)
63
64 SELFWRAP(think, void, (entity this), (this))
65 #define setthink(e, f) SELFWRAP_SET(think, e, f)
66
67 SELFWRAP(touch, void, (entity this), (this))
68 #define settouch(e, f) _SELFWRAP_SET(touch, e, f)
69 #define gettouch(e) _SELFWRAP_GET(touch, e)
70
71 SELFWRAP(predraw, void, (entity this), (this))
72 #define setpredraw(e, f) SELFWRAP_SET(predraw, e, f)
73
74 #ifndef MENUQC
75 void adaptor_think2touch() { SELFPARAM(); WITH(entity, other, NULL, gettouch(this)()); }
76 void adaptor_think2use() { SELFPARAM(); if (this.use) this.use(this, NULL, NULL); }
77 #endif