]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/centerprint.qc
Client: Type check
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / centerprint.qc
1 #include "centerprint.qh"
2
3 #include <client/scoreboard.qh>
4
5 // CenterPrint (#16)
6
7 const int CENTERPRINT_MAX_MSGS = 10;
8 const int CENTERPRINT_MAX_ENTRIES = 50;
9 const float CENTERPRINT_SPACING = 0.7;
10 int cpm_index;
11 string centerprint_messages[CENTERPRINT_MAX_MSGS];
12 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
13 float centerprint_time[CENTERPRINT_MAX_MSGS];
14 float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
15 int centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
16 bool centerprint_showing;
17
18 void centerprint_generic(int new_id, string strMessage, float duration, int countdown_num)
19 {
20     TC(int, new_id); TC(int, countdown_num);
21         //printf("centerprint_generic(%d, '%s^7', %d, %d);\n", new_id, strMessage, duration, countdown_num);
22         int i, j;
23
24         if(strMessage == "" && new_id == 0)
25                 return;
26
27         // strip trailing newlines
28         j = strlen(strMessage) - 1;
29         while(substring(strMessage, j, 1) == "\n" && j >= 0)
30                 --j;
31         if (j < strlen(strMessage) - 1)
32                 strMessage = substring(strMessage, 0, j + 1);
33
34         if(strMessage == "" && new_id == 0)
35                 return;
36
37         // strip leading newlines
38         j = 0;
39         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
40                 ++j;
41         if (j > 0)
42                 strMessage = substring(strMessage, j, strlen(strMessage) - j);
43
44         if(strMessage == "" && new_id == 0)
45                 return;
46
47         if (!centerprint_showing)
48                 centerprint_showing = true;
49
50         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
51         {
52                 if (j == CENTERPRINT_MAX_MSGS)
53                         j = 0;
54                 if (new_id && new_id == centerprint_msgID[j])
55                 {
56                         if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
57                         {
58                                 // fade out the current msg (duration and countdown_num are ignored)
59                                 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
60                                 if (centerprint_expire_time[j] > time + min(5, autocvar_hud_panel_centerprint_fade_out) || centerprint_expire_time[j] < time)
61                                         centerprint_expire_time[j] = time + min(5, autocvar_hud_panel_centerprint_fade_out);
62                                 return;
63                         }
64                         break; // found a msg with the same id, at position j
65                 }
66         }
67
68         if (i == CENTERPRINT_MAX_MSGS)
69         {
70                 // a msg with the same id was not found, add the msg at the next position
71                 --cpm_index;
72                 if (cpm_index == -1)
73                         cpm_index = CENTERPRINT_MAX_MSGS - 1;
74                 j = cpm_index;
75         }
76         if(centerprint_messages[j])
77                 strunzone(centerprint_messages[j]);
78         centerprint_messages[j] = strzone(strMessage);
79         centerprint_msgID[j] = new_id;
80         if (duration < 0)
81         {
82                 centerprint_time[j] = -1;
83                 centerprint_expire_time[j] = time;
84         }
85         else
86         {
87                 if(duration == 0)
88                         duration = max(1, autocvar_hud_panel_centerprint_time);
89                 centerprint_time[j] = duration;
90                 centerprint_expire_time[j] = time + duration;
91         }
92         centerprint_countdown_num[j] = countdown_num;
93 }
94
95 void centerprint_kill(int id)
96 {
97     TC(int, id);
98         centerprint_generic(id, "", 0, 0);
99 }
100
101 void centerprint_hud(string strMessage)
102 {
103         centerprint_generic(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
104 }
105
106 void reset_centerprint_messages()
107 {
108         for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
109         {
110                 centerprint_expire_time[i] = 0;
111                 centerprint_time[i] = 1;
112                 centerprint_msgID[i] = 0;
113                 if(centerprint_messages[i])
114                         strunzone(centerprint_messages[i]);
115                 centerprint_messages[i] = string_null;
116         }
117 }
118 float hud_configure_cp_generation_time;
119 void HUD_CenterPrint ()
120 {
121         if(!autocvar__hud_configure)
122         {
123                 if(!autocvar_hud_panel_centerprint) return;
124
125                 if(hud_configure_prev)
126                         reset_centerprint_messages();
127         }
128         else
129         {
130                 if(!hud_configure_prev)
131                         reset_centerprint_messages();
132                 if (time > hud_configure_cp_generation_time)
133                 {
134                         if(highlightedPanel == HUD_PANEL(CENTERPRINT))
135                         {
136                                 float r;
137                                 r = random();
138                                 if (r > 0.8)
139                                         centerprint_generic(floor(r*1000), strcat(sprintf("^3Countdown message at time %s", seconds_tostring(time)), ", seconds left: ^COUNT"), 1, 10);
140                                 else if (r > 0.55)
141                                         centerprint_generic(0, sprintf("^1Multiline message at time %s that\n^1lasts longer than normal", seconds_tostring(time)), 20, 0);
142                                 else
143                                         centerprint_hud(sprintf("Message at time %s", seconds_tostring(time)));
144                                 hud_configure_cp_generation_time = time + 1 + random()*4;
145                         }
146                         else
147                         {
148                                 centerprint_generic(0, sprintf("Centerprint message", seconds_tostring(time)), 10, 0);
149                                 hud_configure_cp_generation_time = time + 10 - random()*3;
150                         }
151                 }
152         }
153
154         // this panel fades only when the menu does
155         float hud_fade_alpha_save = 0;
156         if(scoreboard_fade_alpha)
157         {
158                 hud_fade_alpha_save = hud_fade_alpha;
159                 hud_fade_alpha = 1 - autocvar__menu_alpha;
160         }
161         HUD_Panel_UpdateCvars();
162
163         if ( HUD_Radar_Clickable() )
164         {
165                 if (hud_panel_radar_bottom >= 0.96 * vid_conheight)
166                         return;
167
168                 panel_pos = eY * hud_panel_radar_bottom + eX * 0.5 * (vid_conwidth - panel_size_x);
169                 panel_size_y = min(panel_size_y, vid_conheight - hud_panel_radar_bottom);
170         }
171         else if(scoreboard_fade_alpha)
172         {
173                 hud_fade_alpha = hud_fade_alpha_save;
174
175                 // move the panel below the scoreboard
176                 if (scoreboard_bottom >= 0.96 * vid_conheight)
177                         return;
178                 vector target_pos;
179
180                 target_pos = eY * scoreboard_bottom + eX * 0.5 * (vid_conwidth - panel_size.x);
181
182                 if(target_pos.y > panel_pos.y)
183                 {
184                         panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
185                         panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
186                 }
187         }
188
189         HUD_Panel_DrawBg(1);
190
191         if (!centerprint_showing)
192                 return;
193
194         if(panel_bg_padding)
195         {
196                 panel_pos += '1 1 0' * panel_bg_padding;
197                 panel_size -= '2 2 0' * panel_bg_padding;
198         }
199
200         int entries;
201         float height;
202         vector fontsize;
203         // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
204         // height = panel_size_y/entries;
205         // fontsize = '1 1 0' * height;
206         height = vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale;
207         fontsize = '1 1 0' * height;
208         entries = bound(1, floor(panel_size.y/height), CENTERPRINT_MAX_ENTRIES);
209
210         int i, j, k, n, g;
211         float a, sz, align, current_msg_posY = 0, msg_size;
212         vector pos;
213         string ts;
214         bool all_messages_expired = true;
215
216         pos = panel_pos;
217         if (autocvar_hud_panel_centerprint_flip)
218                 pos.y += panel_size.y;
219         align = bound(0, autocvar_hud_panel_centerprint_align, 1);
220         for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
221         {
222                 if (j == CENTERPRINT_MAX_MSGS)
223                         j = 0;
224                 if (centerprint_expire_time[j] <= time)
225                 {
226                         if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
227                         {
228                                 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
229                                 if (centerprint_countdown_num[j] == 0)
230                                         continue;
231                                 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
232                         }
233                         else if(centerprint_time[j] != -1)
234                                 continue;
235                 }
236
237                 all_messages_expired = false;
238
239                 // fade the centerprint_hud in/out
240                 if(centerprint_time[j] < 0)  // Expired but forced. Expire time is the fade-in time.
241                         a = (time - centerprint_expire_time[j]) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
242                 else if(centerprint_expire_time[j] - autocvar_hud_panel_centerprint_fade_out > time)  // Regularily printed. Not fading out yet.
243                         a = (time - (centerprint_expire_time[j] - centerprint_time[j])) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
244                 else // Expiring soon, so fade it out.
245                         a = (centerprint_expire_time[j] - time) / max(0.0001, autocvar_hud_panel_centerprint_fade_out);
246
247                 // while counting down show it anyway in order to hold the current message position
248                 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0)  // Guaranteed invisible - don't show.
249                         continue;
250                 if (a > 1)
251                         a = 1;
252
253                 // set the size from fading in/out before subsequent fading
254                 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
255
256                 // also fade it based on positioning
257                 if(autocvar_hud_panel_centerprint_fade_subsequent)
258                 {
259                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1); // pass one: all messages after the first have half theAlpha
260                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1); // pass two: after that, gradually lower theAlpha even more for each message
261                 }
262                 a *= panel_fg_alpha;
263
264                 // finally set the size based on the new theAlpha from subsequent fading
265                 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
266                 drawfontscale = sz * '1 1 0';
267
268                 if (centerprint_countdown_num[j])
269                         n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
270                 else
271                         n = tokenizebyseparator(centerprint_messages[j], "\n");
272
273                 if (autocvar_hud_panel_centerprint_flip)
274                 {
275                         // check if the message can be entirely shown
276                         for(k = 0; k < n; ++k)
277                         {
278                                 getWrappedLine_remaining = argv(k);
279                                 while(getWrappedLine_remaining)
280                                 {
281                                         ts = getWrappedLine(panel_size.x * sz, fontsize, stringwidth_colors);
282                                         if (ts != "")
283                                                 pos.y -= fontsize.y;
284                                         else
285                                                 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
286                                 }
287                         }
288                         current_msg_posY = pos.y; // save starting pos (first line) of the current message
289                 }
290
291                 msg_size = pos.y;
292                 for(k = 0; k < n; ++k)
293                 {
294                         getWrappedLine_remaining = argv(k);
295                         while(getWrappedLine_remaining)
296                         {
297                                 ts = getWrappedLine(panel_size.x * sz, fontsize, stringwidth_colors);
298                                 if (ts != "")
299                                 {
300                                         if (align)
301                                                 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize)) * align;
302                                         if (a > 0.5/255.0)  // Otherwise guaranteed invisible - don't show. This is checked a second time after some multiplications with other factors were done so temporary changes of these cannot cause flicker.
303                                                 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
304                                         pos.y += fontsize.y;
305                                 }
306                                 else
307                                         pos.y += fontsize.y * CENTERPRINT_SPACING/2;
308                         }
309                 }
310
311                 ++g; // move next position number up
312
313                 msg_size = pos.y - msg_size;
314                 if (autocvar_hud_panel_centerprint_flip)
315                 {
316                         pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
317                         if (a < 1 && centerprint_msgID[j] == 0) // messages with id can be replaced just after they are faded out, so never move over them the next messages
318                                 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
319
320                         if (pos.y < panel_pos.y) // check if the next message can be shown
321                         {
322                                 drawfontscale = '1 1 0';
323                                 return;
324                         }
325                 }
326                 else
327                 {
328                         pos.y += CENTERPRINT_SPACING * fontsize.y;
329                         if (a < 1 && centerprint_msgID[j] == 0) // messages with id can be replaced just after they are faded out, so never move over them the next messages
330                                 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
331
332                         if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
333                         {
334                                 drawfontscale = '1 1 0';
335                                 return;
336                         }
337                 }
338         }
339         drawfontscale = '1 1 0';
340         if (all_messages_expired)
341         {
342                 centerprint_showing = false;
343                 reset_centerprint_messages();
344         }
345 }