1 #include "centerprint.qh"
3 #include "scoreboard.qh"
4 #include <common/notifications/all.qh>
5 #include <client/defs.qh>
6 #include <client/miscfunctions.qh>
10 void HUD_CenterPrint_Export(int fh)
12 // allow saving cvars that aesthetically change the panel into hud skin files
13 HUD_Write_Cvar("hud_panel_centerprint_align");
14 HUD_Write_Cvar("hud_panel_centerprint_flip");
15 HUD_Write_Cvar("hud_panel_centerprint_fontscale");
16 HUD_Write_Cvar("hud_panel_centerprint_fontscale_bold");
17 HUD_Write_Cvar("hud_panel_centerprint_time");
18 HUD_Write_Cvar("hud_panel_centerprint_fade_in");
19 HUD_Write_Cvar("hud_panel_centerprint_fade_out");
20 HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent");
21 HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passone");
22 HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passone_minalpha");
23 HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passtwo");
24 HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passtwo_minalpha");
25 HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_minfontsize");
26 HUD_Write_Cvar("hud_panel_centerprint_fade_minfontsize");
29 // These are the functions that draw the text at the center of the screen (e.g. frag messages and server MOTDs).
30 // Usually local_notification_centerprint_generic() is called, which in turn calls centerprint_generic(), which
31 // uses some kind of macro magic to call HUD_CenterPrint, which draws them on screen using drawcolorcodedstring().
33 const int CENTERPRINT_MAX_MSGS = 10;
34 const int CENTERPRINT_MAX_ENTRIES = 50;
35 const float CENTERPRINT_SPACING = 0.7;
37 string centerprint_messages[CENTERPRINT_MAX_MSGS];
38 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
39 float centerprint_time[CENTERPRINT_MAX_MSGS];
40 float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
41 int centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
42 bool centerprint_showing;
44 void centerprint_generic(int new_id, string strMessage, float duration, int countdown_num)
46 TC(int, new_id); TC(int, countdown_num);
47 //printf("centerprint_generic(%d, '%s^7', %d, %d);\n", new_id, strMessage, duration, countdown_num);
50 if(strMessage == "" && new_id == 0)
53 // strip trailing newlines
54 j = strlen(strMessage) - 1;
55 while(substring(strMessage, j, 1) == "\n" && j >= 0)
57 if (j < strlen(strMessage) - 1)
58 strMessage = substring(strMessage, 0, j + 1);
60 if(strMessage == "" && new_id == 0)
63 // strip leading newlines
65 while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
68 strMessage = substring(strMessage, j, strlen(strMessage) - j);
70 if(strMessage == "" && new_id == 0)
73 if (!centerprint_showing)
74 centerprint_showing = true;
76 for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
78 if (j == CENTERPRINT_MAX_MSGS)
80 if (new_id && new_id == centerprint_msgID[j])
82 if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
84 // fade out the current msg (duration and countdown_num are ignored)
85 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
86 centerprint_expire_time[j] = -1; // don't use the variable time here!
89 break; // found a msg with the same id, at position j
93 if (i == CENTERPRINT_MAX_MSGS)
95 // a msg with the same id was not found, add the msg at the next position
98 cpm_index = CENTERPRINT_MAX_MSGS - 1;
101 strcpy(centerprint_messages[j], strMessage);
102 centerprint_msgID[j] = new_id;
105 centerprint_time[j] = -1;
106 centerprint_expire_time[j] = -1; // don't use the variable time here!
111 duration = max(1, autocvar_hud_panel_centerprint_time);
112 centerprint_time[j] = duration;
113 centerprint_expire_time[j] = -1; // don't use the variable time here!
115 centerprint_countdown_num[j] = countdown_num;
118 void centerprint_kill(int id)
121 centerprint_generic(id, "", 0, 0);
124 void centerprint_hud(string strMessage)
126 centerprint_generic(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
129 void reset_centerprint_messages()
131 for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
133 centerprint_expire_time[i] = 0;
134 centerprint_time[i] = 1;
135 centerprint_msgID[i] = 0;
136 strfree(centerprint_messages[i]);
140 float hud_configure_cp_generation_time;
141 void HUD_CenterPrint ()
143 if(!autocvar__hud_configure)
145 if(!autocvar_hud_panel_centerprint) return;
147 if(hud_configure_prev)
148 reset_centerprint_messages();
152 if(!hud_configure_prev)
154 reset_centerprint_messages();
155 hud_configure_cp_generation_time = time; // show a message immediately
157 if (time > hud_configure_cp_generation_time)
159 if(highlightedPanel == HUD_PANEL(CENTERPRINT))
164 centerprint_generic(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
166 centerprint_generic(0, sprintf(_("^1Multiline message at time %s that\n^1lasts longer than normal"), seconds_tostring(time)), 20, 0);
168 centerprint_hud(sprintf(_("Message at time %s"), seconds_tostring(time)));
169 hud_configure_cp_generation_time = time + 1 + random()*4;
173 centerprint_generic(0, _("Generic message"), 10, 0);
174 hud_configure_cp_generation_time = time + 10 - random()*3;
179 HUD_Panel_LoadCvars();
181 if ( HUD_Radar_Clickable() )
183 if (hud_panel_radar_bottom >= 0.96 * vid_conheight)
186 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
187 panel_pos.y = hud_panel_radar_bottom;
188 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
190 else if(!autocvar__hud_configure && scoreboard_fade_alpha)
192 // move the panel below the scoreboard
193 if (scoreboard_bottom >= 0.96 * vid_conheight)
195 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
196 if(target_pos.y > panel_pos.y)
198 panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
199 panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
203 if (autocvar_hud_panel_centerprint_dynamichud)
209 if (!centerprint_showing)
214 panel_pos += '1 1 0' * panel_bg_padding;
215 panel_size -= '2 2 0' * panel_bg_padding;
219 string centerprint_message = strzone("");
225 float a, sz, align, current_msg_posY = 0, msg_size;
228 bool all_messages_expired = true;
231 if (autocvar_hud_panel_centerprint_flip)
232 pos.y += panel_size.y;
233 align = bound(0, autocvar_hud_panel_centerprint_align, 1);
234 for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
236 // if the notification starts with ^BOLD make it bold (e.g. frag messages);
237 is_bold = (substring(centerprint_messages[j], 0, 5) == BOLD_OPERATOR);
238 // remove ^BOLD so it doesn't get printed onscreen
240 strcpy(centerprint_message, substring(centerprint_messages[j], 5, -1));
242 strcpy(centerprint_message, centerprint_messages[j]);
244 // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
245 // height = panel_size_y/entries;
246 // fontsize = '1 1 0' * height;
247 height = (is_bold) ? vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale_bold : vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale;
248 fontsize = '1 1 0' * height;
249 entries = bound(1, floor(panel_size.y/height), CENTERPRINT_MAX_ENTRIES);
251 if (j == CENTERPRINT_MAX_MSGS)
253 if (centerprint_expire_time[j] == -1)
255 // here we are sure the time variable is not altered by CSQC_Ent_Update
256 centerprint_expire_time[j] = time;
257 if (centerprint_time[j] > 0)
258 centerprint_expire_time[j] += centerprint_time[j];
260 if (centerprint_expire_time[j] <= time)
262 if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
264 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
265 if (centerprint_countdown_num[j] == 0)
267 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
269 else if(centerprint_time[j] != -1)
273 all_messages_expired = false;
275 // fade the centerprint_hud in/out
276 if(centerprint_time[j] < 0) // Expired but forced. Expire time is the fade-in time.
277 a = (time - centerprint_expire_time[j]) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
278 else if(centerprint_expire_time[j] - autocvar_hud_panel_centerprint_fade_out > time) // Regularily printed. Not fading out yet.
279 a = (time - (centerprint_expire_time[j] - centerprint_time[j])) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
280 else // Expiring soon, so fade it out.
281 a = (centerprint_expire_time[j] - time) / max(0.0001, autocvar_hud_panel_centerprint_fade_out);
283 if(centerprint_msgID[j] == CPID_TIMEIN)
286 // while counting down show it anyway in order to hold the current message position
287 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0) // Guaranteed invisible - don't show.
292 // set the size from fading in/out before subsequent fading
293 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
295 // also fade it based on positioning
296 if(autocvar_hud_panel_centerprint_fade_subsequent)
298 // pass one: all messages after the first have half alpha
299 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
300 // pass two: after that, gradually lower alpha even more for each message
301 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
305 // finally set the size based on the new alpha from subsequent fading
306 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
307 drawfontscale = hud_scale * sz;
309 if (centerprint_countdown_num[j])
310 n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_message), "\n");
312 n = tokenizebyseparator(centerprint_message, "\n");
314 if (autocvar_hud_panel_centerprint_flip)
316 // check if the message can be entirely shown
317 for(k = 0; k < n; ++k)
319 getWrappedLine_remaining = argv(k);
320 while(getWrappedLine_remaining)
322 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
326 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
329 current_msg_posY = pos.y; // save starting pos (first line) of the current message
333 for(k = 0; k < n; ++k)
335 getWrappedLine_remaining = argv(k);
336 while(getWrappedLine_remaining)
338 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
342 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
343 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.
344 if (is_bold) draw_beginBoldFont();
345 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
346 if (is_bold) draw_endBoldFont();
350 pos.y += fontsize.y * CENTERPRINT_SPACING/2;
354 ++g; // move next position number up
356 msg_size = pos.y - msg_size;
357 if (autocvar_hud_panel_centerprint_flip)
359 pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
360 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
361 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
363 if (pos.y < panel_pos.y) // check if the next message can be shown
365 drawfontscale = hud_scale;
366 strfree(centerprint_message);
372 pos.y += CENTERPRINT_SPACING * fontsize.y;
373 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
374 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
376 if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
378 drawfontscale = hud_scale;
379 strfree(centerprint_message);
384 strfree(centerprint_message);
386 drawfontscale = hud_scale;
387 if (all_messages_expired)
389 centerprint_showing = false;
390 reset_centerprint_messages();