]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/centerprint.qc
Merge branch 'bones_was_here/jlhooks' into 'master'
[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 float centerprint_title_offset;
50
51 void centerprint_Add(int new_id, string strMessage, float duration, int countdown_num)
52 {
53         TC(int, new_id); TC(int, countdown_num);
54         //LOG_INFOF("centerprint_Add: ^2id: %d ^3dur: %d ^5countdown: %d\n'%s'", new_id, duration, countdown_num, strreplace("\n", "^7\\n^7", strMessage));
55         int i, j;
56
57         if(strMessage == "" && new_id == 0)
58                 return;
59
60         // strip trailing newlines
61         j = strlen(strMessage) - 1;
62         while(substring(strMessage, j, 1) == "\n" && j >= 0)
63                 --j;
64         if (j < strlen(strMessage) - 1)
65                 strMessage = substring(strMessage, 0, j + 1);
66
67         if(strMessage == "" && new_id == 0)
68                 return;
69
70         // strip leading newlines
71         j = 0;
72         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
73                 ++j;
74         if (j > 0)
75                 strMessage = substring(strMessage, j, strlen(strMessage) - j);
76
77         if(strMessage == "" && new_id == 0)
78                 return;
79
80         if (!centerprint_showing)
81                 centerprint_showing = true;
82
83         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
84         {
85                 if (j == CENTERPRINT_MAX_MSGS)
86                         j = 0;
87                 if (new_id && new_id == centerprint_msgID[j])
88                 {
89                         if (strMessage == "" && centerprint_messages[j] != "")
90                         {
91                                 // fade out the current msg (duration and countdown_num are ignored)
92                                 centerprint_start_time[j] = 0;
93                                 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
94                                 centerprint_expire_time[j] = -1; // don't use the variable time here!
95                                 return;
96                         }
97                         break; // found a msg with the same id, at position j
98                 }
99         }
100
101         if (strMessage == "")
102                 return;
103
104         if (i == CENTERPRINT_MAX_MSGS)
105         {
106                 // a msg with the same id was not found, add the msg at the next position
107                 --cpm_index;
108                 if (cpm_index == -1)
109                         cpm_index = CENTERPRINT_MAX_MSGS - 1;
110                 j = cpm_index;
111         }
112         strcpy(centerprint_messages[j], strMessage);
113         centerprint_start_time[j] = time;
114         centerprint_msgID[j] = new_id;
115         if (duration < 0)
116         {
117                 centerprint_time[j] = -1;
118                 centerprint_expire_time[j] = -1; // don't use the variable time here!
119         }
120         else
121         {
122                 if(duration == 0)
123                         duration = max(1, autocvar_hud_panel_centerprint_time);
124                 centerprint_time[j] = duration;
125                 centerprint_expire_time[j] = -1; // don't use the variable time here!
126         }
127         centerprint_countdown_num[j] = countdown_num;
128 }
129
130 void centerprint_Kill(int id)
131 {
132         TC(int, id);
133         centerprint_Add(id, "", 0, 0);
134 }
135
136 void centerprint_AddStandard(string strMessage)
137 {
138         centerprint_Add(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
139 }
140
141 void centerprint_KillAll()
142 {
143         for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
144         {
145                 centerprint_start_time[i] = 0;
146                 centerprint_expire_time[i] = 0;
147                 centerprint_time[i] = 1;
148                 centerprint_msgID[i] = 0;
149                 strfree(centerprint_messages[i]);
150         }
151 }
152
153 void centerprint_ClearTitle()
154 {
155         strfree(centerprint_title);
156         centerprint_title_offset = 0;
157 }
158
159 void centerprint_SetTitle(string title, float offset)
160 {
161         if(title != centerprint_title) {
162                 strcpy(centerprint_title, CCR(title));
163                 centerprint_title_offset = offset;
164         }
165 }
166
167 float hud_configure_cp_generation_time;
168 void HUD_CenterPrint()
169 {
170         if(!autocvar__hud_configure)
171         {
172                 if(!autocvar_hud_panel_centerprint) return;
173
174                 if(hud_configure_prev) {
175                         centerprint_ClearTitle();
176                         centerprint_KillAll();
177                 }
178         }
179         else
180         {
181                 if(!hud_configure_prev)
182                 {
183                         centerprint_KillAll();
184                         hud_configure_cp_generation_time = time; // show a message immediately
185                 }
186                 if (time > hud_configure_cp_generation_time)
187                 {
188                         if(highlightedPanel == HUD_PANEL(CENTERPRINT))
189                         {
190                                 centerprint_SetTitle(sprintf(_("Title at %s"), seconds_tostring(hud_configure_cp_generation_time)), 0);
191
192                                 float r;
193                                 r = random();
194                                 if (r > 0.8)
195                                         centerprint_Add(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
196                                 else if (r > 0.55)
197                                         centerprint_Add(0, sprintf(_("^1Multiline message at time %s that\n^BOLDlasts longer than normal"), seconds_tostring(time)), 20, 0);
198                                 else
199                                         centerprint_AddStandard(sprintf(_("Message at time %s"), seconds_tostring(time)));
200                                 hud_configure_cp_generation_time = time + 1 + random()*4;
201                         }
202                         else
203                         {
204                                 centerprint_Add(0, _("Generic message"), 10, 0);
205                                 hud_configure_cp_generation_time = time + 10 - random()*3;
206                         }
207                 }
208         }
209
210         HUD_Panel_LoadCvars();
211
212         if ( HUD_Radar_Clickable() )
213         {
214                 if (hud_panel_radar_bottom >= vid_conheight)
215                         return;
216
217                 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
218                 panel_pos.y = hud_panel_radar_bottom;
219                 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
220         }
221         else if(!autocvar__hud_configure && scoreboard_fade_alpha)
222         {
223                 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
224                 if(target_pos.y > panel_pos.y)
225                 {
226                         panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
227                         panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
228                 }
229
230                 // move the panel below the scoreboard
231                 if (panel_pos.y >= vid_conheight)
232                         return;
233         }
234
235         if (autocvar_hud_panel_centerprint_dynamichud)
236                 HUD_Scale_Enable();
237         else
238                 HUD_Scale_Disable();
239         HUD_Panel_DrawBg();
240
241         if (!centerprint_showing)
242                 return;
243
244         if(panel_bg_padding)
245         {
246                 panel_pos += '1 1 0' * panel_bg_padding;
247                 panel_size -= '2 2 0' * panel_bg_padding;
248         }
249
250         int i, j, k, n, g;
251         float a = 1, sz, align, current_msg_posY = 0, msg_size;
252         vector pos;
253         vector cp_fontsize = hud_fontsize * CENTERPRINT_BASE_SIZE;
254         string ts = "";
255         bool all_messages_expired = true;
256
257         pos = panel_pos;
258         if (autocvar_hud_panel_centerprint_flip)
259                 pos.y += panel_size.y;
260         align = bound(0, autocvar_hud_panel_centerprint_align, 1);
261
262         // Show title if available
263         if(centerprint_title) {
264                 vector fontsize = cp_fontsize * autocvar_hud_panel_centerprint_fontscale_title;
265                 float width = stringwidth(centerprint_title, true, fontsize);
266
267                 pos.x = panel_pos.x + (panel_size.x - width) * align;
268
269                 if (autocvar_hud_panel_centerprint_flip)
270                         pos.y -= fontsize.y;
271                 if (centerprint_title_offset && align == 0.5)
272                         pos.x += centerprint_title_offset * CENTERPRINT_BASE_SIZE * autocvar_hud_panel_centerprint_fontscale_title;
273
274                 drawcolorcodedstring(pos, centerprint_title, fontsize, 1, DRAWFLAG_NORMAL);
275
276                 if (autocvar_hud_panel_centerprint_flip)
277                         pos.y -= cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
278                 else
279                         pos.y += fontsize.y + (hud_fontsize.y * CENTERPRINT_TITLE_SPACING);
280
281                 drawfill(pos, vec2(width, 1), '1 1 1', 1, DRAWFLAG_NORMAL);
282
283                 if (autocvar_hud_panel_centerprint_flip)
284                         pos.y -= cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
285                 else
286                         pos.y += cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
287
288                 all_messages_expired = false;
289         }
290
291         for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
292         {
293                 if (j == CENTERPRINT_MAX_MSGS)
294                         j = 0;
295                 if (centerprint_expire_time[j] == -1)
296                 {
297                         // here we are sure the time variable is not altered by CSQC_Ent_Update
298                         centerprint_expire_time[j] = time;
299                         if (centerprint_time[j] > 0)
300                                 centerprint_expire_time[j] += centerprint_time[j];
301                 }
302                 if (centerprint_expire_time[j] <= time)
303                 {
304                         if (centerprint_countdown_num[j] && centerprint_time[j] > 0 && centerprint_start_time[j])
305                         {
306                                 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
307                                 if (centerprint_countdown_num[j] == 0)
308                                         continue;
309                                 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
310                         }
311                         else if(centerprint_time[j] != -1)
312                                 continue;
313                 }
314
315                 all_messages_expired = false;
316
317                 if (time < centerprint_start_time[j]) continue;
318
319                 float fade_in_time = autocvar_hud_panel_centerprint_fade_in;
320                 float fade_out_time = autocvar_hud_panel_centerprint_fade_out;
321
322                 if (centerprint_countdown_num[j] && centerprint_start_time[j]) {
323                         fade_in_time = 0;
324                         fade_out_time = 0;
325                 }
326
327                 // fade
328                 if(fade_in_time && centerprint_start_time[j] && time < centerprint_start_time[j] + fade_in_time) // Fade in
329                         a = (time - centerprint_start_time[j]) / fade_in_time;
330                 else if(time < centerprint_expire_time[j] - fade_out_time || centerprint_time[j] < 0) // Regularily printed or forced
331                         a = 1;
332                 else if(fade_out_time) // Expiring soon, so fade it out.
333                         a = (centerprint_expire_time[j] - time) / fade_out_time;
334
335                 if(centerprint_msgID[j] == ORDINAL(CPID_TIMEIN))
336                         a = 1;
337
338                 // while counting down show it anyway in order to hold the current message position
339                 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0)  // Guaranteed invisible - don't show.
340                         continue;
341
342                 // also fade it based on positioning
343                 if(autocvar_hud_panel_centerprint_fade_subsequent)
344                 {
345                         // pass one: all messages after the first have half alpha
346                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
347                         // pass two: after that, gradually lower alpha even more for each message
348                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
349                 }
350                 a *= panel_fg_alpha;
351
352                 // finally set the size based on the alpha
353                 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
354                 drawfontscale = hud_scale * sz;
355
356                 if (centerprint_countdown_num[j])
357                         n = tokenizebyseparator(strreplace("^COUNT", ftos(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
358                 else
359                         n = tokenizebyseparator(centerprint_messages[j], "\n");
360
361                 if (autocvar_hud_panel_centerprint_flip)
362                 {
363                         // check if the message can be entirely shown
364                         for(k = 0; k < n; ++k)
365                         {
366                                 getWrappedLine_remaining = argv(k);
367                                 while(getWrappedLine_remaining)
368                                 {
369                                         bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
370                                         vector fontsize = cp_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
371
372                                         ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
373                                         if (ts != "")
374                                                 pos.y -= fontsize.y;
375                                         else
376                                                 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
377                                 }
378                         }
379                         current_msg_posY = pos.y; // save starting pos (first line) of the current message
380                 }
381
382                 msg_size = pos.y;
383                 for(k = 0; k < n; ++k)
384                 {
385                         getWrappedLine_remaining = argv(k);
386
387                         bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
388                         vector fontsize = cp_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
389                         if (is_bold)
390                                 getWrappedLine_remaining = substring(getWrappedLine_remaining, 5, -1);
391
392                         while(getWrappedLine_remaining)
393                         {
394                                 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
395                                 if (ts != "")
396                                 {
397                                         if (align)
398                                                 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
399                                         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.
400                                                 if (is_bold) draw_beginBoldFont();
401                                                 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
402                                                 if (is_bold) draw_endBoldFont();
403                                         pos.y += fontsize.y;
404                                 }
405                                 else
406                                         pos.y += fontsize.y * CENTERPRINT_SPACING/2;
407                         }
408                 }
409
410                 ++g; // move next position number up
411
412                 msg_size = pos.y - msg_size;
413
414                 if (autocvar_hud_panel_centerprint_flip)
415                 {
416                         pos.y -= msg_size + CENTERPRINT_SPACING * cp_fontsize.y;
417                         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
418                                 pos.y += (1 - sqrt(a));
419
420                         if (pos.y < panel_pos.y) // check if the next message can be shown
421                         {
422                                 drawfontscale = hud_scale;
423                                 return;
424                         }
425                 }
426                 else
427                 {
428                         pos.y += CENTERPRINT_SPACING * cp_fontsize.y;
429                         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
430                                 pos.y -= (1 - sqrt(a));
431
432                         if(pos.y > panel_pos.y + panel_size.y - cp_fontsize.y) // check if the next message can be shown
433                         {
434                                 drawfontscale = hud_scale;
435                                 return;
436                         }
437                 }
438         }
439
440         drawfontscale = hud_scale;
441         if (all_messages_expired)
442         {
443                 centerprint_showing = false;
444                 centerprint_KillAll();
445         }
446 }