1 #include "centerprint.qh"
3 #include <client/draw.qh>
4 #include <client/hud/panel/scoreboard.qh>
5 #include <common/notifications/all.qh>
9 void HUD_CenterPrint_Export(int fh)
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");
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
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;
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;
48 string centerprint_title;
49 string centerprint_title_left;
50 string centerprint_title_right;
52 void centerprint_Add(int new_id, string strMessage, float duration, int countdown_num)
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));
58 if(strMessage == "" && new_id == 0)
61 // strip trailing newlines
62 j = strlen(strMessage) - 1;
63 while(substring(strMessage, j, 1) == "\n" && j >= 0)
65 if (j < strlen(strMessage) - 1)
66 strMessage = substring(strMessage, 0, j + 1);
68 if(strMessage == "" && new_id == 0)
71 // strip leading newlines
73 while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
76 strMessage = substring(strMessage, j, strlen(strMessage) - j);
78 if(strMessage == "" && new_id == 0)
81 if (!centerprint_showing)
82 centerprint_showing = true;
84 for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
86 if (j == CENTERPRINT_MAX_MSGS)
88 if (new_id && new_id == centerprint_msgID[j])
90 if (strMessage == "" && centerprint_messages[j] != "")
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!
98 break; // found a msg with the same id, at position j
102 if (strMessage == "")
105 if (i == CENTERPRINT_MAX_MSGS)
107 // a msg with the same id was not found, add the msg at the next position
110 cpm_index = CENTERPRINT_MAX_MSGS - 1;
113 strcpy(centerprint_messages[j], strMessage);
114 centerprint_start_time[j] = time;
115 centerprint_msgID[j] = new_id;
118 centerprint_time[j] = -1;
119 centerprint_expire_time[j] = -1; // don't use the variable time here!
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!
128 centerprint_countdown_num[j] = countdown_num;
131 void centerprint_Kill(int id)
134 centerprint_Add(id, "", 0, 0);
137 void centerprint_AddStandard(string strMessage)
139 centerprint_Add(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
142 void centerprint_KillAll()
144 for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
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]);
154 void centerprint_SetDuelTitle(string left, string right, string div)
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 centerprint_SetTitle(sprintf("^BG%s^BG %s %s", centerprint_title_left, div, centerprint_title_right));
162 void centerprint_SetTitle(string title)
164 if(title != centerprint_title)
165 strcpy(centerprint_title, CCR(title));
168 void centerprint_ClearTitle()
170 strfree(centerprint_title);
171 strfree(centerprint_title_left);
172 strfree(centerprint_title_right);
175 float hud_configure_cp_generation_time;
176 void HUD_CenterPrint()
178 if(!autocvar__hud_configure)
180 if(!autocvar_hud_panel_centerprint) return;
182 if(hud_configure_prev) {
183 centerprint_ClearTitle();
184 centerprint_KillAll();
189 if(!hud_configure_prev)
191 centerprint_KillAll();
192 hud_configure_cp_generation_time = time; // show a message immediately
194 if (time > hud_configure_cp_generation_time)
196 if(highlightedPanel == HUD_PANEL(CENTERPRINT))
198 centerprint_SetTitle(sprintf(_("Title at %s"), seconds_tostring(hud_configure_cp_generation_time)));
203 centerprint_Add(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
205 centerprint_Add(0, sprintf(_("^1Multiline message at time %s that\n^BOLDlasts longer than normal"), seconds_tostring(time)), 20, 0);
207 centerprint_AddStandard(sprintf(_("Message at time %s"), seconds_tostring(time)));
208 hud_configure_cp_generation_time = time + 1 + random()*4;
212 centerprint_Add(0, _("Generic message"), 10, 0);
213 hud_configure_cp_generation_time = time + 10 - random()*3;
218 HUD_Panel_LoadCvars();
220 if ( HUD_Radar_Clickable() )
222 if (hud_panel_radar_bottom >= vid_conheight)
225 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
226 panel_pos.y = hud_panel_radar_bottom;
227 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
229 else if(!autocvar__hud_configure && scoreboard_fade_alpha)
231 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
232 if(target_pos.y > panel_pos.y)
234 panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
235 panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
238 // move the panel below the scoreboard
239 if (panel_pos.y >= vid_conheight)
243 if (autocvar_hud_panel_centerprint_dynamichud)
249 if (!centerprint_showing)
254 panel_pos += '1 1 0' * panel_bg_padding;
255 panel_size -= '2 2 0' * panel_bg_padding;
259 float a = 1, sz, align, current_msg_posY = 0, msg_size;
261 vector cp_fontsize = hud_fontsize * CENTERPRINT_BASE_SIZE;
263 bool all_messages_expired = true;
266 if (autocvar_hud_panel_centerprint_flip)
267 pos.y += panel_size.y;
268 align = bound(0, autocvar_hud_panel_centerprint_align, 1);
270 // Show title if available
271 if(centerprint_title) {
272 vector fontsize = cp_fontsize * autocvar_hud_panel_centerprint_fontscale_title;
273 float width = stringwidth(centerprint_title, true, fontsize);
275 pos.x = panel_pos.x + (panel_size.x - width) * align;
277 if (autocvar_hud_panel_centerprint_flip)
279 float right_width = 0;
280 float left_width = 0;
281 if (centerprint_title_left != "")
283 right_width = stringwidth(centerprint_title_right, true, fontsize);
284 left_width = stringwidth(centerprint_title_left, true, fontsize);
285 if (align == 0.5) // Center line at the main word (for duels)
286 pos.x += (right_width - left_width) / 2;
289 drawcolorcodedstring(pos, centerprint_title, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
291 if (autocvar_hud_panel_centerprint_flip)
292 pos.y -= cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
294 pos.y += fontsize.y + (hud_fontsize.y * CENTERPRINT_TITLE_SPACING);
296 if (centerprint_title_left != "")
298 drawfill(pos, vec2(left_width, 1), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
299 drawfill(pos + vec2(width - right_width, 1), vec2(right_width, 1), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
302 drawfill(pos, vec2(width, 1), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
304 if (autocvar_hud_panel_centerprint_flip)
305 pos.y -= cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
307 pos.y += cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
309 all_messages_expired = false;
312 for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
314 if (j == CENTERPRINT_MAX_MSGS)
316 if (centerprint_expire_time[j] == -1)
318 // here we are sure the time variable is not altered by CSQC_Ent_Update
319 centerprint_expire_time[j] = time;
320 if (centerprint_time[j] > 0)
321 centerprint_expire_time[j] += centerprint_time[j];
323 if (centerprint_expire_time[j] <= time)
325 if (centerprint_countdown_num[j] && centerprint_time[j] > 0 && centerprint_start_time[j])
327 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
328 if (centerprint_countdown_num[j] == 0)
330 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
332 else if(centerprint_time[j] != -1)
336 all_messages_expired = false;
338 if (time < centerprint_start_time[j]) continue;
340 float fade_in_time = autocvar_hud_panel_centerprint_fade_in;
341 float fade_out_time = autocvar_hud_panel_centerprint_fade_out;
343 if (centerprint_countdown_num[j] && centerprint_start_time[j]) {
349 if(fade_in_time && centerprint_start_time[j] && time < centerprint_start_time[j] + fade_in_time) // Fade in
350 a = (time - centerprint_start_time[j]) / fade_in_time;
351 else if(time < centerprint_expire_time[j] - fade_out_time || centerprint_time[j] < 0) // Regularily printed or forced
353 else if(fade_out_time) // Expiring soon, so fade it out.
354 a = (centerprint_expire_time[j] - time) / fade_out_time;
356 if(centerprint_msgID[j] == ORDINAL(CPID_TIMEIN))
359 // while counting down show it anyway in order to hold the current message position
360 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0) // Guaranteed invisible - don't show.
363 // also fade it based on positioning
364 if(autocvar_hud_panel_centerprint_fade_subsequent)
366 // pass one: all messages after the first have half alpha
367 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
368 // pass two: after that, gradually lower alpha even more for each message
369 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
373 // finally set the size based on the alpha
374 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
375 drawfontscale = hud_scale * sz;
377 if (centerprint_countdown_num[j])
378 n = tokenizebyseparator(strreplace("^COUNT", ftos(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
380 n = tokenizebyseparator(centerprint_messages[j], "\n");
382 if (autocvar_hud_panel_centerprint_flip)
384 // check if the message can be entirely shown
385 for(k = 0; k < n; ++k)
387 getWrappedLine_remaining = argv(k);
388 while(getWrappedLine_remaining)
390 bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
391 vector fontsize = cp_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
393 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
397 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
400 current_msg_posY = pos.y; // save starting pos (first line) of the current message
404 for(k = 0; k < n; ++k)
406 getWrappedLine_remaining = argv(k);
408 bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
409 vector fontsize = cp_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
411 getWrappedLine_remaining = substring(getWrappedLine_remaining, 5, -1);
413 while(getWrappedLine_remaining)
415 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
419 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
420 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.
421 if (is_bold) draw_beginBoldFont();
422 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
423 if (is_bold) draw_endBoldFont();
427 pos.y += fontsize.y * CENTERPRINT_SPACING/2;
431 ++g; // move next position number up
433 msg_size = pos.y - msg_size;
435 if (autocvar_hud_panel_centerprint_flip)
437 pos.y -= msg_size + CENTERPRINT_SPACING * cp_fontsize.y;
438 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
439 pos.y += (1 - sqrt(a));
441 if (pos.y < panel_pos.y) // check if the next message can be shown
443 drawfontscale = hud_scale;
449 pos.y += CENTERPRINT_SPACING * cp_fontsize.y;
450 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
451 pos.y -= (1 - sqrt(a));
453 if(pos.y > panel_pos.y + panel_size.y - cp_fontsize.y) // check if the next message can be shown
455 drawfontscale = hud_scale;
461 drawfontscale = hud_scale;
462 if (all_messages_expired)
464 centerprint_showing = false;
465 centerprint_KillAll();