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.7;
38 string centerprint_messages[CENTERPRINT_MAX_MSGS];
39 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
40 bool centerprint_bold[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 void centerprint_Add(int new_id, string strMessage, float duration, int countdown_num)
49 TC(int, new_id); TC(int, countdown_num);
50 LOG_INFOF("centerprint_Add: ^2id: %d ^3dur: %d ^5countdown: %d\n'%s'", new_id, duration, countdown_num, strreplace("\n", "^7\\n^7", strMessage));
53 if(strMessage == "" && new_id == 0)
56 // strip BOLD_OPERATOR
57 bool is_bold = (substring(strMessage, 0, 5) == BOLD_OPERATOR);
59 strMessage = substring(strMessage, 5, -1);
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] != "" && centerprint_countdown_num[j] == 0)
92 // fade out the current msg (duration and countdown_num are ignored)
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 (i == CENTERPRINT_MAX_MSGS)
103 // a msg with the same id was not found, add the msg at the next position
106 cpm_index = CENTERPRINT_MAX_MSGS - 1;
109 strcpy(centerprint_messages[j], strMessage);
110 centerprint_start_time[j] = time;
111 centerprint_bold[j] = is_bold;
112 centerprint_msgID[j] = new_id;
115 centerprint_time[j] = -1;
116 centerprint_expire_time[j] = -1; // don't use the variable time here!
121 duration = max(1, autocvar_hud_panel_centerprint_time);
122 centerprint_time[j] = duration;
123 centerprint_expire_time[j] = -1; // don't use the variable time here!
125 centerprint_countdown_num[j] = countdown_num;
128 void centerprint_Kill(int id)
131 centerprint_Add(id, "", 0, 0);
134 void centerprint_AddStandard(string strMessage)
136 centerprint_Add(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
139 void centerprint_KillAll()
141 for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
143 centerprint_expire_time[i] = 0;
144 centerprint_time[i] = 1;
145 centerprint_msgID[i] = 0;
146 centerprint_bold[i] = false;
147 strfree(centerprint_messages[i]);
151 float hud_configure_cp_generation_time;
152 void HUD_CenterPrint()
154 if(!autocvar__hud_configure)
156 if(!autocvar_hud_panel_centerprint) return;
158 if(hud_configure_prev)
159 centerprint_KillAll();
163 if(!hud_configure_prev)
165 centerprint_KillAll();
166 hud_configure_cp_generation_time = time; // show a message immediately
168 if (time > hud_configure_cp_generation_time)
170 if(highlightedPanel == HUD_PANEL(CENTERPRINT))
175 centerprint_Add(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
177 centerprint_Add(0, sprintf(_("^1Multiline message at time %s that\n^1lasts longer than normal"), seconds_tostring(time)), 20, 0);
179 centerprint_AddStandard(sprintf(_("Message at time %s"), seconds_tostring(time)));
180 hud_configure_cp_generation_time = time + 1 + random()*4;
184 centerprint_Add(0, _("Generic message"), 10, 0);
185 hud_configure_cp_generation_time = time + 10 - random()*3;
190 HUD_Panel_LoadCvars();
192 if ( HUD_Radar_Clickable() )
194 if (hud_panel_radar_bottom >= vid_conheight)
197 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
198 panel_pos.y = hud_panel_radar_bottom;
199 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
201 else if(!autocvar__hud_configure && scoreboard_fade_alpha)
203 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
204 if(target_pos.y > panel_pos.y)
206 panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
207 panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
210 // move the panel below the scoreboard
211 if (panel_pos.y >= vid_conheight)
215 if (autocvar_hud_panel_centerprint_dynamichud)
221 if (!centerprint_showing)
226 panel_pos += '1 1 0' * panel_bg_padding;
227 panel_size -= '2 2 0' * panel_bg_padding;
235 float a, sz, align, current_msg_posY = 0, msg_size;
238 bool all_messages_expired = true;
241 if (autocvar_hud_panel_centerprint_flip)
242 pos.y += panel_size.y;
243 align = bound(0, autocvar_hud_panel_centerprint_align, 1);
245 for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
247 fontsize = hud_fontsize;
248 if (j == CENTERPRINT_MAX_MSGS)
250 if (centerprint_expire_time[j] == -1)
252 // here we are sure the time variable is not altered by CSQC_Ent_Update
253 centerprint_expire_time[j] = time;
254 if (centerprint_time[j] > 0)
255 centerprint_expire_time[j] += centerprint_time[j];
257 if (centerprint_expire_time[j] <= time)
259 if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
261 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
262 if (centerprint_countdown_num[j] == 0)
264 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
266 else if(centerprint_time[j] != -1)
270 all_messages_expired = false;
272 if (time < centerprint_start_time[j]) continue;
274 float fade_in_time = autocvar_hud_panel_centerprint_fade_in;
275 float fade_out_time = autocvar_hud_panel_centerprint_fade_out;
277 if (centerprint_time[j] == 1)
278 fade_in_time = autocvar_hud_panel_centerprint_fade_in_short;
282 if(fade_in_time && centerprint_start_time[j] && time < centerprint_start_time[j] + fade_in_time) // Fade in
283 a = (time - centerprint_start_time[j]) / fade_in_time;
284 else if(time < centerprint_expire_time[j] - fade_out_time || centerprint_time[j] < 0) // Regularily printed or forced
286 else if(fade_out_time) // Expiring soon, so fade it out.
287 a = (centerprint_expire_time[j] - time) / fade_out_time;
289 if(centerprint_msgID[j] == ORDINAL(CPID_TIMEIN))
292 // while counting down show it anyway in order to hold the current message position
293 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0) // Guaranteed invisible - don't show.
296 // set the size from fading in/out before subsequent fading
297 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
299 // also fade it based on positioning
300 if(autocvar_hud_panel_centerprint_fade_subsequent)
302 // pass one: all messages after the first have half alpha
303 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
304 // pass two: after that, gradually lower alpha even more for each message
305 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
309 // finally set the size based on the new alpha from subsequent fading
310 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
311 drawfontscale = hud_scale * sz;
313 if (centerprint_countdown_num[j])
314 n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
316 n = tokenizebyseparator(centerprint_messages[j], "\n");
318 if (autocvar_hud_panel_centerprint_flip)
320 // check if the message can be entirely shown
321 for(k = 0; k < n; ++k)
323 getWrappedLine_remaining = argv(k);
324 while(getWrappedLine_remaining)
326 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
330 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
333 current_msg_posY = pos.y; // save starting pos (first line) of the current message
337 for(k = 0; k < n; ++k)
339 getWrappedLine_remaining = argv(k);
340 while(getWrappedLine_remaining)
342 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
346 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
347 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.
348 if (is_bold) draw_beginBoldFont();
349 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
350 if (is_bold) draw_endBoldFont();
354 pos.y += fontsize.y * CENTERPRINT_SPACING/2;
358 ++g; // move next position number up
360 msg_size = pos.y - msg_size;
361 if (autocvar_hud_panel_centerprint_flip)
363 pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
364 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
365 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
367 if (pos.y < panel_pos.y) // check if the next message can be shown
369 drawfontscale = hud_scale;
375 pos.y += CENTERPRINT_SPACING * fontsize.y;
376 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
377 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
379 if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
381 drawfontscale = hud_scale;
387 drawfontscale = hud_scale;
388 if (all_messages_expired)
390 centerprint_showing = false;
391 centerprint_KillAll();