-
Notifications
You must be signed in to change notification settings - Fork 2
/
pmenu.qc
176 lines (136 loc) · 3.1 KB
/
pmenu.qc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#ifdef PMENU
void(entity ent) PMenu_Update =
{
if (!ent.client.menu.open)
{
gi.dprintf("warning: ent has no menu\n");
return;
}
string str = "xv 32 yv 8 picn inventory ";
for (int i = 0; i < ent.client.menu.num; i++)
{
pmenu_t *entry = &ent.client.menu.entries[i];
if (!entry->text)
continue; // blank line
string t = entry->text;
bool alt;
if (strat(t, 0) == '*')
{
alt = true;
t = substr(t, 1);
}
else
alt = false;
str = strconcat(str, "yv ", itos(32 + i * 8), " ");
int x;
if (entry->align == PMENU_ALIGN_CENTER)
x = 196/2 - strlen(t)*4 + 64;
else if (entry->align == PMENU_ALIGN_RIGHT)
x = 64 + (196 - strlen(t)*8);
else
x = 64;
str = strconcat(str, "xv ", itos(x - ((ent.client.menu.cur == i) ? 8 : 0)), " ");
if (ent.client.menu.cur == i)
str = strconcat(str, "string2 \"\x0d", t, "\" ");
else if (alt)
str = strconcat(str, "string2 \"", t, "\" ");
else
str = strconcat(str, "string \"", t, "\" ");
if (strlen(str) >= MESSAGE_LIMIT)
{
gi.dprintf("menu message length overflow\n");
return;
}
}
gi.WriteByte(svc_layout);
gi.WriteString(str);
gi.unicast(ent, true);
}
void(entity ent) PMenu_Close =
{
if (!ent.client.menu.open)
return;
ent.client.menu.open = false;
ent.client.showscores = false;
memclear(&ent.client.menu.entries[0], sizeof(pmenu_t) * 18);
}
void(entity ent, pmenu_t *entries, int num, int cur = -1, void *arg = 0) PMenu_Open =
{
if (!ent.is_client)
return;
if (ent.client.menu.open)
{
gi.dprintf("warning: ent already has a menu\n");
PMenu_Close(ent);
}
memcpy(&ent.client.menu.entries[0], entries, sizeof(pmenu_t) * num);
ent.client.menu.num = num;
int i = cur;
if (cur < 0 || !entries[cur].func)
for (i = 0; i < num; i++)
if (entries[i].func)
break;
if (i >= num)
ent.client.menu.cur = -1;
else
ent.client.menu.cur = i;
ent.client.showscores = true;
ent.client.menu.open = true;
ent.client.showinventory = false;
#ifdef SINGLE_PLAYER
ent.client.showhelp = false;
#endif
PMenu_Update(ent);
}
inline void(pmenu_t *entry, string text, int align, pmenu_select_func func) PMenu_UpdateEntry =
{
entry->text = text;
entry->align = align;
entry->func = func;
}
void(entity ent, int offset) PMenu_Move =
{
if (!ent.client.menu.open)
{
gi.dprintf("warning: ent has no menu\n");
return;
}
if (ent.client.menu.cur < 0)
return; // no selectable entries
int i = ent.client.menu.cur;
do
{
i += offset;
if (i < 0)
i = ent.client.menu.num - 1;
else if (i >= ent.client.menu.num)
i = 0;
pmenu_t *p = &ent.client.menu.entries[i];
if (p->func)
break;
} while (i != ent.client.menu.cur);
ent.client.menu.cur = i;
PMenu_Update(ent);
}
inline void(entity ent) PMenu_Next =
{
return PMenu_Move(ent, 1);
}
inline void(entity ent) PMenu_Prev =
{
return PMenu_Move(ent, -1);
}
void(entity ent) PMenu_Select =
{
if (!ent.client.menu.open)
{
gi.dprintf("warning: ent has no menu\n");
return;
}
if (ent.client.menu.cur < 0)
return; // no selectable entries
pmenu_t *p = &ent.client.menu.entries[ent.client.menu.cur];
if (p->func)
p->func(ent);
}
#endif