Duel centerprint title: slightly extend underline under the longest name, use the...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / centerprint.qc
1 #include "centerprint.qh"
2
3 #include <client/draw.qh>
4 #include <client/hud/panel/scoreboard.qh>
5 #include <common/notifications/all.qh>
6
7 // CenterPrint (#16)
8
9 void HUD_CenterPrint_Export(int fh)
10 {
11         // allow saving cvars that aesthetically change the panel into hud skin files
12         HUD_Write_Cvar("hud_panel_centerprint_align");
13         HUD_Write_Cvar("hud_panel_centerprint_flip");
14         HUD_Write_Cvar("hud_panel_centerprint_fontscale");
15         HUD_Write_Cvar("hud_panel_centerprint_fontscale_bold");
16         HUD_Write_Cvar("hud_panel_centerprint_time");
17         HUD_Write_Cvar("hud_panel_centerprint_fade_in");
18         HUD_Write_Cvar("hud_panel_centerprint_fade_out");
19         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent");
20         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passone");
21         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passone_minalpha");
22         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passtwo");
23         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passtwo_minalpha");
24         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_minfontsize");
25         HUD_Write_Cvar("hud_panel_centerprint_fade_minfontsize");
26 }
27
28 // These are the functions that draw the text at the center of the screen (e.g. frag messages and server MOTD).
29 // centerprint_Add parses a message and puts it in the circular buffer centerprint_messages
30 // centerprint_Add is usually called by Local_Notification_centerprint_Add, which is called
31 // by Local_Notification.
32 // HUD_CenterPrint draws all the messages on screen
33
34 const int CENTERPRINT_MAX_MSGS = 10;
35 const int CENTERPRINT_MAX_ENTRIES = 50;
36 const float CENTERPRINT_BASE_SIZE = 1.3;
37 const float CENTERPRINT_SPACING = 0.3;
38 const float CENTERPRINT_TITLE_SPACING = 0.35;
39 int cpm_index;
40 string centerprint_messages[CENTERPRINT_MAX_MSGS];
41 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
42 float centerprint_time[CENTERPRINT_MAX_MSGS];
43 float centerprint_start_time[CENTERPRINT_MAX_MSGS];
44 float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
45 int centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
46 bool centerprint_showing;
47
48 string centerprint_title;
49 string centerprint_title_left;
50 string centerprint_title_right;
51
52 void centerprint_Add(int new_id, string strMessage, float duration, int countdown_num)
53 {
54         TC(int, new_id); TC(int, countdown_num);
55         //LOG_INFOF("centerprint_Add: ^2id: %d ^3dur: %d ^5countdown: %d\n'%s'", new_id, duration, countdown_num, strreplace("\n", "^7\\n^7", strMessage));
56         int i, j;
57
58         if(strMessage == "" && new_id == 0)
59                 return;
60
61         // strip trailing newlines
62         j = strlen(strMessage) - 1;
63         while(substring(strMessage, j, 1) == "\n" && j >= 0)
64                 --j;
65         if (j < strlen(strMessage) - 1)
66                 strMessage = substring(strMessage, 0, j + 1);
67
68         if(strMessage == "" && new_id == 0)
69                 return;
70
71         // strip leading newlines
72         j = 0;
73         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
74                 ++j;
75         if (j > 0)
76                 strMessage = substring(strMessage, j, strlen(strMessage) - j);
77
78         if(strMessage == "" && new_id == 0)
79                 return;
80
81         if (!centerprint_showing)
82                 centerprint_showing = true;
83
84         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
85         {
86                 if (j == CENTERPRINT_MAX_MSGS)
87                         j = 0;
88                 if (new_id && new_id == centerprint_msgID[j])
89                 {
90                         if (strMessage == "" && centerprint_messages[j] != "")
91                         {
92                                 // fade out the current msg (duration and countdown_num are ignored)
93                                 centerprint_start_time[j] = 0;
94                                 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
95                                 centerprint_expire_time[j] = -1; // don't use the variable time here!
96                                 return;
97                         }
98                         break; // found a msg with the same id, at position j
99                 }
100         }
101
102         if (strMessage == "")
103                 return;
104
105         if (i == CENTERPRINT_MAX_MSGS)
106         {
107                 // a msg with the same id was not found, add the msg at the next position
108                 --cpm_index;
109                 if (cpm_index == -1)
110                         cpm_index = CENTERPRINT_MAX_MSGS - 1;
111                 j = cpm_index;
112         }
113         strcpy(centerprint_messages[j], strMessage);
114         centerprint_start_time[j] = time;
115         centerprint_msgID[j] = new_id;
116         if (duration < 0)
117         {
118                 centerprint_time[j] = -1;
119                 centerprint_expire_time[j] = -1; // don't use the variable time here!
120         }
121         else
122         {
123                 if(duration == 0)
124                         duration = max(1, autocvar_hud_panel_centerprint_time);
125                 centerprint_time[j] = duration;
126                 centerprint_expire_time[j] = -1; // don't use the variable time here!
127         }
128         centerprint_countdown_num[j] = countdown_num;
129 }
130
131 void centerprint_Kill(int id)
132 {
133         TC(int, id);
134         centerprint_Add(id, "", 0, 0);
135 }
136
137 void centerprint_AddStandard(string strMessage)
138 {
139         centerprint_Add(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
140 }
141
142 void centerprint_KillAll()
143 {
144         for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
145         {
146                 centerprint_start_time[i] = 0;
147                 centerprint_expire_time[i] = 0;
148                 centerprint_time[i] = 1;
149                 centerprint_msgID[i] = 0;
150                 strfree(centerprint_messages[i]);
151         }
152 }
153
154 void centerprint_SetDuelTitle(string left, string right)
155 {
156         float namesize = autocvar_hud_panel_scoreboard_namesize * hud_fontsize.x;
157         strcpy(centerprint_title_left, textShortenToWidth(left, namesize, hud_fontsize, stringwidth_colors));
158         strcpy(centerprint_title_right, textShortenToWidth(right, namesize, hud_fontsize, stringwidth_colors));
159 }
160
161 void centerprint_SetTitle(string title)
162 {
163         if(title != centerprint_title)
164                 strcpy(centerprint_title, CCR(title));
165 }
166
167 void centerprint_ClearTitle()
168 {
169         strfree(centerprint_title);
170         strfree(centerprint_title_left);
171         strfree(centerprint_title_right);
172 }
173
174 float hud_configure_cp_generation_time;
175 void HUD_CenterPrint()
176 {
177         if(!autocvar__hud_configure)
178         {
179                 if(!autocvar_hud_panel_centerprint) return;
180
181                 if(hud_configure_prev) {
182                         centerprint_ClearTitle();
183                         centerprint_KillAll();
184                 }
185         }
186         else
187         {
188                 if(!hud_configure_prev)
189                 {
190                         centerprint_KillAll();
191                         hud_configure_cp_generation_time = time; // show a message immediately
192                 }
193                 if (time > hud_configure_cp_generation_time)
194                 {
195                         if(highlightedPanel == HUD_PANEL(CENTERPRINT))
196                         {
197                                 centerprint_SetTitle(sprintf(_("Title at %s"), seconds_tostring(hud_configure_cp_generation_time)));
198
199                                 float r;
200                                 r = random();
201                                 if (r > 0.8)
202                                         centerprint_Add(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
203                                 else if (r > 0.55)
204                                         centerprint_Add(0, sprintf(_("^1Multiline message at time %s that\n^BOLDlasts longer than normal"), seconds_tostring(time)), 20, 0);
205                                 else
206                                         centerprint_AddStandard(sprintf(_("Message at time %s"), seconds_tostring(time)));
207                                 hud_configure_cp_generation_time = time + 1 + random()*4;
208                         }
209                         else
210                         {
211                                 centerprint_Add(0, _("Generic message"), 10, 0);
212                                 hud_configure_cp_generation_time = time + 10 - random()*3;
213                         }
214                 }
215         }
216
217         HUD_Panel_LoadCvars();
218
219         if ( HUD_Radar_Clickable() )
220         {
221                 if (hud_panel_radar_bottom >= vid_conheight)
222                         return;
223
224                 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
225                 panel_pos.y = hud_panel_radar_bottom;
226                 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
227         }
228         else if(!autocvar__hud_configure && scoreboard_fade_alpha)
229         {
230                 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
231                 if(target_pos.y > panel_pos.y)
232                 {
233                         panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
234                         panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
235                 }
236
237                 // move the panel below the scoreboard
238                 if (panel_pos.y >= vid_conheight)
239                         return;
240         }
241
242         if (autocvar_hud_panel_centerprint_dynamichud)
243                 HUD_Scale_Enable();
244         else
245                 HUD_Scale_Disable();
246         HUD_Panel_DrawBg();
247
248         if (!centerprint_showing)
249                 return;
250
251         if(panel_bg_padding)
252         {
253                 panel_pos += '1 1 0' * panel_bg_padding;
254                 panel_size -= '2 2 0' * panel_bg_padding;
255         }
256
257         int i, j, k, n, g;
258         float a = 1, sz, align, current_msg_posY = 0, msg_size;
259         vector pos;
260         vector cp_fontsize = hud_fontsize * CENTERPRINT_BASE_SIZE;
261         string ts = "";
262         bool all_messages_expired = true;
263
264         pos = panel_pos;
265         if (autocvar_hud_panel_centerprint_flip)
266                 pos.y += panel_size.y;
267         align = bound(0, autocvar_hud_panel_centerprint_align, 1);
268
269         // Show title if available
270         if(centerprint_title != "" || centerprint_title_left != "") {
271                 vector fontsize = cp_fontsize * autocvar_hud_panel_centerprint_fontscale_title;
272                 float width = 0, right_width = 0, left_width = 0,  max_rl_width = 0;
273                 if (centerprint_title != "")
274                 {
275                         width = stringwidth(centerprint_title, true, fontsize);
276                 }
277                 else
278                 {
279                         right_width = stringwidth(centerprint_title_right, true, fontsize);
280                         left_width = stringwidth(centerprint_title_left, true, fontsize);
281                 }
282
283                 if (autocvar_hud_panel_centerprint_flip)
284                         pos.y -= fontsize.y;
285
286                 vector duel_title_pos = '0 0 0';
287                 float padding = stringwidth(" ", false, fontsize) * 2;
288                 if (centerprint_title_left != "")
289                 {
290                         if (left_width > right_width)
291                                 max_rl_width = left_width;
292                         else
293                                 max_rl_width = right_width;
294
295                         width = max_rl_width * 2 + padding * 6 + stringwidth(_("vs"), false, fontsize);
296                         pos.x += (panel_size.x - width) * align;
297                         duel_title_pos = pos;
298
299                         pos.x += padding;
300                         if (left_width < right_width)
301                                 pos.x += (max_rl_width - left_width) * 0.5;
302                         drawcolorcodedstring(pos, centerprint_title_left, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
303
304                         pos.x = duel_title_pos.x + max_rl_width + padding * 3;
305                         drawstring(pos, _("vs"), fontsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
306
307                         pos.x = duel_title_pos.x + width - padding - max_rl_width;
308                         if (left_width >= right_width)
309                                 pos.x += (max_rl_width - right_width) * 0.5;
310                         drawcolorcodedstring(pos, centerprint_title_right, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
311                 }
312                 else
313                 {
314                         pos.x = panel_pos.x + (panel_size.x - width) * align;
315                         drawcolorcodedstring(pos, centerprint_title, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
316                 }
317
318                 if (autocvar_hud_panel_centerprint_flip)
319                         pos.y -= cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
320                 else
321                         pos.y += fontsize.y + (hud_fontsize.y * CENTERPRINT_TITLE_SPACING);
322
323                 if (centerprint_title_left != "")
324                 {
325                         pos.x = duel_title_pos.x;
326                         drawfill(pos, vec2(max_rl_width + padding * 2, 1), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
327                         drawfill(pos + vec2(width - max_rl_width - padding * 2, 0), vec2(max_rl_width + padding * 2, 1), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
328                 }
329                 else
330                         drawfill(pos, vec2(width, 1), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
331
332                 if (autocvar_hud_panel_centerprint_flip)
333                         pos.y -= cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
334                 else
335                         pos.y += cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
336
337                 all_messages_expired = false;
338         }
339
340         for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
341         {
342                 if (j == CENTERPRINT_MAX_MSGS)
343                         j = 0;
344                 if (centerprint_expire_time[j] == -1)
345                 {
346                         // here we are sure the time variable is not altered by CSQC_Ent_Update
347                         centerprint_expire_time[j] = time;
348                         if (centerprint_time[j] > 0)
349                                 centerprint_expire_time[j] += centerprint_time[j];
350                 }
351                 if (centerprint_expire_time[j] <= time)
352                 {
353                         if (centerprint_countdown_num[j] && centerprint_time[j] > 0 && centerprint_start_time[j])
354                         {
355                                 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
356                                 if (centerprint_countdown_num[j] == 0)
357                                         continue;
358                                 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
359                         }
360                         else if(centerprint_time[j] != -1)
361                                 continue;
362                 }
363
364                 all_messages_expired = false;
365
366                 if (time < centerprint_start_time[j]) continue;
367
368                 float fade_in_time = autocvar_hud_panel_centerprint_fade_in;
369                 float fade_out_time = autocvar_hud_panel_centerprint_fade_out;
370
371                 if (centerprint_countdown_num[j] && centerprint_start_time[j]) {
372                         fade_in_time = 0;
373                         fade_out_time = 0;
374                 }
375
376                 // fade
377                 if(fade_in_time && centerprint_start_time[j] && time < centerprint_start_time[j] + fade_in_time) // Fade in
378                         a = (time - centerprint_start_time[j]) / fade_in_time;
379                 else if(time < centerprint_expire_time[j] - fade_out_time || centerprint_time[j] < 0) // Regularily printed or forced
380                         a = 1;
381                 else if(fade_out_time) // Expiring soon, so fade it out.
382                         a = (centerprint_expire_time[j] - time) / fade_out_time;
383
384                 if(centerprint_msgID[j] == ORDINAL(CPID_TIMEIN))
385                         a = 1;
386
387                 // while counting down show it anyway in order to hold the current message position
388                 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0)  // Guaranteed invisible - don't show.
389                         continue;
390
391                 // also fade it based on positioning
392                 if(autocvar_hud_panel_centerprint_fade_subsequent)
393                 {
394                         // pass one: all messages after the first have half alpha
395                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
396                         // pass two: after that, gradually lower alpha even more for each message
397                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
398                 }
399                 a *= panel_fg_alpha;
400
401                 // finally set the size based on the alpha
402                 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
403                 drawfontscale = hud_scale * sz;
404
405                 if (centerprint_countdown_num[j])
406                         n = tokenizebyseparator(strreplace("^COUNT", ftos(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
407                 else
408                         n = tokenizebyseparator(centerprint_messages[j], "\n");
409
410                 if (autocvar_hud_panel_centerprint_flip)
411                 {
412                         // check if the message can be entirely shown
413                         for(k = 0; k < n; ++k)
414                         {
415                                 getWrappedLine_remaining = argv(k);
416                                 while(getWrappedLine_remaining)
417                                 {
418                                         bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
419                                         vector fontsize = cp_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
420
421                                         ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
422                                         if (ts != "")
423                                                 pos.y -= fontsize.y;
424                                         else
425                                                 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
426                                 }
427                         }
428                         current_msg_posY = pos.y; // save starting pos (first line) of the current message
429                 }
430
431                 msg_size = pos.y;
432                 for(k = 0; k < n; ++k)
433                 {
434                         getWrappedLine_remaining = argv(k);
435
436                         bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
437                         vector fontsize = cp_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
438                         if (is_bold)
439                                 getWrappedLine_remaining = substring(getWrappedLine_remaining, 5, -1);
440
441                         while(getWrappedLine_remaining)
442                         {
443                                 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
444                                 if (ts != "")
445                                 {
446                                         if (align)
447                                                 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
448                                         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.
449                                                 if (is_bold) draw_beginBoldFont();
450                                                 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
451                                                 if (is_bold) draw_endBoldFont();
452                                         pos.y += fontsize.y;
453                                 }
454                                 else
455                                         pos.y += fontsize.y * CENTERPRINT_SPACING/2;
456                         }
457                 }
458
459                 ++g; // move next position number up
460
461                 msg_size = pos.y - msg_size;
462
463                 if (autocvar_hud_panel_centerprint_flip)
464                 {
465                         pos.y -= msg_size + CENTERPRINT_SPACING * cp_fontsize.y;
466                         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
467                                 pos.y += (1 - sqrt(a));
468
469                         if (pos.y < panel_pos.y) // check if the next message can be shown
470                         {
471                                 drawfontscale = hud_scale;
472                                 return;
473                         }
474                 }
475                 else
476                 {
477                         pos.y += CENTERPRINT_SPACING * cp_fontsize.y;
478                         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
479                                 pos.y -= (1 - sqrt(a));
480
481                         if(pos.y > panel_pos.y + panel_size.y - cp_fontsize.y) // check if the next message can be shown
482                         {
483                                 drawfontscale = hud_scale;
484                                 return;
485                         }
486                 }
487         }
488
489         drawfontscale = hud_scale;
490         if (all_messages_expired)
491         {
492                 centerprint_showing = false;
493                 centerprint_KillAll();
494         }
495 }