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