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.4;
37 const float CENTERPRINT_SPACING = 0.5;
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 float centerprint_title_offset;
51 void centerprint_Add(int new_id, string strMessage, float duration, int countdown_num)
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));
57 if(strMessage == "" && new_id == 0)
60 // strip trailing newlines
61 j = strlen(strMessage) - 1;
62 while(substring(strMessage, j, 1) == "\n" && j >= 0)
64 if (j < strlen(strMessage) - 1)
65 strMessage = substring(strMessage, 0, j + 1);
67 if(strMessage == "" && new_id == 0)
70 // strip leading newlines
72 while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
75 strMessage = substring(strMessage, j, strlen(strMessage) - j);
77 if(strMessage == "" && new_id == 0)
80 if (!centerprint_showing)
81 centerprint_showing = true;
83 for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
85 if (j == CENTERPRINT_MAX_MSGS)
87 if (new_id && new_id == centerprint_msgID[j])
89 if (strMessage == "" && centerprint_messages[j] != "")
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!
97 break; // found a msg with the same id, at position j
101 if (strMessage == "")
104 if (i == CENTERPRINT_MAX_MSGS)
106 // a msg with the same id was not found, add the msg at the next position
109 cpm_index = CENTERPRINT_MAX_MSGS - 1;
112 strcpy(centerprint_messages[j], strMessage);
113 centerprint_start_time[j] = time;
114 centerprint_msgID[j] = new_id;
117 centerprint_time[j] = -1;
118 centerprint_expire_time[j] = -1; // don't use the variable time here!
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!
127 centerprint_countdown_num[j] = countdown_num;
130 void centerprint_Kill(int id)
133 centerprint_Add(id, "", 0, 0);
136 void centerprint_AddStandard(string strMessage)
138 centerprint_Add(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
141 void centerprint_KillAll()
143 for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
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]);
153 void centerprint_ClearTitle()
155 strfree(centerprint_title);
156 centerprint_title_offset = 0;
159 void centerprint_SetTitle(string title, float offset)
161 if(title != centerprint_title) {
162 if(centerprint_title)
163 strfree(centerprint_title);
165 centerprint_title = strzone(CCR(title));
166 centerprint_title_offset = offset;
170 float hud_configure_cp_generation_time;
171 void HUD_CenterPrint()
173 if(!autocvar__hud_configure)
175 if(!autocvar_hud_panel_centerprint) return;
177 if(hud_configure_prev) {
178 centerprint_ClearTitle();
179 centerprint_KillAll();
184 if(!hud_configure_prev)
186 centerprint_KillAll();
187 hud_configure_cp_generation_time = time; // show a message immediately
189 if (time > hud_configure_cp_generation_time)
191 if(highlightedPanel == HUD_PANEL(CENTERPRINT))
193 centerprint_SetTitle(sprintf(_("Title at %s"), seconds_tostring(hud_configure_cp_generation_time)), 0);
198 centerprint_Add(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
200 centerprint_Add(0, sprintf(_("^1Multiline message at time %s that\n^BOLDlasts longer than normal"), seconds_tostring(time)), 20, 0);
202 centerprint_AddStandard(sprintf(_("Message at time %s"), seconds_tostring(time)));
203 hud_configure_cp_generation_time = time + 1 + random()*4;
207 centerprint_Add(0, _("Generic message"), 10, 0);
208 hud_configure_cp_generation_time = time + 10 - random()*3;
213 HUD_Panel_LoadCvars();
215 if ( HUD_Radar_Clickable() )
217 if (hud_panel_radar_bottom >= vid_conheight)
220 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
221 panel_pos.y = hud_panel_radar_bottom;
222 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
224 else if(!autocvar__hud_configure && scoreboard_fade_alpha)
226 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
227 if(target_pos.y > panel_pos.y)
229 panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
230 panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
233 // move the panel below the scoreboard
234 if (panel_pos.y >= vid_conheight)
238 if (autocvar_hud_panel_centerprint_dynamichud)
244 if (!centerprint_showing)
249 panel_pos += '1 1 0' * panel_bg_padding;
250 panel_size -= '2 2 0' * panel_bg_padding;
254 float a = 1, sz, align, current_msg_posY = 0, msg_size;
256 vector cp_fontsize = hud_fontsize * CENTERPRINT_BASE_SIZE;
258 bool all_messages_expired = true;
261 if (autocvar_hud_panel_centerprint_flip)
262 pos.y += panel_size.y;
263 align = bound(0, autocvar_hud_panel_centerprint_align, 1);
265 // Show title if available
266 if(centerprint_title) {
267 vector fontsize = cp_fontsize * autocvar_hud_panel_centerprint_fontscale_title;
268 float width = stringwidth(centerprint_title, true, fontsize);
270 pos.x = panel_pos.x + (panel_size.x - width) * align;
272 if (autocvar_hud_panel_centerprint_flip)
274 if (centerprint_title_offset && align == 0.5)
275 pos.x += centerprint_title_offset * CENTERPRINT_BASE_SIZE * autocvar_hud_panel_centerprint_fontscale_title;
277 drawcolorcodedstring(pos, centerprint_title, fontsize, 1, DRAWFLAG_NORMAL);
279 if (autocvar_hud_panel_centerprint_flip)
280 pos.y -= cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
282 pos.y += fontsize.y + (hud_fontsize.y * CENTERPRINT_TITLE_SPACING);
284 drawfill(pos, vec2(width, 1), '1 1 1', 1, DRAWFLAG_NORMAL);
286 if (autocvar_hud_panel_centerprint_flip)
287 pos.y -= cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
289 pos.y += cp_fontsize.y * CENTERPRINT_TITLE_SPACING;
291 all_messages_expired = false;
294 for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
296 if (j == CENTERPRINT_MAX_MSGS)
298 if (centerprint_expire_time[j] == -1)
300 // here we are sure the time variable is not altered by CSQC_Ent_Update
301 centerprint_expire_time[j] = time;
302 if (centerprint_time[j] > 0)
303 centerprint_expire_time[j] += centerprint_time[j];
305 if (centerprint_expire_time[j] <= time)
307 if (centerprint_countdown_num[j] && centerprint_time[j] > 0 && centerprint_start_time[j])
309 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
310 if (centerprint_countdown_num[j] == 0)
312 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
314 else if(centerprint_time[j] != -1)
318 all_messages_expired = false;
320 if (time < centerprint_start_time[j]) continue;
322 float fade_in_time = autocvar_hud_panel_centerprint_fade_in;
323 float fade_out_time = autocvar_hud_panel_centerprint_fade_out;
325 if (centerprint_countdown_num[j] && centerprint_start_time[j]) {
331 if(fade_in_time && centerprint_start_time[j] && time < centerprint_start_time[j] + fade_in_time) // Fade in
332 a = (time - centerprint_start_time[j]) / fade_in_time;
333 else if(time < centerprint_expire_time[j] - fade_out_time || centerprint_time[j] < 0) // Regularily printed or forced
335 else if(fade_out_time) // Expiring soon, so fade it out.
336 a = (centerprint_expire_time[j] - time) / fade_out_time;
338 if(centerprint_msgID[j] == ORDINAL(CPID_TIMEIN))
341 // while counting down show it anyway in order to hold the current message position
342 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0) // Guaranteed invisible - don't show.
345 // also fade it based on positioning
346 if(autocvar_hud_panel_centerprint_fade_subsequent)
348 // pass one: all messages after the first have half alpha
349 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
350 // pass two: after that, gradually lower alpha even more for each message
351 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
355 // finally set the size based on the alpha
356 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
357 drawfontscale = hud_scale * sz;
359 if (centerprint_countdown_num[j])
360 n = tokenizebyseparator(strreplace("^COUNT", ftos(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
362 n = tokenizebyseparator(centerprint_messages[j], "\n");
364 if (autocvar_hud_panel_centerprint_flip)
366 // check if the message can be entirely shown
367 for(k = 0; k < n; ++k)
369 getWrappedLine_remaining = argv(k);
370 while(getWrappedLine_remaining)
372 bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
373 vector fontsize = cp_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
375 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
379 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
382 current_msg_posY = pos.y; // save starting pos (first line) of the current message
386 for(k = 0; k < n; ++k)
388 getWrappedLine_remaining = argv(k);
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 getWrappedLine_remaining = substring(getWrappedLine_remaining, 5, -1);
395 while(getWrappedLine_remaining)
397 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
401 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
402 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.
403 if (is_bold) draw_beginBoldFont();
404 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
405 if (is_bold) draw_endBoldFont();
409 pos.y += fontsize.y * CENTERPRINT_SPACING/2;
413 ++g; // move next position number up
415 msg_size = pos.y - msg_size;
417 if (autocvar_hud_panel_centerprint_flip)
419 pos.y -= msg_size + CENTERPRINT_SPACING * cp_fontsize.y;
420 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
421 pos.y += (1 - sqrt(a));
423 if (pos.y < panel_pos.y) // check if the next message can be shown
425 drawfontscale = hud_scale;
431 pos.y += CENTERPRINT_SPACING * cp_fontsize.y;
432 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
433 pos.y -= (1 - sqrt(a));
435 if(pos.y > panel_pos.y + panel_size.y - cp_fontsize.y) // check if the next message can be shown
437 drawfontscale = hud_scale;
443 drawfontscale = hud_scale;
444 if (all_messages_expired)
446 centerprint_showing = false;
447 centerprint_KillAll();