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_SPACING = 0.5;
37 const float CENTERPRINT_TITLE_SPACING = 1;
39 string centerprint_messages[CENTERPRINT_MAX_MSGS];
40 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
41 float centerprint_time[CENTERPRINT_MAX_MSGS];
42 float centerprint_start_time[CENTERPRINT_MAX_MSGS];
43 float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
44 int centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
45 bool centerprint_showing;
47 bool centerprint_title_show;
48 string centerprint_title;
50 void centerprint_Add(int new_id, string strMessage, float duration, int countdown_num)
52 TC(int, new_id); TC(int, countdown_num);
53 LOG_INFOF("centerprint_Add: ^2id: %d ^3dur: %d ^5countdown: %d\n'%s'", new_id, duration, countdown_num, strreplace("\n", "^7\\n^7", strMessage));
56 if(strMessage == "" && new_id == 0)
59 // strip trailing newlines
60 j = strlen(strMessage) - 1;
61 while(substring(strMessage, j, 1) == "\n" && j >= 0)
63 if (j < strlen(strMessage) - 1)
64 strMessage = substring(strMessage, 0, j + 1);
66 if(strMessage == "" && new_id == 0)
69 // strip leading newlines
71 while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
74 strMessage = substring(strMessage, j, strlen(strMessage) - j);
76 if(strMessage == "" && new_id == 0)
79 if (!centerprint_showing)
80 centerprint_showing = true;
82 for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
84 if (j == CENTERPRINT_MAX_MSGS)
86 if (new_id && new_id == centerprint_msgID[j])
88 if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
90 // fade out the current msg (duration and countdown_num are ignored)
91 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
92 centerprint_expire_time[j] = -1; // don't use the variable time here!
95 break; // found a msg with the same id, at position j
99 if (i == CENTERPRINT_MAX_MSGS)
101 // a msg with the same id was not found, add the msg at the next position
104 cpm_index = CENTERPRINT_MAX_MSGS - 1;
107 strcpy(centerprint_messages[j], strMessage);
108 centerprint_start_time[j] = time;
109 centerprint_msgID[j] = new_id;
112 centerprint_time[j] = -1;
113 centerprint_expire_time[j] = -1; // don't use the variable time here!
118 duration = max(1, autocvar_hud_panel_centerprint_time);
119 centerprint_time[j] = duration;
120 centerprint_expire_time[j] = -1; // don't use the variable time here!
122 centerprint_countdown_num[j] = countdown_num;
125 void centerprint_Kill(int id)
128 centerprint_Add(id, "", 0, 0);
131 void centerprint_AddStandard(string strMessage)
133 centerprint_Add(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
136 void centerprint_KillAll()
138 for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
140 centerprint_start_time[i] = 0;
141 centerprint_expire_time[i] = 0;
142 centerprint_time[i] = 1;
143 centerprint_msgID[i] = 0;
144 strfree(centerprint_messages[i]);
148 void centerprint_ClearTitle()
150 strfree(centerprint_title);
151 centerprint_title_show = false;
154 void centerprint_SetTitle(string title)
156 if(title != centerprint_title) {
157 if(centerprint_title)
158 strfree(centerprint_title);
160 centerprint_title = strzone(title);
161 centerprint_title_show = true;
165 float hud_configure_cp_generation_time;
166 void HUD_CenterPrint()
168 if(!autocvar__hud_configure)
170 if(!autocvar_hud_panel_centerprint) return;
172 if(hud_configure_prev) {
173 centerprint_ClearTitle();
174 centerprint_KillAll();
179 if(!hud_configure_prev)
181 centerprint_KillAll();
182 hud_configure_cp_generation_time = time; // show a message immediately
184 if (time > hud_configure_cp_generation_time)
186 if(highlightedPanel == HUD_PANEL(CENTERPRINT))
188 centerprint_SetTitle(strcat("test", ftos(hud_configure_cp_generation_time)));
193 centerprint_Add(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
195 centerprint_Add(0, sprintf(_("^1Multiline message at time %s that\n^BOLDlasts longer than normal"), seconds_tostring(time)), 20, 0);
197 centerprint_AddStandard(sprintf(_("Message at time %s"), seconds_tostring(time)));
198 //hud_configure_cp_generation_time = time + 1 + random()*4;
199 hud_configure_cp_generation_time = time + 1;
203 centerprint_Add(0, _("Generic message"), 10, 0);
204 hud_configure_cp_generation_time = time + 10 - random()*3;
209 HUD_Panel_LoadCvars();
211 if ( HUD_Radar_Clickable() )
213 if (hud_panel_radar_bottom >= vid_conheight)
216 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
217 panel_pos.y = hud_panel_radar_bottom;
218 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
220 else if(!autocvar__hud_configure && scoreboard_fade_alpha)
222 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
223 if(target_pos.y > panel_pos.y)
225 panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
226 panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
229 // move the panel below the scoreboard
230 if (panel_pos.y >= vid_conheight)
234 if (autocvar_hud_panel_centerprint_dynamichud)
240 if (!centerprint_showing)
245 panel_pos += '1 1 0' * panel_bg_padding;
246 panel_size -= '2 2 0' * panel_bg_padding;
250 float a = 1, sz, align, current_msg_posY = 0, msg_size;
253 bool all_messages_expired = true;
256 if (autocvar_hud_panel_centerprint_flip)
257 pos.y += panel_size.y;
258 align = bound(0, autocvar_hud_panel_centerprint_align, 1);
260 // Show title if available
261 if(centerprint_title_show) {
262 vector fontsize = hud_fontsize * autocvar_hud_panel_centerprint_fontscale_title;
264 pos.x = panel_pos.x + (panel_size.x - stringwidth(centerprint_title, true, fontsize)) * align;
265 drawcolorcodedstring(pos, centerprint_title, fontsize, 1, DRAWFLAG_NORMAL);
267 if (autocvar_hud_panel_centerprint_flip)
268 pos.y -= fontsize.y + CENTERPRINT_TITLE_SPACING;
270 pos.y += fontsize.y + CENTERPRINT_TITLE_SPACING;
272 all_messages_expired = false;
275 for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
277 if (j == CENTERPRINT_MAX_MSGS)
279 if (centerprint_expire_time[j] == -1)
281 // here we are sure the time variable is not altered by CSQC_Ent_Update
282 centerprint_expire_time[j] = time;
283 if (centerprint_time[j] > 0)
284 centerprint_expire_time[j] += centerprint_time[j];
286 if (centerprint_expire_time[j] <= time)
288 if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
290 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
291 if (centerprint_countdown_num[j] == 0)
293 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
295 else if(centerprint_time[j] != -1)
299 all_messages_expired = false;
301 if (time < centerprint_start_time[j]) continue;
303 float fade_in_time = autocvar_hud_panel_centerprint_fade_in;
304 float fade_out_time = autocvar_hud_panel_centerprint_fade_out;
306 if (centerprint_countdown_num[j]) {
307 fade_in_time = autocvar_hud_panel_centerprint_fade_in_short;
312 if(fade_in_time && centerprint_start_time[j] && time < centerprint_start_time[j] + fade_in_time) // Fade in
313 a = (time - centerprint_start_time[j]) / fade_in_time;
314 else if(time < centerprint_expire_time[j] - fade_out_time || centerprint_time[j] < 0) // Regularily printed or forced
316 else if(fade_out_time) // Expiring soon, so fade it out.
317 a = (centerprint_expire_time[j] - time) / fade_out_time;
319 if(centerprint_msgID[j] == ORDINAL(CPID_TIMEIN))
322 // while counting down show it anyway in order to hold the current message position
323 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0) // Guaranteed invisible - don't show.
326 // set the size from fading in/out before subsequent fading
327 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
329 // also fade it based on positioning
330 if(autocvar_hud_panel_centerprint_fade_subsequent)
332 // pass one: all messages after the first have half alpha
333 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
334 // pass two: after that, gradually lower alpha even more for each message
335 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
339 // finally set the size based on the new alpha from subsequent fading
340 // TODO: Apply this only if subsequent, otherwise it fucks it up
342 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
343 drawfontscale = hud_scale * sz;
345 if (centerprint_countdown_num[j])
346 n = tokenizebyseparator(strreplace("^COUNT", ftos(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
348 n = tokenizebyseparator(centerprint_messages[j], "\n");
350 if (autocvar_hud_panel_centerprint_flip)
352 // check if the message can be entirely shown
353 for(k = 0; k < n; ++k)
355 getWrappedLine_remaining = argv(k);
356 while(getWrappedLine_remaining)
358 bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
359 vector fontsize = hud_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
361 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
365 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
368 current_msg_posY = pos.y; // save starting pos (first line) of the current message
372 for(k = 0; k < n; ++k)
374 getWrappedLine_remaining = argv(k);
376 bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
377 vector fontsize = hud_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
379 getWrappedLine_remaining = substring(getWrappedLine_remaining, 5, -1);
381 while(getWrappedLine_remaining)
383 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
387 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
388 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.
389 if (is_bold) draw_beginBoldFont();
390 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
391 if (is_bold) draw_endBoldFont();
395 pos.y += fontsize.y * CENTERPRINT_SPACING/2;
399 ++g; // move next position number up
401 msg_size = pos.y - msg_size;
403 if (autocvar_hud_panel_centerprint_flip)
405 pos.y -= msg_size + CENTERPRINT_SPACING * hud_fontsize.y;
406 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
407 pos.y += (1 - sqrt(a));
409 if (pos.y < panel_pos.y) // check if the next message can be shown
411 drawfontscale = hud_scale;
417 pos.y += CENTERPRINT_SPACING * hud_fontsize.y;
418 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
419 pos.y -= (1 - sqrt(a));
421 if(pos.y > panel_pos.y + panel_size.y - hud_fontsize.y) // check if the next message can be shown
423 drawfontscale = hud_scale;
429 drawfontscale = hud_scale;
430 if (all_messages_expired)
432 centerprint_showing = false;
433 centerprint_KillAll();